-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemon-project.py
143 lines (110 loc) · 5.02 KB
/
pokemon-project.py
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
140
141
142
143
import requests
import random
# Class to represent the player and store persistent stats
class Player:
def __init__(self):
# Introducing a new player object with initial stats
self.wins = 0
self.losses = 0
# These attributes store the player's performance across multiple rounds in the game.
# The wins and losses are initially set to 0, and they will be updated as the player progresses in the game.
# Each player object created from this class will have its own wins and losses attributes.
# The __init__ method is a special method that is automatically called when a new player object is created.
# Function to get information about a random Pokemon from the PokeAPI
def random_pokemon():
player_pokemon_number = random.randint(1, 151)
opponent_pokemon_number = player_pokemon_number
# Keep generating a new opponent's Pokemon until it's different from the player's Pokemon
while opponent_pokemon_number == player_pokemon_number:
opponent_pokemon_number = random.randint(1, 151)
player_url = 'https://pokeapi.co/api/v2/pokemon/{}/'.format(player_pokemon_number)
player_response = requests.get(player_url)
player_pokemon = player_response.json()
opponent_url = 'https://pokeapi.co/api/v2/pokemon/{}/'.format(opponent_pokemon_number)
opponent_response = requests.get(opponent_url)
opponent_pokemon = opponent_response.json()
# Return dictionaries containing the player's and opponent's Pokemon data
return {
'player': {
'name': player_pokemon['name'],
'id': player_pokemon['id'],
'height': player_pokemon['height'],
'weight': player_pokemon['weight'],
},
'opponent': {
'name': opponent_pokemon['name'],
'id': opponent_pokemon['id'],
'height': opponent_pokemon['height'],
'weight': opponent_pokemon['weight'],
}
}
# Function to display Pokemon information
def display_pokemon(pokemon, show_stats=True):
print("Name: {}".format(pokemon['name']))
print("ID: {}".format(pokemon['id']))
# Display height and weight if show_stats is True
if show_stats:
print("Height: {} decimetres".format(pokemon['height']))
print("Weight: {} hectograms".format(pokemon['weight']))
print("\n")
# Main game function
def run():
# Print a welcome message to the player
print("Welcome to Pokemon Top Trumps!")
# Create an instance of the Player class to track persistent stats
player = Player()
# Continuous loop for the game
while True:
# Display available game modes
print("\nGame Modes:")
print("1. Normal Mode")
print("2. Difficult Mode")
print("3. Exit")
# Allow the player to choose a game mode
mode_choice = input("Choose a game mode (1/2/3): ")
# Check if the player wants to exit the game
if mode_choice == '3':
print("Thanks for playing! Goodbye.")
break
# Introducing the round counter
rounds = 0
# Loop for each round (best of 3 rounds)
while rounds < 3:
# Display the current round number
print("\nRound {}".format(rounds + 1))
# Generate Pokemon for the player and opponent
pokemon_data = random_pokemon()
player_pokemon = pokemon_data['player']
opponent_pokemon = pokemon_data['opponent']
# In easy mode, show Pokemon data. In difficult mode this won't be seen.
if mode_choice == '1':
print("Your Pokemon:")
display_pokemon(player_pokemon)
# Ask the user which stat they want to use (id, height, or weight)
valid_stats = ['id', 'height', 'weight']
stat_to_compare = input("Which stat do you want to compare? (id/height/weight): ").lower()
if stat_to_compare in valid_stats:
player_stat = player_pokemon[stat_to_compare]
opponent_stat = opponent_pokemon[stat_to_compare]
print("Your Pokemon's {}: {}".format(stat_to_compare, player_stat))
# Display opponent's Pokemon after player's input
input("Press Enter to reveal the opponent's Pokemon.")
display_pokemon(opponent_pokemon)
# Compare the stats and determine the winner
if player_stat > opponent_stat:
print("You win this round!")
player.wins += 1
elif player_stat < opponent_stat:
print("Opponent wins this round!")
player.losses += 1
else:
print("It's a tie!")
rounds += 1
else:
print("Invalid stat. Please choose 'id', 'height', or 'weight.'")
print("\nGame Over!")
print("Wins: {}".format(player.wins))
print("Losses: {}".format(player.losses))
# Run the game if the script is executed
if __name__ == "__main__":
run()