Skip to content

Commit

Permalink
Added Wizard Tower
Browse files Browse the repository at this point in the history
  • Loading branch information
victorknox committed Apr 13, 2022
1 parent 8cb0d83 commit 212373f
Showing 1 changed file with 62 additions and 15 deletions.
77 changes: 62 additions & 15 deletions src/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from colorama import Fore, Back, Style, init
import math

from time import *
from .characters import King

init(autoreset=True)

class Building:
Expand All @@ -17,26 +20,28 @@ def __init__(self, height, length, position, maxhealth, 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):
def attacked(self, damage, buildings):
" receives damage and updates the health accordingly "
self.health -= damage
if self.health <= 0:
self.destroy()
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):
def destroy(self, buildings):
" destroys the building and updates the board accordingly "
self.content = [['']*self.length for tile in range(self.height)]
self.x = -1
self.y = -1
try:
buildings.remove(self)
except:
pass

def update_color(self, color):
" updates the color of the building "
Expand All @@ -56,7 +61,7 @@ def __init__(self, position):
super().__init__(height, length, position, maxhealth, icon)

class Hut(Building):
""" The Hut class which inherits from the Building class. It is the building which is the main target of the game. """
""" The Hut class which inherits from the Building class. It is a normal building"""
def __init__(self, position):
height = 1
length = 1
Expand All @@ -65,7 +70,7 @@ def __init__(self, position):
super().__init__(height, length, position, maxhealth, icon)

class Wall(Building):
""" The Wall class which inherits from the Building class. It is the building which is the main target of the game. """
""" The Wall class which inherits from the Building class. It is a protective building around village"""
def __init__(self, position):
height = 1
length = 1
Expand All @@ -75,34 +80,76 @@ def __init__(self, position):
self.iswall = True

class Cannon(Building):
""" The Cannon class which inherits from the Building class. It is the building which is the main target of the game. """
""" 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
self.damage = 10
icon = 'C'
super().__init__(height, length, position, maxhealth, icon)
self.damage = 10

def attack_enemy(self, characters):
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)
character.attacked(self.damage, characters)
inrange = True
break
if not inrange:
# change canon color to original color
self.attacked(0)
self.attacked(0, buildings)
else:
# change canon color to white to indicate attack
# 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


0 comments on commit 212373f

Please sign in to comment.