Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 21, 2020 19:03
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/4a2aca4ebe3b7fc74b2e2f44097f9eb6 to your computer and use it in GitHub Desktop.
Save codecademydev/4a2aca4ebe3b7fc74b2e2f44097f9eb6 to your computer and use it in GitHub Desktop.
Codecademy export
# elements list is expandable without changing .attack method
elements = [['Fire', [0, 0.5, 2]], ['Water', [2, 0, 0.5]], ['Grass', [0.5, 2, 0]]]
class Pokemon:
def __init__(self, name, level, element, is_knocked_out=False):
self.name = name
self.level = level
self.element = element
self.max_health = level
self.health = self.max_health
self.is_knocked_out = is_knocked_out
self.is_charmander = False
def __repr__(self):
return self.name
def knocked_out(self):
self.is_knocked_out = True
print(f'{self} is knocked_out!')
def revive(self):
self.health = self.max_health
print(f'{self.name} now has {self.health} health')
def lose_health(self, points):
health = self.health - points
self.health = max(health, 0)
print(f'{self} now has {self.health} health')
if self.health == 0:
self.knocked_out()
def gain_health(self, points):
health = self.health + points
self.health = min(health, self.max_health)
print(f'{self} now has {self.health} health')
# the moment you all've been waitig for
def attack(self, target):
for params in elements:
if params[0] == target.element:
target_index = elements.index(params)
for params in elements:
if params[0] == self.element:
attacker_power = params[1][target_index]
print(f'{self} attacks {target}!')
print(f'{self} deals {attacker_power} damage.')
target.lose_health(attacker_power)
class Trainer:
def __init__(self, name, pokemons, potions, active=0):
self.name = name
self.pokemons = pokemons[:6] if len(pokemons) > 6 else pokemons
self.potions = potions
self.active = active
self.pokemon = self.pokemons[self.active]
def __repr__(self):
print(f'{self.name} currently has the following pokemons:')
for pokemon in self.pokemons:
print(pokemon)
return f'Current pokemon: {self.pokemon}'
def charmander_call(self, target):
for pokemon in self.pokemons:
if pokemon.is_charmander:
pokemon.resurrection(target)
print(f"Your {target} is revived by {pokemon}")
return
print("Thou can't use this magic!")
def heal(self):
print(f"{self.name} heals {self.pokemon}")
if self.potions > 0:
self.pokemon.gain_health(1)
self.potions -= 1
else:
print('Out of potions!')
def attack(self, target):
if self.pokemon.is_knocked_out:
print(f"{self.pokemon} can't attack.")
return
self.pokemon.attack(target.pokemon)
def switch(self, pokemon):
if self.pokemon == pokemon:
print("Can't switch on the same pokemon.")
elif pokemon.is_knocked_out:
print("Can't switch on knockouted pokemon")
else:
self.pokemon = pokemon
print(f'{self.pokemon} is now active.')
class Charmander(Pokemon):
def __init__(self, name, level, element, is_knocked_out=False):
super().__init__(name, level, element, is_knocked_out=False)
self.is_charmander = True
def resurrection(self, target):
target.revive()
# sorry for some cyrillic stuff
andrew = Pokemon("Андрей Андреевич", 3, "Fire", False)
basil = Pokemon("Василий Семёнович", 3, "Grass", False)
nik = Pokemon("Николай Васильевич", 3, "Water", False)
alex = Charmander("Алексей Алексеевич", 3, "Fire", False)
player1 = Trainer('Карпов', [andrew, alex], 2)
player2 = Trainer('Каспаров', [basil, nik], 2)
player1.attack(player2)
player2.attack(player1)
player2.heal()
player1.attack(player2)
player2.attack(player1)
player2.switch(nik)
player2.attack(player1)
player1.heal()
player1.heal()
player1.heal()
player1.charmander_call(basil)
player2.charmander_call(basil)
print(player2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment