Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
victorknox committed Mar 14, 2022
1 parent 0435bc0 commit aa12e38
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 19 deletions.
Binary file modified src/__pycache__/board.cpython-310.pyc
Binary file not shown.
Binary file modified src/__pycache__/building.cpython-310.pyc
Binary file not shown.
Binary file modified src/__pycache__/characters.cpython-310.pyc
Binary file not shown.
Binary file modified src/__pycache__/utils.cpython-310.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions src/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self):

def clear(self):

# Blank board with boundaries
# Initializing a Blank board with boundaries
self.content = [[' ']*self.width for tile in range(self.height)]
self.content[0] = ['═']*self.width
self.content[self.height-1] = ['═']*self.width
Expand Down Expand Up @@ -66,6 +66,6 @@ def update(self, buildings, characters):
if(isinstance(building, Cannon)):
building.attack_enemy(characters)

# barbarians' automated movement
# calling barbarians' automated movement
for character in characters[1:]:
character.automove(self, buildings)
18 changes: 15 additions & 3 deletions src/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
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
Expand All @@ -18,6 +21,7 @@ def __init__(self, height, length, position, maxhealth, icon):


def attacked(self, damage):
" receives damage and updates the health accordingly "
self.health -= damage
if self.health <= 0:
self.destroy()
Expand All @@ -29,16 +33,21 @@ def attacked(self, damage):
self.update_color(Fore.GREEN)

def destroy(self):
" destroys the building and updates the board accordingly "
self.content = [['']*self.length for tile in range(self.height)]
self.x = -1
self.y = -1

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
Expand All @@ -47,6 +56,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. """
def __init__(self, position):
height = 1
length = 1
Expand All @@ -55,6 +65,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. """
def __init__(self, position):
height = 1
length = 1
Expand All @@ -64,6 +75,7 @@ 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. """
def __init__(self, position):
height = 2
length = 2
Expand All @@ -74,7 +86,7 @@ def __init__(self, position):
super().__init__(height, length, position, maxhealth, icon)

def attack_enemy(self, characters):
# scan the range, if enemy is in range, attack
""" 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
Expand All @@ -84,10 +96,10 @@ def attack_enemy(self, characters):
inrange = True
break
if not inrange:
# change canon color
# change canon color to original color
self.attacked(0)
else:
# change canon color
# change canon color to white to indicate attack
self.update_color(Fore.WHITE)
return

Expand Down
34 changes: 31 additions & 3 deletions src/characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
size = os.get_terminal_size()

class Character:
""" The Character class which stores basic details such as health, position, etc.
also stores basic methods such and unit movement, taking damage attacking the enemy """

def __init__(self, maxhealth, damage, position, ms, icon):
""" The Character class which stores basic details such as health, position, etc."""
self.health = maxhealth
self.maxhealth = maxhealth
self.damage = damage
Expand All @@ -19,6 +23,7 @@ def __init__(self, maxhealth, damage, position, ms, icon):


def unitmove(self, ip, board):
""" Unit move for any character, checks if the unit can move in the direction it is facing and if it can, moves it. """
if ip == 'w' and board.content[self.y-1][self.x] == ' ':
self.y -= 1
elif ip == 's' and board.content[self.y+1][self.x] == ' ':
Expand All @@ -31,6 +36,7 @@ def unitmove(self, ip, board):
pass

def attack_enemy(self, buildings):
""" Attack enemy method, checks if the unit can attack the enemy and if it can, attacks it. """
for building in buildings:
if self.direction == "r":
if(self.x + 1 == building.x and self.y <= building.y + building.height - 1 and self.y >= building.y):
Expand All @@ -46,6 +52,7 @@ def attack_enemy(self, buildings):
building.attacked(self.damage)

def attacked(self, damage):
""" receives damage and updates the health accordingly """
self.health -= damage
if self.health <= 0:
self.destroy()
Expand All @@ -57,18 +64,23 @@ def attacked(self, damage):
self.update_color(Fore.BLUE)

def destroy(self):
""" removes the unit from the board """
self.content = [['' + Fore.RESET]*1 for tile in range(1)]
self.x = -1
self.y = -1


def update_color(self, color):
""" updates the color of the unit """
self.content = [[color + self.icon + Fore.RESET]*1 for tile in range(1)]



class King(Character):
""" King class, inherits from Character class """

def __init__(self):
""" initialize the king with basic details """
health = 1000
damage = 30
position = (1, 1)
Expand All @@ -77,7 +89,7 @@ def __init__(self):
super().__init__(health, damage, position, ms, icon)

def move(self, ip, board):
# setting the character direction according to the input
""" King move method, checks if the king can move in the direction it is facing and if it can, moves it. """
if ip == 'w':
self.direction = "u"
elif ip == 's':
Expand All @@ -92,6 +104,7 @@ def move(self, ip, board):
ms -= 1

def axe_attack(self, buildings):
""" Axe attack method, attacks any building withing the radius of the attack range. """
attack_range = 3
for building in buildings:
for i in range(-attack_range, attack_range):
Expand All @@ -102,15 +115,20 @@ def axe_attack(self, buildings):


class Barbarian(Character):
""" Barbarian class, inherits from Character class """

def __init__(self, position):
""" initialize the barbarian with basic details """
health = 300
damage = 1
ms = 1
icon = '¥'
super().__init__(health, damage, position, ms, icon)

def automove(self, board, buildings):
# find closest buiding
""" Barbarian automove method, checks for the closest building and moves towards it. """

# search for the closest building
closest = -1
for building in buildings:
if building.iswall == False:
Expand All @@ -119,6 +137,7 @@ def automove(self, board, buildings):
else:
if abs(self.x - building.x) + abs(self.y - building.y) < abs(self.x - closest.x) + abs(self.y - closest.y):
closest = building

# move towards closest buiding
if closest.x > self.x:
self.direction = "r"
Expand All @@ -137,20 +156,25 @@ def automove(self, board, buildings):
self.attack_enemy(buildings)

def move(self, ip, board):
""" Barbarian move method, checks if the barbarian can move in the direction it is facing and if it can, moves it. """
ms = self.ms
while(ms > 0):
self.unitmove(ip, board)
ms -= 1

class Wallbreaker(Character):
""" Wallbreaker class, inherits from Character class """

def __init__(self, position):
""" initialize the wallbreaker with basic details """
health = 100
damage = 1000
ms = 2
icon = '¤'
super().__init__(health, damage, position, ms, icon)

def bomb_attack(self, buildings):
""" Bomb attack method, attacks any building withing the radius of the attack range. """
attack_range = 3
for building in buildings:
for i in range(-attack_range, attack_range):
Expand All @@ -160,7 +184,9 @@ def bomb_attack(self, buildings):
building.attacked(self.damage)

def automove(self, board, buildings):
# find closest wall
""" Wallbreaker automove method, checks for the closest wall and moves towards it. """

# search for the closest wall
closest = -1
for building in buildings:
if building.iswall == True:
Expand All @@ -169,6 +195,7 @@ def automove(self, board, buildings):
else:
if abs(self.x - building.x) + abs(self.y - building.y) < abs(self.x - closest.x) + abs(self.y - closest.y):
closest = building

# move towards closest wall
if closest.x > self.x:
self.direction = "r"
Expand All @@ -191,6 +218,7 @@ def automove(self, board, buildings):
self.destroy()

def move(self, ip, board):
""" Wallbreaker move method, checks if the wallbreaker can move in the direction it is facing and if it can, moves it. """
ms = self.ms
while(ms > 0):
self.unitmove(ip, board)
Expand Down
3 changes: 3 additions & 0 deletions src/spells.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import math

class Spell:
""" Spell parent class """
def __init__(self, characters):
for character in characters:
self.effect(character)


class Rage(Spell):
""" Rage spell class, doubles the damage and movement speed of the character """
def __init__(self, characters):
super().__init__(characters)

Expand All @@ -15,6 +17,7 @@ def effect(self, character):
character.ms *= 2

class Heal(Spell):
""" Heal spell class, heals the character by a certain amount """
def __init__(self, characters):
super().__init__(characters)

Expand Down
Loading

0 comments on commit aa12e38

Please sign in to comment.