robbiecarroll.com

GIS | Data Science & Analysis | Coding & Automation | Deep Learning

Python Example 1

#!/usr/bin/env python
# coding: utf-8

#Robert Carroll
#DSC615 Spring 2024
#Project 1: Pokémon Battle System

import random


#R1&R2_______________________________________________
def r1(r1_text, input1):
    for text in r1_text:
        print(text)

    while type(input1)==str:
        
        input1 = input('<<Select an option then press enter to continue>>\n')
        try:
            input1 = int(input1)
            if input1 == 1:
                print("It's Battle time!")
                return(input1)
            if input1 == 2:
                print(f"You ran away! Exiting program.")
                return(input1)
            else:
                input1 = str(input1)
                raise ValueError()
   
        except ValueError:
            print(f'"{input1}" is not a valid entry: Please input 1 or 2 into the textbox.')
            
            


#R4_______________________________________________      
def r4(char_list, Bulbasaur_status, Pikachu_status, Bulbasaur_hp, Bulbasaur_acc, Pikachu_hp, Pikachu_acc, count):
    print(f'***ROUND {count} CHARACTER STATS***:')
    for char in char_list:
        print(char)
        if char == 'Bulbasaur':
            print(Bulbasaur_status)
        if char == 'Pikachu':
            print(Pikachu_status)
        else:
            pass
        
#R5________________________________________________       
def r5(Bulbasaur_status, Bulbasaur_hp, Bulbasaur_acc, Pikachu_status, Pikachu_hp, Pikachu_acc, tackle_points, sand_points, Pikachu_loses_all_hp):
    
    reactions = ['F#*%!!!!', 'WHOOOOAA', 'GOSH DARN IT!', 'WHAT A JERK!', 'OH HELL NAH...', 'THIS GUY IS TOUGH!']
    select_reaction = random.choices(reactions)
    
    print(f'Bulbasaur attacks with either Tackle or Sand Attack!! {select_reaction[0]}')
    
    #options
    bulbasar_options = ['Tackle', 'Sand Attack']
    
    #randomly choose the action
    bulbasaur_action_random_selection = random.randint(0, 1)
    
    #select the action:
    selection = bulbasar_options[bulbasaur_action_random_selection]  
    print(f"!! Bulbasaur uses {selection} !!")

    enter = input("<<press enter>>\n")
    
    while enter != '':
        enter = input("<<Please press enter to continue>>\n")

    while enter == '':
        if selection == 'Tackle':
            #options
            tackle_options = ['Bam! (Hit! -10 hp)', 'But it missed!']

            #Probabilistically choose the outcome
            rand_tackle_result = random.choices(tackle_options, weights=(Bulbasaur_acc, 100-Bulbasaur_acc), k=1)
            rand_tackle_result = rand_tackle_result[0]
            print(rand_tackle_result)

            #Calculate attack impact
            if rand_tackle_result == tackle_options[0]:
                Pikachu_hp-=tackle_points
                if Pikachu_hp <= 0:
                    Pikachu_loses_all_hp = True
                else:
                    pass
            if rand_tackle_result == tackle_options[1]:
                pass



        if selection == 'Sand Attack':
            #options
            sandattack_options = ['Swoosh! (Hit! -10 acc)', 'But it missed!']

            #Probabilistically choose the outcome
            rand_sandattck_result = random.choices(sandattack_options, weights=(Bulbasaur_acc, 100-Bulbasaur_acc), k=1)
            rand_sandattck_result = rand_sandattck_result[0]
            print(rand_sandattck_result)

            #Calculate attack impact
            if rand_sandattck_result == sandattack_options[0]:
                Pikachu_acc-=sand_points
            if rand_sandattck_result == sandattack_options[1]:
                pass
            
        return(Pikachu_hp, Pikachu_acc, Pikachu_loses_all_hp)
    
    
   
        
#R6________________________________________________   
def r6(r6_text, input6):   
    for text in r6_text:
        print(text)
    while type(input6)==str:
        input6 = input('<<enter 1, 2, or 3>>')
        if input6 == '1' or input6 == '2' or input6 == '3':
            input6 = int(input6)
            return(input6)                 
        else:
            print(f'"{input6}" is not a valid entry: Please enter only the numbers 1, 2, or 3.')
            pass

            
#R7________________________________________________        
def r7(r6_return, Bulbasaur_status, Bulbasaur_hp, Bulbasaur_acc,  Pikachu_status, Pikachu_hp, Pikachu_acc, tackle_points, sand_points, Bulbasaur_loses_all_hp, Bulbasaur_is_caught):
    
    #options
    pikachu_options = ['Tackle', 'Sand Attack', 'Throw Pokeball']

    #select the action:
    selection = pikachu_options[r6_return-1]
  
    print(f"!! Pikachu uses {selection} !!")
    enter = input("<<press enter>>\n")
    
    while enter != '':
        enter = input("<<Please enter either 1, 2, or 3 into the textbox>>\n")
    
    while enter == '':
    
        if selection == 'Tackle':
            #options
            tackle_options = ['Bam! (Hit! -10 hp)', 'But it missed!']

            #Probabilistically choose the outcome
            rand_tackle_result = random.choices(tackle_options, weights=(Pikachu_acc, 100-Pikachu_acc), k=1)
            rand_tackle_result = rand_tackle_result[0]
            print(rand_tackle_result)

            if rand_tackle_result == tackle_options[0]:
                Bulbasaur_hp-=tackle_points
                if Bulbasaur_hp <= 0:
                    Bulbasaur_loses_all_hp = True
                else:
                    pass
            if rand_tackle_result == tackle_options[1]:
                pass


        if selection == 'Sand Attack':
            #options
            sandattack_options = ['Swoosh! (Hit! -10 acc)', 'But it missed!']

            #Probabilistically choose the outcome
            rand_sandattck_result = random.choices(sandattack_options, weights=(Pikachu_acc, 100-Pikachu_acc), k=1)
            rand_sandattck_result = rand_sandattck_result[0]
            print(rand_sandattck_result)

            #Calculate attack impact
            if rand_sandattck_result == sandattack_options[0]:
                Bulbasaur_acc-=sand_points
            if rand_sandattck_result == sandattack_options[1]:
                pass


        if selection == 'Throw Pokeball':
            #options
            pokeball_options = ['!!!!!!!YOU CAUGHT A BULBASAUR!!!!!!!', 'NOOOO, it broke free, I NEED TO KEEP FIGHTING!']

            #Probabilistically choose the outcome
            rand_pokeball_result = random.choices(pokeball_options, weights=(100-Bulbasaur_hp, Bulbasaur_hp), k=1)
            rand_pokeball_result = rand_pokeball_result[0]
            print(rand_pokeball_result)

            #try to catch that thing
            if rand_pokeball_result == pokeball_options[0]:
                Bulbasaur_is_caught = True
            if rand_pokeball_result == pokeball_options[1]:
                Bulbasaur_is_caught = False

        return(Bulbasaur_hp, Bulbasaur_acc, Bulbasaur_loses_all_hp, Bulbasaur_is_caught)

        

        

#MAIN PROCESS________________________________________________

def main():
     
    #ALL VARIABLES___________________________________________
    #R1
    r1_text = ['What do you want to do?', '1. Battle','2. Run away']   
    input1 = '' 
    #R4 Vars
    char_list = ['Bulbasaur', 'Pikachu']
    Bulbasaur_hp = 100
    Bulbasaur_acc = 100
    Pikachu_hp = 100
    Pikachu_acc = 100
    Bulbasaur_status = f'hp {Bulbasaur_hp} acc {Bulbasaur_acc}'
    Pikachu_status = f'hp {Pikachu_hp} acc {Pikachu_acc}'
    #R6
    r6_text = ['Select an attack option for Pikachu:', '1. Tackle','2. Sand Attack', '3. Throw Pokeball']   
    input6 = ''
    #R10      
    tackle_points = 10
    #R11 
    sand_points = 10       
    #R12.
    Pikachu_loses_all_hp = False
    Bulbasaur_loses_all_hp = False
    Bulbasaur_is_caught = False
    
    #START MAIN PROGRAM________________________________________
    while True:
    
        print("It's a Bulbasaur...")
        enter = input('<<press "enter" to continue>>\n')

        while enter != '':
            enter = input('<<press "enter" to continue>>\n')

        while enter == '':          
            #R1 #R2

            r1_return = r1(r1_text, input1) 
            print('\n')

            if r1_return == 1:            
                #R3
                print("Pikachu I choose you")
                enter = input("<<press enter>>\n")

                while enter != '':
                    enter = input('<<press "enter" to continue>>\n')

                while enter == '':
                    count=0
                    #R4 - display the hitpoints and accuracy for each Pokémon
                    r4(char_list, Bulbasaur_status, Pikachu_status, Bulbasaur_hp, Bulbasaur_acc, Pikachu_hp, Pikachu_acc, count)
                    print('\n')

                    while Pikachu_loses_all_hp != True and Bulbasaur_loses_all_hp != True and Bulbasaur_is_caught != True:
                        count+=1
                        print(f'ROUND {count}: FIGHT!_____________________________________________________________\n')

                        #R5 + R8 + R9 + R10 + R11 Bulbasar Attacks
                        r5_return = r5(Bulbasaur_status, Bulbasaur_hp, Bulbasaur_acc, Pikachu_status, Pikachu_hp, Pikachu_acc, tackle_points, sand_points, Pikachu_loses_all_hp)
                        try:
                            Pikachu_hp = r5_return[0]
                            Pikachu_acc = r5_return[1]
                            Pikachu_loses_all_hp = r5_return[2]
                        except:
                            enter = ''
                        print('\n')

                        if Pikachu_hp > 0:

                        #R6 - Select Pikachu Attack
                            r6_return = r6(r6_text, input6)
                            print('\n')

                            #R7 + R8 + R9 + R10 + R11 - Pikachu Attacks
                            r7_return = r7(r6_return, Bulbasaur_status, Bulbasaur_hp, Bulbasaur_acc,  Pikachu_status, Pikachu_hp, Pikachu_acc, tackle_points, sand_points, Bulbasaur_loses_all_hp, Bulbasaur_is_caught)
                            Bulbasaur_hp = r7_return[0]
                            Bulbasaur_acc = r7_return[1]
                            Bulbasaur_loses_all_hp = r7_return[2]
                            Bulbasaur_is_caught = r7_return[3]
                            print('\n')

                        else:
                            pass

                        #R4 - Display the hitpoints and accuracy for each Pokémon
                        Bulbasaur_status = f'hp {Bulbasaur_hp} acc {Bulbasaur_acc}'
                        Pikachu_status = f'hp {Pikachu_hp} acc {Pikachu_acc}'
                        r4(char_list, Bulbasaur_status, Pikachu_status, Bulbasaur_hp, Bulbasaur_acc, Pikachu_hp, Pikachu_acc, count)
                        print('\n')

                        #R12_____________________________________________________________
                        if Pikachu_loses_all_hp == True and Bulbasaur_loses_all_hp == False and Bulbasaur_is_caught == False:
                            print('OH NO! Pikachu fainted. Game Over. Please try again.')
                            print('\n')
                            break


                        if Bulbasaur_loses_all_hp == True and Pikachu_loses_all_hp == False and Bulbasaur_is_caught == False:
                            print('OH NO! Bulbasaur fainted. Game Over. Please try again.')
                            print('\n')
                            break


                        if Bulbasaur_is_caught == True and Bulbasaur_loses_all_hp == False and Pikachu_loses_all_hp == False:
                            print('**********************************')
                            print('GOOD JOB!: YOU CAUGHT A BULBASAUR!')
                            print("YOU'RE A POKEMASTER!")
                            print('**********************************')
                            print('\n')
                            break

                    break


            if r1_return == 2:
                break
            else:
                break
            break
        if r1_return == 2:
            break
        
main()

SAMPLE OUTPUT

It's a Bulbasaur
<<press enter>>
What do you want to do?
1. Battle
2. Run Away
<<enter 1 or 2>>Not sure
Please enter 1 or 2
What do you want to do?
1. Battle
2. Run Away
<<enter 1 or 2>>1
Pikachu I choose you
<<press enter>>
Bulbasaur
  hp 100  accuracy 100
Pikachu
  hp 100  accuracy 100
<<press enter>>
attack 1
Bulbasaur uses Sand attack
<<press enter>>
Swoosh!
<<press enter>>
Bulbasaur
  hp 100  accuracy 100
Pikachu
  hp 100  accuracy 90
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>Tackle
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>1
Pikachu uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 90  accuracy 100
Pikachu
  hp 100  accuracy 90
<<press enter>>
attack 0
Bulbasaur uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 90  accuracy 100
Pikachu
  hp 90  accuracy 90
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>2
Pikachu uses Sand attack
<<press enter>>
Swoosh!
<<press enter>>
Bulbasaur
  hp 90  accuracy 90
Pikachu
  hp 90  accuracy 90
<<press enter>>
attack 0
Bulbasaur uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 90  accuracy 90
Pikachu
  hp 80  accuracy 90
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>2
Pikachu uses Sand attack
<<press enter>>
Swoosh!
<<press enter>>
Bulbasaur
  hp 90  accuracy 80
Pikachu
  hp 80  accuracy 90
<<press enter>>
attack 1
Bulbasaur uses Sand attack
<<press enter>>
Swoosh!
<<press enter>>
Bulbasaur
  hp 90  accuracy 80
Pikachu
  hp 80  accuracy 80
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>1
Pikachu uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 80  accuracy 80
Pikachu
  hp 80  accuracy 80
<<press enter>>
attack 0
Bulbasaur uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 80  accuracy 80
Pikachu
  hp 70  accuracy 80
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>1
Pikachu uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 70  accuracy 80
Pikachu
  hp 70  accuracy 80
<<press enter>>
attack 0
Bulbasaur uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 70  accuracy 80
Pikachu
  hp 60  accuracy 80
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>3
You threw a Pokeball
<<press enter>>
But Bulbasaur broke free!
<<press enter>>
<<press enter>>
Bulbasaur
  hp 70  accuracy 80
Pikachu
  hp 60  accuracy 80
<<press enter>>
attack 1
Bulbasaur uses Sand attack
<<press enter>>
But it missed!
<<press enter>>
Bulbasaur
  hp 70  accuracy 80
Pikachu
  hp 60  accuracy 80
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>3
You threw a Pokeball
<<press enter>>
But Bulbasaur broke free!
<<press enter>>
<<press enter>>
Bulbasaur
  hp 70  accuracy 80
Pikachu
  hp 60  accuracy 80
<<press enter>>
attack 0
Bulbasaur uses Tackle
<<press enter>>
Bam!
<<press enter>>
Bulbasaur
  hp 70  accuracy 80
Pikachu
  hp 50  accuracy 80
<<press enter>>
What to do?
1. Tackle
2. Sand attack
3. Throw Pokeball
<<enter 1, 2, or 3>>3
You threw a Pokeball
<<press enter>>
You caught a Bulbasaur!
<<press enter>>
It's a Bulbasaur
<<press enter>>
What do you want to do?
1. Battle
2. Run Away
<<enter 1 or 2>>2
You Ran Away
Goodbye

© 2024 Robert Carroll