-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (125 loc) · 7.09 KB
/
main.py
File metadata and controls
139 lines (125 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import random
from types import NoneType
from api_call import get_pokemon_info
from helper import give_nickname, choose_move, shiny_chance, add_to_pokedex, \
display_pokemon_info, check_pokedex, perform_attack
from type_chart import type_effectiveness_chart
def main():
print("Welcome to the Pokémon Battle Simulator!")
while True:
choice = input(
"Do you want to: \n1. Start a Pokémon battle \n2. Add Pokémon to your Pokédex \n3. Check Type Effectiveness \n4. Exit\nChoice-number: ")
if choice == "1":
#Pokémon battle
multiplayer = False
pokemon = []
nicknames = []
game_mode = input("Do you want to play: 1. singleplayer or 2. multiplayer:\nChoice-number: ")
if game_mode == "2":
multiplayer = True
check_pokedex()
#
for i in range(2):
#Change the input message based on the game mode
if multiplayer:
name = input(f"Player {i + 1}, enter your Pokémon's name: ")
# Ask the players if they want to give their Pokémon a nickname
nicknames.append(input(f"Player {i + 1}, would you like to give your Pokémon a nickname?: "))
else:
name = input("Enter Pokémon's name: ")
#give only your Pokémon a nickname
if i == 0:
nicknames.append(input(f"Would you like to give {name} a nickname?: "))
#Get the Pokémon information from the PokéAPI
pokemon.append(get_pokemon_info(name))
shiny_chance(pokemon[i])
if pokemon[i] is None:
print(f"Could not find {name}, please try again\n")
break
#If the index is 0 and the length of the nicknames list is 1, give the nickname to the first Pokémon
if i == 0 and len(nicknames) == 1:
#give the first Pokémon a nickname, because the other pokemon is not user controlled.
pokemon[0] = give_nickname(pokemon[0], nicknames[0])
elif len(nicknames) > 1:
#give all Pokémon a nickname
pokemon[i] = give_nickname(pokemon[i], nicknames[i])
#If the first or second Pokémon is None, the battle cannot start
if pokemon[0] is None or pokemon[1] is None:
continue
# The battle is active while 1 of the 2 Pokémon has not fainted yet (hp > 0)
#The pokemon battle is not created dynamically, it is hardcoded to be 2 players
print(f"{pokemon[0]['name']} {pokemon[0]['gender']} vs. {pokemon[1]['name']} {pokemon[1]['gender']}")
print("Let the battle begin!")
while (pokemon[0]["stats"]["hp"] > 0) and (pokemon[1]["stats"]["hp"] > 0):
print(
f"\n{pokemon[0]["name"]} {pokemon[0]['gender']} current HP: {pokemon[0]["stats"]["hp"]} & {pokemon[1]["name"]} {pokemon[1]['gender']} current HP: {pokemon[1]["stats"]["hp"]}")
# Choose a move for each Pokémon
move_a = choose_move(pokemon[0])
# If the gamemode is multiplayer, the second player chooses a move
# Otherwise, a random move is chosen
if multiplayer:
move_b = choose_move(pokemon[1])
else:
move_b = random.choice(pokemon[1]["moves"])
#Who goes first?
# Based on the speed stat, the Pokémon with the higher speed stat goes first, this move is printed first
if pokemon[0]["stats"]["speed"] > pokemon[1]["stats"]["speed"]:
#The first pokémon is faster so it performs a move first
pokemon[0], pokemon[1] = perform_attack(pokemon[0], pokemon[1], move_a)
# Check if the second Pokémon can still attack
if pokemon[1]["stats"]["hp"] > 0:
pokemon[1], pokemon[0] = perform_attack(pokemon[1], pokemon[0], move_b)
else:
#The second pokémon is faster so it performs a move first
pokemon[1], pokemon[0] = perform_attack(pokemon[1], pokemon[0], move_b)
# Check if the first Pokémon can still attack
if pokemon[0]["stats"]["hp"] > 0:
pokemon[0], pokemon[1] = perform_attack(pokemon[0], pokemon[1], move_a)
#End of the battle, create a winner message
if pokemon[0]["stats"]["hp"] == 0:
print(f"{pokemon[0]["name"]} has fainted! {pokemon[1]["name"]} wins!\n")
elif pokemon[1]["stats"]["hp"] == 0:
print(f"{pokemon[1]["name"]} has fainted! {pokemon[0]["name"]} wins!\n")
elif choice == "2":
#Pokédex
pokemon_name = input(
"Welcome to the pokédex where you can see types, moves, stats & evolutions of every Pokémon. "
"\nAfter which you can add the Pokémon to your Pokédex to use in battle! "
"\nEnter the name of the Pokémon you want to see: ")
pokemon = get_pokemon_info(pokemon_name, True)
if pokemon:
display_pokemon_info(pokemon)
choice = input(f"Do you wanna add {pokemon['name']} to your pokedex? (y/n): ")
if choice.lower() == "y":
add_to_pokedex(pokemon)
else:
print(f"{pokemon['name']} is not added to your pokedex")
else:
print(f"Could not find information about {pokemon_name}\n")
elif choice == "3":
#Pokémon type effectiveness checker
pokemon_type = input("Enter the Pokémon type: ")
#capitalize the first letter of the pokemon type
pokemon_type = pokemon_type.capitalize()
if pokemon_type not in type_effectiveness_chart.keys():
print(f"{pokemon_type} is not a valid Pokémon type\n")
continue
#show all types which this type is effective against
print(f"Type effectiveness of {pokemon_type} type:")
for key, value in type_effectiveness_chart.items():
if pokemon_type in value and value[pokemon_type] == 2:
print(f" {key} type is super-effective against {pokemon_type} type")
elif pokemon_type in value and value[pokemon_type] == 0.5:
print(f" {key} type is not very effective against {pokemon_type} type")
elif pokemon_type in value and value[pokemon_type] == 0:
print(f" {key} type has no effect against {pokemon_type} type")
#print a new line
print()
elif choice == "4":
#exit with a pokemon greeting
print("Thank you for playing the Pokémon Battle Simulator! Gotta catch 'em all!")
break
else:
print("Invalid choice, please try again\n")
if __name__ == "__main__":
main()