Created
May 14, 2020 19:47
-
-
Save caseygobrien/e7e5eea609f2ee15327b24875e936757 to your computer and use it in GitHub Desktop.
Pokemon Master Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Pokemon: | |
def __init__(self, name, level, max_health, current_health, *elements, is_knocked_out=False): | |
self.name = name | |
self.level = level | |
self.elements = elements | |
self.max_health = max_health | |
self.current_health = current_health | |
self.is_knocked_out = is_knocked_out | |
def __repr__(self): | |
element1_dict = self.elements[0] | |
element1 = element1_dict['name'] | |
if len(self.elements) > 1: | |
element2_dict = self.elements[1] | |
element2 = element2_dict['name'] | |
string = "{} is a level {} {} & {} Pokemon with {} health.".format(self.name, self.level, | |
element1, element2, self.current_health) | |
else: | |
string = "{} is a level {} {} Pokemon with {} health.".format(self.name, self.level, | |
element1, self.current_health) | |
return string | |
def lose_health(self, hit): | |
self.current_health -= hit | |
print("{} loses {} health".format(self.name, hit)) | |
if self.current_health <= 0: | |
self.current_health = 0 | |
self.knockout() | |
else: | |
print("{}'s current health is {}".format(self.name, self.current_health)) | |
def gain_health(self, heal): | |
if self.is_knocked_out is True: | |
print("{} is knocked out and must be revived first.".format(self.name)) | |
return | |
elif self.current_health + heal <= self.max_health: | |
self.current_health += heal | |
else: | |
heal = self.max_health - self.current_health | |
self.current_health = self.max_health | |
print("{} has gained {} health".format(self.name, heal)) | |
print("{}'s current health is {}".format(self.name, self.current_health)) | |
def knockout(self): | |
print("{} has been knocked out!".format(self.name)) | |
self.is_knocked_out = True | |
def revive(self, target, heal): | |
target.is_knocked_out = False | |
print("{} has been revived".format(target.name)) | |
target.gain_health(heal) | |
def attack(self, target, kind, hit): | |
attack_type = kind["name"] | |
if attack_type == self.name: | |
hit = hit * 1.5 | |
print("{} attacks {} for {} {} damage.".format(self.name, target.name, hit, attack_type)) | |
for element in target.elements: | |
if element["name"] in kind.keys(): | |
multiplier = kind[element["name"]] | |
hit = hit * multiplier | |
target.lose_health(hit) | |
normal = {"name": "normal", "rock": 0.5, "ghost": 0, "steel": 0.5} | |
fire = {"name": "fire", "fire": 0.5, "water": 0.5, "grass": 2, "ice": 2, "bug": 2, | |
"rock": 0.5, "dragon": 0.5, "steel": 2} | |
water = {"name": "water", "fire": 2, "water": 0.5, "grass": 0.5, "ground": 2, "rock": 2, "dragon": 0.5} | |
electric = {"name": "electric", "water": 2, "electric": 0.5, "grass": 0.5, "ground": 0, "flying": 2, "dragon": 0.5} | |
grass = {"name": "grass", "fire": 0.5, "water": 2, "grass": 0.5, "poison": 0.5, "ground": 2, "flying": 0.5, | |
"bug": 0.5, "rock": 2, "dragon": 0.5} | |
ice = {"name": "ice", "fire": 0.5, "water": 0.5, "grass": 2, "ice": 0.5, "ground": 2, | |
"flying": 2, "dragon": 2, "steel": 0.5} | |
fighting = {"name": "fighting", "normal": 2, "ice": 2, "poison": 0.5, "flying": 0.5, "psychic": 0.5, "bug": 0.5, | |
"rock": 2, "ghost": 0, "dark": 2, "steel": 2, "fairy": 0.5} | |
poison = {"name": "poison", "grass": 2, "poison": 0.5, "ground": 0.5, "rock": 0.5, "ghost": 0.5, "steel": 0, | |
"fairy": 2} | |
ground = {"name": "ground", "fire": 2, "electric": 2, "grass": 0.5, "poison": 2, "flying": 0, "bug": 0.5, | |
"rock": 2, "steel": 2} | |
flying = {"name": "flying", "electric": 0.5, "grass": 2, "fighting": 2, "bug": 2, "rock": 0.5, "steel": 2} | |
psychic = {"name": "psychic", "fighting": 2, "poison": 2, "psychic": 0.5, "dark": 0, "steel": 0.5} | |
bug = {"name": "bug", "fire": 0.5, "ice": 2, "fighting": 0.5, "poison": 0.5, "flying": 0.5, "psychic": 2, | |
"ghost": 0.5, "dark": 2, "steel": 0.5, "fairy": 0.5} | |
rock = {"name": "rock", "fire": 2, "ice": 2, "fighting": 0.5, "ground": 0.5, "flying": 2, "bug": 2, "steel": 0.5} | |
ghost = {"name": "ghost", "normal": 0, "psychic": 2, "ghost": 2, "dark": 0.5} | |
dragon = {"name": "dragon", "dragon": 2, "steel": 0.5, "fairy": 0} | |
dark = {"name": "dark", "fighting": 0.5, "psychic": 2, "ghost": 2, "dragon": 0.5, "fairy": 0.5} | |
steel = {"name": "steel", "fire": 0.5, "water": 0.5, "grass": 0.5, "ice": 2, "rock": 2, "steel": 0.5, "fariy": 2} | |
fairy = {"name": "fariy", "fire": 0.5, "fighting": 2, "ground": 0.5, "ghost": 2, "dark": 2, "steel": 0.5} | |
venusaur = Pokemon("Venusaur", 1, 80, 80, grass, poison) | |
charizard = Pokemon("Charizard", 1, 78, 78, fire, dragon) | |
blastoise = Pokemon("Blastoise", 1, 79, 79, water) | |
beedrill = Pokemon("Beedrill", 1, 65, 65, bug, poison) | |
pidgeot = Pokemon("Pidgeot", 1, 83, 83, normal, flying) | |
alakazam = Pokemon("Alakazam", 1, 55, 55, psychic) | |
slowbro = Pokemon("Slowbro", 1, 95, 95, water, psychic) | |
gengar = Pokemon("Gengar", 1, 60, 60, ghost, poison) | |
kangaskhan = Pokemon("Kangaskhan", 1, 105, 105, normal) | |
pinsir = Pokemon("Pinsir", 1, 65, 65, bug, flying) | |
all_pokemon = [venusaur, charizard, blastoise, beedrill, pidgeot, alakazam, slowbro, gengar, kangaskhan, pinsir] | |
all_elements = [normal, fire, water, electric, grass, ice, fighting, poison, ground, flying, psychic, bug, | |
rock, ghost, dragon, dark, steel, fairy] | |
iteration = 0 | |
while len(all_pokemon) > 1: | |
iteration += 1 | |
print("Round {}".format(iteration)) | |
attackers = [] | |
for pokemon in all_pokemon: | |
attacker = random.choice(all_pokemon) | |
while attacker in attackers: | |
attacker = random.choice(all_pokemon) | |
attackers.append(attacker) | |
for attacker in attackers: | |
if attacker.is_knocked_out is True: | |
continue | |
defender = random.choice(all_pokemon) | |
while attacker == defender: | |
defender = random.choice(all_pokemon) | |
damage = random.randint(1, 21) | |
damage_type = random.choice(all_elements) | |
attacker.attack(defender, damage_type, damage) | |
print("-" * 20) | |
if defender.is_knocked_out is True: | |
all_pokemon.remove(defender) | |
print("{} is the winner!".format(all_pokemon[0].name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment