import pygame import time import os import sys 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 window_h = 640 window_w = 480 # Background bg_color = [255, 255, 255] screen = pygame.display.set_mode( (window_h, window_w) ) pygame.display.set_caption("Ranger") # Tree plant_time = 100 trees = [] start_time = time.time() fps = 60 frame_time = 1.0/fps # Ranger unit = 1 ranger_speed = 4 ranger = Ranger(window_h, window_w, ranger_speed) # Cutter cutter_time = 100 cutter_speed = 4 cutter = [] # 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 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 > 300: 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 for cortador in cutter: if ranger.collides_with(cortador): cutter.remove(cortador) # Tree cutters kills trees for arbol in trees: for cortador in cutter: if cortador.collides_with(arbol): trees.remove(arbol) if cutter_time == 100: cutter.append( Cutter(window_h, window_w, cutter_speed) ) cutter_time = 0 cutter_time += 1 for arbol in trees: arbol.draw(screen) ## SCORE IS DRAWN AFTER EVERYTHING ELSE # Score is equal to amount of trees for arbol in trees: score += 1 # Score text score_text = "Air: " + str(score) + " Planting in : " + str(plant_time) font = pygame.font.Font(None, 36) text = font.render( score_text , 1, (0, 0, 0, 0)) screen.blit(text, (0, 0)) # Score updates after being shown score = 0 pygame.display.update()