class GameController: def __init__(self, game): self.game = game self.camera = Camera() self.environment = Environment(self.game) self.player = Player(10) self.enemy_controller = EnemyController(self.player, self.environment.trees, self.camera) self.camera.object = self.player self.health_bar = HealthBar() self.background = pygame.image.load('sprites/background.png') def render(self, surface): surface.blit(self.background, (0, 0)) self.environment.render(surface, self.camera.x) self.player.render(surface, self.camera.x) self.enemy_controller.render(surface, self.camera.x) self.health_bar.render(surface, 10, 10) def update(self, dt, event): self.environment.update(dt, self.player.timer.time) self.player.update(dt, event) self.enemy_controller.update(dt, event) self.camera.update(dt, event) self.health_bar.health = self.environment.tree_health
class Tree: def __init__(self, x): self.x = x self.image = pygame.image.load('sprites/tree.png') self.sprite = self.image self.width, self.height = self.sprite.get_size() self.y = 696 - self.height self.destroy = 0 self.health_bar = HealthBar() self.health = 10 self.total_health = 10 def render(self, surface, camera_x): surface.blit(self.sprite, (self.x - camera_x, self.y)) self.health_bar.render(surface, self.x - camera_x + 25, self.y - 30, self.width - 50) def update(self, dt): self.health_bar.health = self.health / self.total_health
class Enemy: def __init__(self, x, trees): self.trees = trees self.sprite = pygame.image.load("sprites/enemy.png") self.width, self.height = self.sprite.get_size() self.y = 696 - self.height self.x = x self.speed = 200 self.search_for_tree() self.health_bar = HealthBar() self.health = 10 self.total_health = 10 self.rect_head = pygame.Rect(self.x + 32, self.y, 36, self.height) self.rect_body = pygame.Rect(self.x, self.y + 51, self.width, self.height) self.sad = False self.chainsaw = Chainsaw() def search_for_tree(self): self.nearest_tree = None if self.trees: for tree in self.trees: if self.nearest_tree == None: self.nearest_tree = tree else: if abs(tree.x - self.x) < abs(self.nearest_tree.x - self.x): self.nearest_tree = tree else: return None def make_sad(self): self.health = 0 self.sad = True self.sprite = pygame.image.load('sprites/enemy_sad.png') self.health_bar.health = 0 def render(self, surface, camera_x): surface.blit(self.sprite, (self.x - camera_x, self.y)) self.health_bar.render(surface, self.x - camera_x, self.y - 30, self.width) self.chainsaw.render(surface, self.x - camera_x + 92, self.y + 130) def update(self, dt, event): if self.sad: self.x += 350 * dt return if self.nearest_tree != None: if self.nearest_tree.x < self.x: self.x -= self.speed * dt elif self.nearest_tree.x > self.x: self.x += self.speed * dt if abs(self.x - self.nearest_tree.x) < self.speed * dt: self.x = self.nearest_tree.x self.nearest_tree.health -= 2 * dt if self.nearest_tree.health < 0: self.nearest_tree = None else: self.search_for_tree() self.health_bar.health = self.health / self.total_health self.rect_head = pygame.Rect(self.x + 32, self.y, 36, self.height) self.rect_body = pygame.Rect(self.x, self.y + 51, self.width, self.height)