enemy
1import pygame.draw 2"""pygame.draw used to more easily render enemies""" 3 4 5class Enemy: 6 """Class that handles the creation of enemies""" 7 def __init__(self, e_type, health, speed, strength, path): 8 self._type = e_type 9 """The type of enemy, to be used at a later date for different kinds of enemies""" 10 self._health = health 11 self._max_health = health 12 self._speed = speed 13 self._strength = strength 14 self._path = path 15 """The path the enemy is supposed to follow""" 16 self._position = self._path[0] 17 """Initially set at the starting point on the path""" 18 self._path_index = 0 19 self._status = True 20 """Means the enemy is alive""" 21 self._resource_worth = health * 3 22 23 def _move(self): 24 """Function to move the enemy""" 25 if self._path_index < len(self._path) - 1: 26 """Checks to see if the enemy has anywhere else to go.""" 27 target_x, target_y = self._path[self._path_index] 28 """Finds the target x and y""" 29 dir_x, dir_y = target_x - self._position[0], target_y - self._position[1] 30 """Sets the direction to go""" 31 distance = (dir_x ** 2 + dir_y ** 2) ** 0.5 32 """Helps adjust distance and move based on speed stat""" 33 if distance < self._speed: 34 self._position = (target_x, target_y) 35 self._path_index += 1 36 """Move to the next point""" 37 else: 38 self._position = ( 39 self._position[0] + dir_x / distance * self._speed, 40 self._position[1] + dir_y / distance * self._speed, 41 ) 42 43 def take_damage(self, damage): 44 """Takes damage and calls kill_enemy if health hits zero""" 45 self._health = self._health - damage 46 if self.get_enemy_health() <= 0: 47 self.kill_enemy() 48 49 def kill_enemy(self): 50 """Kills the enemy and calls reward_resources""" 51 self._status = False 52 53 def reward_resources(self, base): 54 """Gives player resources based on how much the enemy is worth""" 55 base.add_money(self._resource_worth) 56 57 def damage_base(self, base): 58 """Damages the base and removes the enemy""" 59 base.remove_health(self.get_enemy_strength()) 60 self._status = False 61 62 def is_alive(self): 63 """Returns the enemy status""" 64 return self._status 65 66 def get_enemy_type(self): 67 """Returns the enemy type""" 68 return self._type 69 70 def get_enemy_health(self): 71 """Returns the enemy's health""" 72 return self._health 73 74 def get_enemy_speed(self): 75 """Returns the enemy's speed""" 76 return self._speed 77 78 def get_enemy_strength(self): 79 """Returns the enemy's strength""" 80 return self._strength 81 82 def render(self, window): 83 """Renders the enemy, currently as a circle. Plan to render as different shapes based on enemy type""" 84 pygame.draw.circle(window, (255, 0, 0), 85 (int(self._position[0]), int(self._position[1])), 10) 86 self._draw_health_bar(window) 87 88 def _draw_health_bar(self, window): 89 """Creates a visual for the player to know the status of the enemy's health""" 90 health_ratio = self._health / self._max_health 91 """Finds ratio of health""" 92 green = int(255 * health_ratio) 93 red = 255 - green 94 pygame.draw.rect(window, (red, green, 0), 95 (self._position[0] - 15, self._position[1] - 20, 30 * health_ratio, 5))
class
Enemy:
6class Enemy: 7 """Class that handles the creation of enemies""" 8 def __init__(self, e_type, health, speed, strength, path): 9 self._type = e_type 10 """The type of enemy, to be used at a later date for different kinds of enemies""" 11 self._health = health 12 self._max_health = health 13 self._speed = speed 14 self._strength = strength 15 self._path = path 16 """The path the enemy is supposed to follow""" 17 self._position = self._path[0] 18 """Initially set at the starting point on the path""" 19 self._path_index = 0 20 self._status = True 21 """Means the enemy is alive""" 22 self._resource_worth = health * 3 23 24 def _move(self): 25 """Function to move the enemy""" 26 if self._path_index < len(self._path) - 1: 27 """Checks to see if the enemy has anywhere else to go.""" 28 target_x, target_y = self._path[self._path_index] 29 """Finds the target x and y""" 30 dir_x, dir_y = target_x - self._position[0], target_y - self._position[1] 31 """Sets the direction to go""" 32 distance = (dir_x ** 2 + dir_y ** 2) ** 0.5 33 """Helps adjust distance and move based on speed stat""" 34 if distance < self._speed: 35 self._position = (target_x, target_y) 36 self._path_index += 1 37 """Move to the next point""" 38 else: 39 self._position = ( 40 self._position[0] + dir_x / distance * self._speed, 41 self._position[1] + dir_y / distance * self._speed, 42 ) 43 44 def take_damage(self, damage): 45 """Takes damage and calls kill_enemy if health hits zero""" 46 self._health = self._health - damage 47 if self.get_enemy_health() <= 0: 48 self.kill_enemy() 49 50 def kill_enemy(self): 51 """Kills the enemy and calls reward_resources""" 52 self._status = False 53 54 def reward_resources(self, base): 55 """Gives player resources based on how much the enemy is worth""" 56 base.add_money(self._resource_worth) 57 58 def damage_base(self, base): 59 """Damages the base and removes the enemy""" 60 base.remove_health(self.get_enemy_strength()) 61 self._status = False 62 63 def is_alive(self): 64 """Returns the enemy status""" 65 return self._status 66 67 def get_enemy_type(self): 68 """Returns the enemy type""" 69 return self._type 70 71 def get_enemy_health(self): 72 """Returns the enemy's health""" 73 return self._health 74 75 def get_enemy_speed(self): 76 """Returns the enemy's speed""" 77 return self._speed 78 79 def get_enemy_strength(self): 80 """Returns the enemy's strength""" 81 return self._strength 82 83 def render(self, window): 84 """Renders the enemy, currently as a circle. Plan to render as different shapes based on enemy type""" 85 pygame.draw.circle(window, (255, 0, 0), 86 (int(self._position[0]), int(self._position[1])), 10) 87 self._draw_health_bar(window) 88 89 def _draw_health_bar(self, window): 90 """Creates a visual for the player to know the status of the enemy's health""" 91 health_ratio = self._health / self._max_health 92 """Finds ratio of health""" 93 green = int(255 * health_ratio) 94 red = 255 - green 95 pygame.draw.rect(window, (red, green, 0), 96 (self._position[0] - 15, self._position[1] - 20, 30 * health_ratio, 5))
Class that handles the creation of enemies
Enemy(e_type, health, speed, strength, path)
8 def __init__(self, e_type, health, speed, strength, path): 9 self._type = e_type 10 """The type of enemy, to be used at a later date for different kinds of enemies""" 11 self._health = health 12 self._max_health = health 13 self._speed = speed 14 self._strength = strength 15 self._path = path 16 """The path the enemy is supposed to follow""" 17 self._position = self._path[0] 18 """Initially set at the starting point on the path""" 19 self._path_index = 0 20 self._status = True 21 """Means the enemy is alive""" 22 self._resource_worth = health * 3
def
take_damage(self, damage):
44 def take_damage(self, damage): 45 """Takes damage and calls kill_enemy if health hits zero""" 46 self._health = self._health - damage 47 if self.get_enemy_health() <= 0: 48 self.kill_enemy()
Takes damage and calls kill_enemy if health hits zero
def
kill_enemy(self):
50 def kill_enemy(self): 51 """Kills the enemy and calls reward_resources""" 52 self._status = False
Kills the enemy and calls reward_resources
def
reward_resources(self, base):
54 def reward_resources(self, base): 55 """Gives player resources based on how much the enemy is worth""" 56 base.add_money(self._resource_worth)
Gives player resources based on how much the enemy is worth
def
damage_base(self, base):
58 def damage_base(self, base): 59 """Damages the base and removes the enemy""" 60 base.remove_health(self.get_enemy_strength()) 61 self._status = False
Damages the base and removes the enemy
def
render(self, window):
83 def render(self, window): 84 """Renders the enemy, currently as a circle. Plan to render as different shapes based on enemy type""" 85 pygame.draw.circle(window, (255, 0, 0), 86 (int(self._position[0]), int(self._position[1])), 10) 87 self._draw_health_bar(window)
Renders the enemy, currently as a circle. Plan to render as different shapes based on enemy type