coder2539@gmail.com

This program simulates a pokemon battle between two pokemon
 avatar
Coder
python
2 years ago
5.2 kB
2
Indexable
Never
import random
#authur_michael

#this is a program that simulates a
#simple pokemon battle

#stores the pokemon names and moves in a dictionary
pokemons_list= {
	"Pikachu":["thunderbolt","quickattack","irontail","electroweb"],
	"Charizard":["flamethrower","thunderpunch","dragonclaw","blastburn"],
	"Blastiose":["hydropump","flashcannon","dragonpulse","powerpunch"],
	"Venusaur":["frenzyplant","absorb","Earthquake","Gigaimpact"],
	"Absol":["nightslash","slash","psychocut","hyperbeam"]
	
	
}
#this function initialised the trainer name
def trainer_name(name):
    trainer_name.name = name


#this function selects a pokemon from the pokemons_list above
#and set up the pokemon stats
def select_pokemon(mon_name,hp,speed):
    #this function checks to see if the input is in the list and initialise the pokemon stats
    select_pokemon.state = False
    if mon_name not in pokemons_list:
        print(pokemons_list)
        print(mon_name +"is not in list")
    else:
        if ((hp > 0 and hp < 301)and(speed >0 and speed <301)):
            select_pokemon.name , select_pokemon.health,select_pokemon.sp = (mon_name,hp,speed)
            select_pokemon.state = True
            print("You have chosen  ", select_pokemon.name,"stats")
            print("hp:",hp,"sp:",speed)
            print("moves: ", pokemons_list[mon_name])
        else:
            print("invalid stats")
 
            
#this function creates the computer pokemon 
#and initializes it stats with a random number
def computer_pokemon():
    computer_pokemon.state = True
    computer_pokemon.sp=random.randint(1,300)
    computer_pokemon.hp=300
    computer_pokemon.name = random.choice(list(pokemons_list.keys()))
    computer_pokemon.moves = pokemons_list[computer_pokemon.name]
 
 
   
#this method is used to start the game
#and to check if all the necessary variables have been set         
def start_battle():
    #this method starts 
    t_name = input("Enter your name ")
    trainer_name(t_name)
    for val,key in enumerate(pokemons_list):print(val,key)
    p_name= input("Enter pokemon name. ")
    hp=int(input("Enter hp "))
    sp=int(input("Enter sp "))
    start_battle.stats= (p_name,hp,sp)
    select_pokemon(*start_battle.stats)
    if select_pokemon.state:
        attack()
    else:
        print("Some variables might have not been set")
        
        
#the attack() handles most of the logic in the game
#such as which pokemon gets to attack first etc.      
def attack():
    #programs how a pokemon attacks
    computer_pokemon()
    print("Computer has chosen ", computer_pokemon.name)
    #this function handles when player takes damage
    def take_damage_player():
        take_damage_player.isfainted=False
        select_pokemon.health -= damage()
        print(select_pokemon.name+" has ", select_pokemon.health,"left")
        if select_pokemon.health <= 0: take_damage_player.isfainted = True
        pass
    #this sub_function handles when the computer takes damage
    def take_damage_computer():
        take_damage_computer.isfainted = False
        computer_pokemon.hp -= damage()
        print(computer_pokemon.name+" has ", computer_pokemon.hp,"left")
        if computer_pokemon.hp <= 0:take_damage_computer.isfainted = True
              
    #this function generates and returns a random damage number
    def damage():
        damage.damage = random.randint(1,150)
        return damage.damage
        
    #this loop will continue as long as both the computers pokemon and trainers pokemon
    #have hp >= 1 
    while select_pokemon.health >= 1 and computer_pokemon.hp >= 1:        
        print("choose a move?")
        moves = pokemons_list[select_pokemon.name]
        for index,item in enumerate(pokemons_list.get(select_pokemon.name)):print(index,item)
        move = int(input("enter move num(0-3):"))
        moves = pokemons_list[select_pokemon.name]
        
        
        
        if select_pokemon.sp > computer_pokemon.sp:
            print(select_pokemon.name+" used "+moves[move])
            take_damage_computer()
            #checks to see if computer_pokemon is fainted
            if take_damage_computer.isfainted:
                print(computer_pokemon.name+" is fainted")
                break
            print("foe "+computer_pokemon.name+" used "+random.choice(computer_pokemon.moves))
            take_damage_player()
       #if computer pokemon speed > player_pokemon_speed.
        elif computer_pokemon.sp > select_pokemon.sp:
            print("foe "+computer_pokemon.name+" used "+random.choice(computer_pokemon.moves))
            take_damage_player()
            #checks to see if player_pokemon is fainted
            if take_damage_player.isfainted:
                print(select_pokemon.name+" is fainted")
                break
            print(select_pokemon.name+" used "+moves[move])
            take_damage_computer()
            if take_damage_computer.isfainted:
                print("foe "+computer_pokemon.name+" is fainted")
    else:
        print("**BATTLE ENDED**")
            
            
        
    	   
start_battle()  
reply = input("Would you like to play again y/n ")
if reply == "y":
    start_battle()