tower
1import pygame 2import os 3""" 4Tower Class for Defense Tower Game 5 6This file defines a Tower class used in a tower defense game. Each Tower object has attributes 7such as damage, attack range, and cooldown, and methods for attacking enemies, upgrading, 8and displaying itself on the screen. 9 10Attributes: 11 - name (str): Name of the tower. 12 - damage (int): Damage dealt per attack. 13 - shot_cooldown (int): Time (in frames) between attacks. 14 - price (int): Cost to build the tower. 15 - attack_range (int): Attack range for hitting enemies. 16 - attack_pattern (int): Defines the tower's specific attack style. 17 - position (tuple): Position of the tower on the screen. 18 - upgrade_level (int): Current level of the tower. 19 - enemies_defeated (int): Count of enemies defeated by this tower. 20""" 21 22 23class Tower: 24 def __init__(self, name: str, damage: int, shot_cooldown: int, price: int, attack_range: int, attack_pattern: int): 25 """Initializes the Tower with its primary attributes.""" 26 self._name = name 27 self._damage = damage 28 self._shot_cooldown = shot_cooldown * 60 29 self._price = price 30 self._sell_price = price * 0.25 31 self._attack_range = attack_range 32 self._attack_pattern = attack_pattern 33 self._position = None 34 self._upgrade_level = 1 35 self._upgrade_cost = price + (price * 0.5) 36 self._enemies_defeated = 0 37 self._cooldown_counter = 0 38 self._image = pygame.image.load(os.path.join('game_assests', "tower.png")) 39 self.size = 28 40 41 def render(self, window): 42 """Draws the tower image on the game window at its position.""" 43 if self._position: 44 adjusted_x = self._position[0] - (self.size * 3) // 2 45 adjusted_y = self._position[1] - (self.size * 3) // 2 46 tower_surface = pygame.transform.scale(self._image, (self.size * 3, self.size * 3)) 47 window.blit(tower_surface, (adjusted_x, adjusted_y)) 48 49 def _render_range(self, window): 50 """Displays a circle around the tower to indicate its attack range.""" 51 range_surface = pygame.Surface((self._attack_range * 2, self._attack_range * 2), pygame.SRCALPHA) 52 range_surface.fill((0, 0, 0, 0)) 53 pygame.draw.circle( 54 range_surface, 55 (128, 128, 128, 100), 56 (self._attack_range, self._attack_range), 57 self._attack_range 58 ) 59 60 adjusted_x = self._position[0] - self._attack_range 61 adjusted_y = self._position[1] - self._attack_range 62 window.blit(range_surface, (adjusted_x, adjusted_y)) 63 64 def place(self, position): 65 """Sets the tower's position on the game map.""" 66 self._position = position 67 68 def get_name(self): 69 """Returns the name of the tower.""" 70 return self._name 71 72 def set_name(self, name): 73 """Sets the name of the tower, raising an error if the name is empty.""" 74 if name == "": 75 raise ValueError("Not a Valid Name") 76 else: 77 self._name = name 78 79 def get_damage(self): 80 """Returns the current damage of the tower.""" 81 return self._damage 82 83 def set_damage(self, damage): 84 """Sets the tower's damage, ensuring it is positive.""" 85 if damage > 0: 86 self._damage = damage 87 else: 88 raise ValueError("Damage must be greater than 0.") 89 90 def get_range(self): 91 """Returns the tower's attack range.""" 92 return self._attack_range 93 94 def set_attack_range(self, attack_range): 95 """Sets the tower's attack range, ensuring it is positive.""" 96 if attack_range > 0: 97 self._attack_range = attack_range 98 else: 99 raise ValueError("Range must be greater than 0.") 100 101 def get_price(self): 102 """Returns the initial cost of the tower.""" 103 return self._price 104 105 def get_sell_price(self): 106 """Returns the sell price of the tower.""" 107 return self._sell_price 108 109 def get_upgrade_cost(self): 110 """Returns the cost to upgrade the tower to the next level.""" 111 return self._upgrade_cost 112 113 def get_enemies_defeated(self): 114 """Returns the count of enemies defeated by this tower.""" 115 return self._enemies_defeated 116 117 def upgrade_tower(self): 118 """Upgrades the tower's attributes, increasing damage, range, and upgrade cost.""" 119 self._upgrade_level += 1 120 self._damage += int(self._damage * 0.5) 121 self._attack_range += 1 122 self._upgrade_cost = int(self._price * (1 + 0.5 * self._upgrade_level)) 123 124 def sell_tower(self): 125 """Displays a message indicating the tower has been sold.""" 126 print(f"{self._name} tower sold for {self._sell_price} credits.") 127 128 def attack(self, enemies): 129 """Attacks the first enemy within range if the tower is not on cooldown.""" 130 if self._cooldown_counter > 0: 131 self._cooldown_counter -= 1 132 return 133 for enemy in enemies: 134 if self._in_range(enemy): 135 enemy.take_damage(self._damage) 136 self._cooldown_counter = self._shot_cooldown 137 self._enemies_defeated += 1 138 break 139 140 def _in_range(self, enemy): 141 """Calculates if the enemy is within the tower's attack range.""" 142 ex, ey = enemy._position 143 tx, ty = self._position 144 distance = ((tx - ex) ** 2 + (ty - ey) ** 2) ** 0.5 145 return distance <= self._attack_range 146 147 def get_stats(self): 148 """Returns a dictionary of the tower's statistics.""" 149 return { 150 "Name": self._name, 151 "Damage": self._damage, 152 "Range": self._attack_range, 153 "Cooldown": self._shot_cooldown, 154 "Enemies Defeated": self._enemies_defeated, 155 "Sell Tower": self._sell_price, 156 "Upgrade Cost": self._upgrade_cost 157 } 158 159# Example 160tower = Tower("Archer Tower", 50, 3, 100, 5, 1)
class
Tower:
24class Tower: 25 def __init__(self, name: str, damage: int, shot_cooldown: int, price: int, attack_range: int, attack_pattern: int): 26 """Initializes the Tower with its primary attributes.""" 27 self._name = name 28 self._damage = damage 29 self._shot_cooldown = shot_cooldown * 60 30 self._price = price 31 self._sell_price = price * 0.25 32 self._attack_range = attack_range 33 self._attack_pattern = attack_pattern 34 self._position = None 35 self._upgrade_level = 1 36 self._upgrade_cost = price + (price * 0.5) 37 self._enemies_defeated = 0 38 self._cooldown_counter = 0 39 self._image = pygame.image.load(os.path.join('game_assests', "tower.png")) 40 self.size = 28 41 42 def render(self, window): 43 """Draws the tower image on the game window at its position.""" 44 if self._position: 45 adjusted_x = self._position[0] - (self.size * 3) // 2 46 adjusted_y = self._position[1] - (self.size * 3) // 2 47 tower_surface = pygame.transform.scale(self._image, (self.size * 3, self.size * 3)) 48 window.blit(tower_surface, (adjusted_x, adjusted_y)) 49 50 def _render_range(self, window): 51 """Displays a circle around the tower to indicate its attack range.""" 52 range_surface = pygame.Surface((self._attack_range * 2, self._attack_range * 2), pygame.SRCALPHA) 53 range_surface.fill((0, 0, 0, 0)) 54 pygame.draw.circle( 55 range_surface, 56 (128, 128, 128, 100), 57 (self._attack_range, self._attack_range), 58 self._attack_range 59 ) 60 61 adjusted_x = self._position[0] - self._attack_range 62 adjusted_y = self._position[1] - self._attack_range 63 window.blit(range_surface, (adjusted_x, adjusted_y)) 64 65 def place(self, position): 66 """Sets the tower's position on the game map.""" 67 self._position = position 68 69 def get_name(self): 70 """Returns the name of the tower.""" 71 return self._name 72 73 def set_name(self, name): 74 """Sets the name of the tower, raising an error if the name is empty.""" 75 if name == "": 76 raise ValueError("Not a Valid Name") 77 else: 78 self._name = name 79 80 def get_damage(self): 81 """Returns the current damage of the tower.""" 82 return self._damage 83 84 def set_damage(self, damage): 85 """Sets the tower's damage, ensuring it is positive.""" 86 if damage > 0: 87 self._damage = damage 88 else: 89 raise ValueError("Damage must be greater than 0.") 90 91 def get_range(self): 92 """Returns the tower's attack range.""" 93 return self._attack_range 94 95 def set_attack_range(self, attack_range): 96 """Sets the tower's attack range, ensuring it is positive.""" 97 if attack_range > 0: 98 self._attack_range = attack_range 99 else: 100 raise ValueError("Range must be greater than 0.") 101 102 def get_price(self): 103 """Returns the initial cost of the tower.""" 104 return self._price 105 106 def get_sell_price(self): 107 """Returns the sell price of the tower.""" 108 return self._sell_price 109 110 def get_upgrade_cost(self): 111 """Returns the cost to upgrade the tower to the next level.""" 112 return self._upgrade_cost 113 114 def get_enemies_defeated(self): 115 """Returns the count of enemies defeated by this tower.""" 116 return self._enemies_defeated 117 118 def upgrade_tower(self): 119 """Upgrades the tower's attributes, increasing damage, range, and upgrade cost.""" 120 self._upgrade_level += 1 121 self._damage += int(self._damage * 0.5) 122 self._attack_range += 1 123 self._upgrade_cost = int(self._price * (1 + 0.5 * self._upgrade_level)) 124 125 def sell_tower(self): 126 """Displays a message indicating the tower has been sold.""" 127 print(f"{self._name} tower sold for {self._sell_price} credits.") 128 129 def attack(self, enemies): 130 """Attacks the first enemy within range if the tower is not on cooldown.""" 131 if self._cooldown_counter > 0: 132 self._cooldown_counter -= 1 133 return 134 for enemy in enemies: 135 if self._in_range(enemy): 136 enemy.take_damage(self._damage) 137 self._cooldown_counter = self._shot_cooldown 138 self._enemies_defeated += 1 139 break 140 141 def _in_range(self, enemy): 142 """Calculates if the enemy is within the tower's attack range.""" 143 ex, ey = enemy._position 144 tx, ty = self._position 145 distance = ((tx - ex) ** 2 + (ty - ey) ** 2) ** 0.5 146 return distance <= self._attack_range 147 148 def get_stats(self): 149 """Returns a dictionary of the tower's statistics.""" 150 return { 151 "Name": self._name, 152 "Damage": self._damage, 153 "Range": self._attack_range, 154 "Cooldown": self._shot_cooldown, 155 "Enemies Defeated": self._enemies_defeated, 156 "Sell Tower": self._sell_price, 157 "Upgrade Cost": self._upgrade_cost 158 }
Tower( name: str, damage: int, shot_cooldown: int, price: int, attack_range: int, attack_pattern: int)
25 def __init__(self, name: str, damage: int, shot_cooldown: int, price: int, attack_range: int, attack_pattern: int): 26 """Initializes the Tower with its primary attributes.""" 27 self._name = name 28 self._damage = damage 29 self._shot_cooldown = shot_cooldown * 60 30 self._price = price 31 self._sell_price = price * 0.25 32 self._attack_range = attack_range 33 self._attack_pattern = attack_pattern 34 self._position = None 35 self._upgrade_level = 1 36 self._upgrade_cost = price + (price * 0.5) 37 self._enemies_defeated = 0 38 self._cooldown_counter = 0 39 self._image = pygame.image.load(os.path.join('game_assests', "tower.png")) 40 self.size = 28
Initializes the Tower with its primary attributes.
def
render(self, window):
42 def render(self, window): 43 """Draws the tower image on the game window at its position.""" 44 if self._position: 45 adjusted_x = self._position[0] - (self.size * 3) // 2 46 adjusted_y = self._position[1] - (self.size * 3) // 2 47 tower_surface = pygame.transform.scale(self._image, (self.size * 3, self.size * 3)) 48 window.blit(tower_surface, (adjusted_x, adjusted_y))
Draws the tower image on the game window at its position.
def
place(self, position):
65 def place(self, position): 66 """Sets the tower's position on the game map.""" 67 self._position = position
Sets the tower's position on the game map.
def
set_name(self, name):
73 def set_name(self, name): 74 """Sets the name of the tower, raising an error if the name is empty.""" 75 if name == "": 76 raise ValueError("Not a Valid Name") 77 else: 78 self._name = name
Sets the name of the tower, raising an error if the name is empty.
def
set_damage(self, damage):
84 def set_damage(self, damage): 85 """Sets the tower's damage, ensuring it is positive.""" 86 if damage > 0: 87 self._damage = damage 88 else: 89 raise ValueError("Damage must be greater than 0.")
Sets the tower's damage, ensuring it is positive.
def
set_attack_range(self, attack_range):
95 def set_attack_range(self, attack_range): 96 """Sets the tower's attack range, ensuring it is positive.""" 97 if attack_range > 0: 98 self._attack_range = attack_range 99 else: 100 raise ValueError("Range must be greater than 0.")
Sets the tower's attack range, ensuring it is positive.
def
get_sell_price(self):
106 def get_sell_price(self): 107 """Returns the sell price of the tower.""" 108 return self._sell_price
Returns the sell price of the tower.
def
get_upgrade_cost(self):
110 def get_upgrade_cost(self): 111 """Returns the cost to upgrade the tower to the next level.""" 112 return self._upgrade_cost
Returns the cost to upgrade the tower to the next level.
def
get_enemies_defeated(self):
114 def get_enemies_defeated(self): 115 """Returns the count of enemies defeated by this tower.""" 116 return self._enemies_defeated
Returns the count of enemies defeated by this tower.
def
upgrade_tower(self):
118 def upgrade_tower(self): 119 """Upgrades the tower's attributes, increasing damage, range, and upgrade cost.""" 120 self._upgrade_level += 1 121 self._damage += int(self._damage * 0.5) 122 self._attack_range += 1 123 self._upgrade_cost = int(self._price * (1 + 0.5 * self._upgrade_level))
Upgrades the tower's attributes, increasing damage, range, and upgrade cost.
def
sell_tower(self):
125 def sell_tower(self): 126 """Displays a message indicating the tower has been sold.""" 127 print(f"{self._name} tower sold for {self._sell_price} credits.")
Displays a message indicating the tower has been sold.
def
attack(self, enemies):
129 def attack(self, enemies): 130 """Attacks the first enemy within range if the tower is not on cooldown.""" 131 if self._cooldown_counter > 0: 132 self._cooldown_counter -= 1 133 return 134 for enemy in enemies: 135 if self._in_range(enemy): 136 enemy.take_damage(self._damage) 137 self._cooldown_counter = self._shot_cooldown 138 self._enemies_defeated += 1 139 break
Attacks the first enemy within range if the tower is not on cooldown.
def
get_stats(self):
148 def get_stats(self): 149 """Returns a dictionary of the tower's statistics.""" 150 return { 151 "Name": self._name, 152 "Damage": self._damage, 153 "Range": self._attack_range, 154 "Cooldown": self._shot_cooldown, 155 "Enemies Defeated": self._enemies_defeated, 156 "Sell Tower": self._sell_price, 157 "Upgrade Cost": self._upgrade_cost 158 }
Returns a dictionary of the tower's statistics.
tower =
<Tower object>