Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 7, 2020 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/f66384aee4f45ad68cd9a0ca8fe78bb6 to your computer and use it in GitHub Desktop.
Save codecademydev/f66384aee4f45ad68cd9a0ca8fe78bb6 to your computer and use it in GitHub Desktop.
Codecademy export
class Pokemon:
def __init__(self,name,ptype,level):
self.name = name
self.level = level
if ptype not in ["Fire", "Water", "Grass"]:
print("Invalid Type")
else:
self.type = ptype
self.max_health = self.level * 5
self.cur_health = self.level * 5
self.is_knocked_out = False
def lose_health(self,health_loss):
self.cur_health = max(0,self.cur_health - health_loss)
if self.cur_health == 0:
self.is_knocked_out = True
def regain_health(self,health_gain):
old_health = self.cur_health
self.cur_health = min(self.cur_health + health_gain, self.max_health)
health_gained = self.cur_health - old_health
print("{name} (level {level}) health gained {health_gained} from {old_health} to {new_health}.".format(name=self.name, level=self.level,health_gained=health_gained,old_health=old_health,new_health=self.cur_health))
def revive(self,health_revive):
if self.is_knocked_out == True:
self.cur_health = min(health_revive, self.max_health)
self.is_knocked_out = False
print("{name} ({name_type}, level {level}) is revived and now has {cur_health} health".format(name=self.name,name_type=self.type,level=self.level,cur_health=self.cur_health))
else:
print("Cannot revive because {name} ({name_type}, level {level}) is not knocked out".format(name=self.name,name_type=self.type,level=self.level))
def attack(self,target):
if target == self:
print("Cannot attack yourself.")
elif self.is_knocked_out == True:
print("Knocked out pokemon cannot attack.")
elif target.is_knocked_out == True:
print("{name} ({name_type}, level {level}) is already knocked out.".format(name=target.name,name_type=target.type,level=target.level))
else:
if (self.type == target.type) or (self.type == target.type) or (self.type == target.type):
damage = 0.5 * self.level
target.lose_health(damage)
print("{name} ({name_type}, level {name_level}) attacked {target} ({target_type}, level {target_level}), {damage} was done.".format(name=self.name,name_type=self.type,name_level=self.level,target=target.name,target_type=target.type,target_level=target.level,damage=damage))
if target.is_knocked_out == True:
print("{target} ({target_type}, level {target_level}) is now knocked out.".format(target=target.name,target_type=target.type,target_level=target.level))
elif (self.type == "Fire" and target.type == "Water") or (self.type == "Water" and target.type == "Grass") or (self.type == "Grass" and target.type == "Fire"):
damage = 0.5 * self.level
target.lose_health(damage)
print("{name} ({name_type}, level {name_level}) attacked {target} ({target_type}, level {target_level}), {damage} was done.".format(name=self.name,name_type=self.type,name_level=self.level,target=target.name,target_type=target.type,target_level=target.level,damage=damage))
if target.is_knocked_out == True:
print("{target} ({target_type}, level {target_level}) is now knocked out.".format(target=target.name,target_type=target.type,target_level=target.level))
elif (self.type == "Fire" and target.type == "Grass") or (self.type == "Water" and target.type == "Fire") or (self.type == "Grass" and target.type == "Water"):
damage = 2 * self.level
target.lose_health(damage)
print("{name} ({name_type}, level {name_level}) attacked {target} ({target_type}, level {target_level}), {damage} was done.".format(name=self.name,name_type=self.type,name_level=self.level,target=target.name,target_type=target.type,target_level=target.level,damage=damage))
if target.is_knocked_out == True:
print("{target} ({target_type}, level {target_level}) is now knocked out.".format(target=target.name,target_type=target.type,target_level=target.level))
class Jake(Pokemon):
def __init__(self, level = 5):
super().__init__("Jake", "Fire", level)
class Mario(Pokemon):
def __init__(self, level = 5):
super().__init__("Mario", "Water", level)
class Elf(Pokemon):
def __init__(self, level = 5):
super().__init__("Elf", "Grass", level)
class Trainer:
def __init__(self,name,potions,pokemons,active_pokemon):
self.name = name
self.potions = potions
self.pokemons = pokemons
self.active_pokemon = active_pokemon
def use_potion(self):
if self.potions > 0:
potion_strength = 5
if self.pokemons[self.active_pokemon].is_knocked_out == True:
self.pokemons[self.active_pokemon].is_knocked_out = False
print("{name} is revived.".format(name=self.pokemons[self.active_pokemon].name))
self.pokemons[self.active_pokemon].regain_health(potion_strength)
else:
self.pokemons[self.active_pokemon].regain_health(potion_strength)
self.potions -= 1
else:
print("No potions available.")
def attack(self,target):
self.pokemons[self.active_pokemon].attack(target.pokemons[target.active_pokemon])
def switch_pokemon(self,number):
old_pokemon = self.active_pokemon
if number > 6 or number < 1:
print("A trainer can only carry 6 pokemons")
elif number <= len(self.pokemons):
if self.pokemons[number - 1].is_knocked_out == True:
print("Cannot switch to a knocked out pokemon.")
elif old_pokemon == number - 1:
print("{name} ({name_type}, level {level}) is already active.".format(name=self.pokemons[old_pokemon].name,name_type=self.pokemons[old_pokemon].type,level=self.pokemons[old_pokemon].level))
else:
self.active_pokemon = number - 1
print("{trainer} switched from {old} ({old_type}, level {old_level}) to {new} ({new_type}, level {new_level}).".format(trainer=self.name,old=self.pokemons[old_pokemon].name,old_type=self.pokemons[old_pokemon].type,old_level=self.pokemons[old_pokemon].level,new=self.pokemons[self.active_pokemon].name,new_type=self.pokemons[self.active_pokemon].type,new_level=self.pokemons[self.active_pokemon].level))
else:
print("No pokemon is selected location")
test1 = Jake(10)
test2 = Mario(20)
test3 = Elf(30)
test4 = Elf(10)
test5 = Jake(20)
test6 = Mario(30)
trainA = Trainer("A",2,[test1,test2,test3],0)
trainB = Trainer("B",2,[test4,test5,test6],0)
trainA.attack(trainB)
trainA.switch_pokemon(1)
trainA.use_potion()
trainB.attack(trainA)
trainB.attack(trainA)
trainA.use_potion()
trainB.attack(trainA)
trainA.switch_pokemon(3)
trainA.attack(trainB)
trainA.attack(trainB)
trainA.attack(trainB)
trainA.attack(trainB)
trainB.attack(trainA)
trainB.switch_pokemon(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment