import pygame from random import randint tree_images_number = 3 tree_images = ["images/tree_%02d.png" % tree_images_number for tree_images_number in xrange(1, tree_images_number + 1) ] cutting_time = 100 class Tree: def __init__(self, position): self.air = 0 self.cutting_time = cutting_time self.sprout_time = 300 self.position = position self.image = pygame.image.load(tree_images[randint(0, tree_images_number - 1)]) self.alert_image = pygame.image.load("images/atention.png") self.sprout_image = pygame.image.load("images/sprout.png") image_rect = self.image.get_rect() size_reduction = 10 self.collision_rect = pygame.Rect(image_rect.left - size_reduction, \ image_rect.top - size_reduction, \ image_rect.width - size_reduction, \ image_rect.height - size_reduction) def draw(self, on_surface): if self.sprout_time <= 0: on_surface.blit(self.image, self.position) if self.air == 0: self.air = 1 else: sprout_text = str(self.sprout_time) font = pygame.font.Font(None, 24) text = font.render( sprout_text , 1, (0, 0, 0, 0)) on_surface.blit(self.sprout_image, self.position) on_surface.blit( text, self.position) def draw_alert(self, on_surface): on_surface.blit(self.alert_image, self.position) cutting_text = str(self.cutting_time) font = pygame.font.Font(None, 24) text = font.render( cutting_text , 1, (0, 0, 0, 0)) on_surface.blit( text, self.position) def collides_with(self, other): return self.get_absolute_rect().colliderect(other.get_absolute_rect()) def get_absolute_rect(self): return self.collision_rect.move(self.position)