Professional Documents
Culture Documents
Battle Game
Battle Game
class main:
def __init__(self):
self.gameName = 'BattleGame'
self.playerUnits = [
]
self.aiUnits = [
]
self.no_of_units = 3
def play_game(self):
try:
print(self.gameName + " - Started")
# Attack
round = 0
NextAttacker = "PlayerTeam" #default
cont = True
while cont==True:
if NextAttacker == "PlayerTeam":
round += 1
print("\n")
print("Round-" + str(round))
self.player_attack()
self.remove_ai_unit() #after attach, check for remove of a unit
from the target team
NextAttacker = "AITeam"
else:
print("\n")
self.ai_attack()
self.remove_player_unit() #after attach, check for remove of a
unit from the target team
NextAttacker = "PlayerTeam"
self.print_teams_current_score()
if self.get_playerteam_active_unit_count() == 0:
print("\n")
print("The winner of the battle game is AI-Team")
print("Game Over")
cont = False
elif self.get_aiteam_active_unit_count() == 0:
print("\n")
print("The winner of the battle game is Player-Team")
print("Game Over")
cont = False
def get_aiteam_active_unit_count(self):
active_units = 0
try:
for unit in self.aiUnits:
if str(unit.status).lower() == "active":
active_units +=1
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return active_units
for i in range(self.no_of_units):
unitClass = unit()
if teamName == "PlayerTeam":
unitClass.name = input("Enter the name of the unit-" + str(i+1)
+ " of the PlayerTeam")
else:
random_number = random.randint(1,99)
unitClass.name = "AI" + str(random_number).zfill(2)
unitClass.teamName = teamName
unitClass.profession = self.get_profession(teamName)
if unitClass.profession == "Warrior":
unitClass.healthPoint = 25 #100
unitClass.attackPoint = random.randint(5,20)
unitClass.defencePoint = random.randint(1,10)
unitClass.experience = 0
unitClass.rank = 1
else:
unitClass.healthPoint = 25 #100
unitClass.attackPoint = random.randint(1,10)
unitClass.defencePoint = random.randint(5,15)
unitClass.experience = 0
unitClass.rank = 1
if str(target_damage_severity).lower() == "severe":
damage = (attacker_ATK - target_DEF) + random.randint(1, 10)
target_HP -= damage
print("\n")
print("Damage for AITeam member:" + str(damage))
attacker_EXP += damage
target_EXP += target_DEF
#extra experience
if damage>10:
target_EXP += (target_DEF * 20)/100
elif damage<0:
target_EXP += (target_DEF * 50) / 100
#level up
if attacker_EXP >= 100:
attacker_Rank +=1
attacker_EXP -= 100
attacker_ATK = attacker_unit.attackPoint
attacker_EXP = attacker_unit.experience
attacker_Rank = attacker_unit.rank
target_DEF = target_unit.defencePoint
target_HP = target_unit.healthPoint
target_EXP = target_unit.experience
target_Rank = target_unit.rank
attacker_EXP += damage
target_EXP += target_DEF
#extra experience
if damage>10:
target_EXP += (target_DEF * 20)/100
elif damage<0:
target_EXP += (target_DEF * 50) / 100
#level up
if attacker_EXP >= 100:
attacker_Rank +=1
attacker_EXP -= 100
if len(tmp_playerUnits) > 0:
random_number = random.randint(1, len(tmp_playerUnits))
unit = self.playerUnits[random_number - 1]
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return unit
tmp_aiUnits = []
for unit in self.aiUnits:
if str(unit.status).lower() == "active":
tmp_aiUnits.append(unit)
if len(tmp_aiUnits)>0:
random_number = random.randint(1, len(tmp_aiUnits))
unit = self.aiUnits[random_number-1]
return unit
def get_specific_ai_unit(self, playerName):
unitValue = {}
try:
for unit in self.aiUnits:
if unit.status.lower() == "active" and unit.name.lower() ==
playerName.lower():
unitValue = unit
break
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return unitValue
def remove_ai_unit(self):
try:
for unit in self.aiUnits:
if unit.healthPoint <= 0 and str(unit.status) == "active":
unit.status = "dead"
print("AIUnit - " + unit.name + " has been removed due to lower
HealthPoint")
break
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
def get_profession(self, teamName):
profession = ""
try:
if teamName == "PlayerTeam":
total_units = len(self.playerUnits)
if total_units == 0:
profession = "Warrior"
else:
lastest_unit = self.playerUnits[total_units-1]
if lastest_unit.profession == "Warrior":
profession = "Tanker"
else:
profession = "Warrior"
else:
total_units = len(self.aiUnits)
if total_units == 0:
profession = "Warrior"
else:
lastest_unit = self.aiUnits[total_units-1]
if lastest_unit.profession == "Warrior":
profession = "Tanker"
else:
profession = "Warrior"
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return profession
def get_playerteam_players(self):
playerteam_unit_names = ""
try:
for unit in self.playerUnits:
if str(unit.status).lower() == "active":
playerteam_unit_names += unit.name + " | "
playerteam_unit_names =
playerteam_unit_names[:len(playerteam_unit_names)-2]
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return playerteam_unit_names.strip()
def get_aiteam_players(self):
aiteam_unit_names = ""
try:
for unit in self.aiUnits:
if str(unit.status).lower() == "active":
aiteam_unit_names += unit.name + " | "
aiteam_unit_names = aiteam_unit_names[:len(aiteam_unit_names)-2]
except Exception as ex:
#print(colored(str(ex), "red"))
print(str(ex))
finally:
return aiteam_unit_names.strip()