import pygame import time import os import sys import math from pygame import K_ESCAPE from pygame.locals import * from random import randint from tree import Tree from ranger import Ranger from cutter import Cutter pygame.init() global window_h, window_w, trees # Background bg_color = [255, 255, 255] screen = pygame.display.set_mode( (0,0) ) pygame.display.set_caption("Ranger") window_h, window_w = screen.get_size () if window_h > 1024: screen = pygame.display.set_mode( (800,640) ) window_h, window_w = screen.get_size () # Tree trees = [] start_time = time.time() fps = 60 frame_time = 1.0/fps # Ranger # Button sensibility when planting button_time = 30 plant_time = 30 # Speed and movement unit = 1 ranger_speed = 10 # The ranger object ranger = Ranger(window_h, window_w, ranger_speed) # Cutter cutter_time = 50 tree_cutting_time = 50 cutter_speed = 4 cutter = [] # Difficulty level cutter_difficulty = 300 # Draw background screen.fill(bg_color) # Draw everything on top of that number_trees = 80 while number_trees != 0: trees.append(Tree((randint(0,window_h), randint(0,window_w))) ) number_trees -= 1 for arbol in trees: arbol.sprout_time = 0 air = 0 score = 0 while True: # Time tracking current_time = time.time() if current_time - start_time <= frame_time: continue start_time = current_time for event in pygame.event.get(): if event.type == pygame.QUIT: exit() ranger.input(event) keyboard = pygame.key.get_pressed() if keyboard[K_ESCAPE]: exit() if ranger.plant == True: ranger.plant = False if ranger.new_plant == False: new_plant_position = ranger.position ranger.new_plant = True if ranger.new_plant == True: plant_time += 1 if plant_time > button_time: trees.append(Tree( new_plant_position ) ) plant_time = 0 ranger.new_plant = False # Update ranger position based on events ranger.update() for treecutter in cutter: treecutter.update() # Draw background screen.fill(bg_color) ranger.draw(screen) for treecutter in cutter: treecutter.draw(screen) # Ranger kills tree cutters and adds points for cortador in cutter: if ranger.collides_with(cortador): cutter.remove(cortador) score += 1 # Draw trees or grow them for arbol in trees: if arbol.sprout_time > 0: arbol.sprout_time -= 1 arbol.draw(screen) # Tree cutters kills trees for arbol in trees: for cortador in cutter: if cortador.collides_with(arbol) and arbol.air == 1: cortador.cut_tree = True arbol.draw_alert(screen) cortador.cut_time += 1 arbol.cutting_time -=1 if cortador.cut_tree == True and cortador.cut_time == 100 or arbol.cutting_time == 0: trees.remove(arbol) cortador.cut_time = 0 cortador.cut_tree = False if cutter_time >= cutter_difficulty: cutter.append( Cutter(window_h, window_w, cutter_speed, 0) ) cutter_time = 0 cutter_time += 1 ## SCORE IS DRAWN AFTER EVERYTHING ELSE # Score is equal to amount of trees for arbol in trees: air = air + arbol.air # HUD text hud_text = "Air: " + str(air) + " Score: " + str(score) font = pygame.font.Font(None, 36) text_hud = font.render( hud_text , 1, (0, 0, 0, 0)) screen.blit(text_hud, (0, 0)) # Difilcuty level based on score if score >= 0: if score <= 10: cutter_difficulty = 200 elif score <= 20: cutter_difficulty = 100 elif score <= 50: cutter_difficulty = 175 elif score <= 100: cutter_difficulty = 100 elif score <= 150: cutter_difficulty = 50 # Speed based on air if air > 0: if ranger.speed > 1.0 : ranger.speed = math.fabs(air/10) else: ranger.speed = 1 for cortador in cutter: cortador.speed = ranger.speed else: exit() # Score updates after being shown air = 0 pygame.display.update()