From b99dbb396c313f4b3130b566c0df42c10eec6084 Mon Sep 17 00:00:00 2001 From: davidovski Date: Thu, 5 Jan 2023 11:35:28 +0000 Subject: Initial Commit --- enemy.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 enemy.py (limited to 'enemy.py') diff --git a/enemy.py b/enemy.py new file mode 100644 index 0000000..d19ad82 --- /dev/null +++ b/enemy.py @@ -0,0 +1,63 @@ +from dataclasses import dataclass + +from game import Game +from shooter import Shooter, ShooterAttributes + + +@dataclass +class EnemyAttributes(ShooterAttributes): + """Attributes of an enemy object""" + + reward: int = 100 + lazer_color: str = "red" + cooldown: int = 20 + + +class Enemy(Shooter): + """An enemy in the game""" + + def __init__(self, game: Game, image_name: str, + attributes: EnemyAttributes): + """Initialise the enemy + + :param game: The game which this belongs to + :type game: Game + :param image_name: The name of the image to use + :type image_name: str + :param attributes: The attributes of this + :type attributes: EnemyAttributes + """ + super().__init__(game, image_name, attributes) + self.attributes = attributes + + def tick(self, player): + """Check for collisions and shoot + + :param player: The player which to check collisions with + """ + super().tick() + if self.attributes.cooldown != -1: + self.shoot() + + lazer_collisions = self.collide_all(player.lazers) + if lazer_collisions != -1: + self.damage() + player.lazers[lazer_collisions].destroy() + + player_collisions = player.collide_all(self.lazers) + if player_collisions != -1: + player.damage() + self.lazers[player_collisions].destroy() + + if self.collides(player): + player.damage() + self.damage() + + def damage(self, amount=1): + """Reduce the object's health + + :param amount: + """ + super().damage(amount) + if self.destroyed: + self.game.score += self.attributes.reward -- cgit v1.2.3