-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilding.py
155 lines (136 loc) · 5.69 KB
/
building.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
from colorama import Fore, Back, Style, init
import math
from time import *
from .characters import King
init(autoreset=True)
class Building:
""" The Building parent class which stores basic details such as building size, health, position, etc.
It also contains basic methods such as recieving damage, self destruction and updating the color of the building as the health drops. """
def __init__(self, height, length, position, maxhealth, icon):
self.health = maxhealth
self.maxhealth = maxhealth
self.height = height
self.length = length
self.icon = icon
self.x = position[0]
self.y = position[1]
self.iswall = False
self.damage = 0
self.content = [[Fore.GREEN + icon + Fore.RESET]*self.length for tile in range(self.height)]
def attacked(self, damage, buildings):
" receives damage and updates the health accordingly "
self.health -= damage
if self.health <= 0:
self.destroy(buildings)
elif self.health < self.maxhealth/5:
self.update_color(Fore.RED)
elif self.health < self.maxhealth/2:
self.update_color(Fore.YELLOW)
else:
self.update_color(Fore.GREEN)
def destroy(self, buildings):
" destroys the building and updates the board accordingly "
try:
buildings.remove(self)
except:
pass
def update_color(self, color):
" updates the color of the building "
for row in range(self.height):
for col in range(self.length):
self.content[row][col] = color + self.icon + Fore.RESET
class TownHall(Building):
""" The TownHall class which inherits from the Building class. It is the building which is the main target of the game. """
def __init__(self, position, maxhealth):
Building.__init__(self, 3, 3, position, maxhealth, 'T')
def __init__(self, position):
height = 4
length = 3
maxhealth = 500
icon = 'T'
super().__init__(height, length, position, maxhealth, icon)
class Hut(Building):
""" The Hut class which inherits from the Building class. It is a normal building"""
def __init__(self, position):
height = 1
length = 1
maxhealth = 100
icon = 'H'
super().__init__(height, length, position, maxhealth, icon)
class Wall(Building):
""" The Wall class which inherits from the Building class. It is a protective building around village"""
def __init__(self, position):
height = 1
length = 1
maxhealth = 50
icon = '#'
super().__init__(height, length, position, maxhealth, icon)
self.iswall = True
class Cannon(Building):
""" The Cannon class which inherits from the Building class. This is a defensive building which can attack the enemy """
def __init__(self, position):
height = 2
length = 2
maxhealth = 200
self.attack_range = 6
icon = 'C'
super().__init__(height, length, position, maxhealth, icon)
self.damage = 10
def attack_enemy(self, buildings, characters):
""" This method is used to attack the enemy. It checks if the enemy is in the attack range and if so, it deals damage to the enemy. """
x = self.x
y = self.y
inrange = False
for character in characters:
if character.aerial:
continue
if character.x <= x + self.attack_range and character.x >= x - self.attack_range and character.y <= y + self.attack_range and character.y >= y - self.attack_range:
character.attacked(self.damage, characters)
inrange = True
break
if not inrange:
# change canon color to original color
self.attacked(0, buildings)
else:
# change canon color to white to indicate that it is attacking
self.update_color(Fore.WHITE)
return
class WizardTower(Building):
""" The WizardTower class which inherits from the Building class. This is a defensive building which can attack the enemy, including areal ones """
def __init__(self, position):
height = 2
length = 2
maxhealth = 200
self.attack_range = 6
icon = 'W'
super().__init__(height, length, position, maxhealth, icon)
self.damage = 10
self.aoe = 1
# finds distance between tower and character
def distance_character(self, x,y,character):
dist = max(abs(character.x - x), abs(character.y - y))
return dist
def attack_enemy(self, buildings, characters):
""" This method is used to attack the enemy. It checks if the enemy is in the attack range and if so, it deals damage to the enemy. This attack deals AOE damage to all characters in the area. """
x = self.x
y = self.y
a_x = x
a_y = y
inrange = False
for character in characters:
if character.x <= x + self.attack_range and character.x >= x - self.attack_range and character.y <= y + self.attack_range and character.y >= y - self.attack_range:
a_x = character.x
a_y = character.y
inrange = True
break
if not inrange:
# change tower color to original color
self.attacked(0, buildings)
else:
# change tower color to white to indicate attack
self.update_color(Fore.WHITE)
for character in characters:
if self.distance_character(a_x, a_y, character) <= self.aoe:
character.attacked(self.damage, characters)
return