-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
238 lines (169 loc) · 6.4 KB
/
test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Description: This is a program that load a pokjemon from a third party library and print it's name and it's abilities and show the image of the pokemon
# Importing the requests library
import requests
import random
# Defining the URL of the API
pokemon_id = random.randint(1, 151)
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_id}"
# Making a request to the API
response = requests.get(url)
# Getting the JSON data from the response
data = response.json()
# Getting the name of the pokemon
name = data["name"]
# Getting the abilities of the pokemon
abilities = data["abilities"]
# Printing the name of the pokemon
print("Name: " + name)
# Printing the abilities of the pokemon
print("Abilities: ")
for ability in abilities:
print(ability["ability"]["name"])
# Getting the image of the pokemon
image_url = data["sprites"]["front_default"]
# Importing the Image library
from PIL import ImageTk, Image
import requests
from io import BytesIO
import tkinter as tk
import pygame
# Making a request to the image URL
response = requests.get(image_url)
# Getting the image data from the response
image_data = response.content
# Creating an image object from the image data
image = Image.open(BytesIO(image_data))
# Convert the image to 'RGB'
image = image.convert('RGB')
# Resizing the window to fit the image
image_width, image_height = image.size
window_width = image_width + 20
window_height = image_height + 20
# Displaying the image
image.show()
# Initialize the pygame mixer
pygame.mixer.init()
# Function to play the pokemon cry
def play_pokemon_cry():
# Get the sound URL from the API response
sound_url = data["species"]["url"]
# Make a request to the sound URL
response = requests.get(sound_url)
sound_data = response.json()
# Get the sound file URL
sound_file_url = sound_data["flavor_text_entries"][0]["sound"]["url"]
# Make a request to the sound file URL
response = requests.get(sound_file_url)
sound_data = response.content
# Save the sound file locally
with open("pokemon_cry.wav", "wb+") as file:
file.write(sound_data)
# Load and play the sound file
pygame.mixer.music.load("pokemon_cry.wav")
pygame.mixer.music.play()
# Function to randomize the pokemon sprite
def randomize_sprite2():
# Generate a new random pokemon ID
pokemon_id = random.randint(1, 151)
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_id}"
# Make a request to the API
response = requests.get(url)
data = response.json()
# Get the new image URL
image_url = data["sprites"]["front_default"]
sound_url = data['cries']['latest']
# Make a request to the new image URL
response = requests.get(image_url)
image_data = response.content
# Create a new image object from the image data
image = Image.open(BytesIO(image_data))
image = image.convert('RGB')
# Convert the PIL Image object to a Tkinter PhotoImage object
tk_image = ImageTk.PhotoImage(image)
# Update the displayed image
image_label.configure(image=tk_image)
image_label.image = tk_image
# Play the pokemon cry
response = requests.get(sound_url)
sound_data = response.content
# Get the moves of the pokemon
moves = data["moves"]
# Get the updated name of the pokemon
name = data["name"]
# Create a label to display the name of the pokemon
name_label = tk.Label(window, text="Name: " + name)
name_label.pack()
# Display only the first 4 moves
for move in moves[:4]:
move_name = move["move"]["name"]
move_label = tk.Label(window, text=move_name)
move_label.pack()
# Create a label to display the moves of the pokemon
#moves_label = tk.Label(window, text="Moves: ")
#moves_label.pack()
# Save the sound file locally
with open(f"pokemon_cry{pokemon_id}.ogg", "wb+") as file:
file.write(sound_data)
# Load and play the sound file
pygame.mixer.music.load(f"pokemon_cry{pokemon_id}.ogg")
pygame.mixer.music.play()
# Create a list to store the labels
labels = []
# Function to randomize the pokemon sprite
def randomize_sprite():
# Clear the previous labels
for label in labels:
label.destroy()
labels.clear()
# Your existing code
pokemon_id = random.randint(1, 151)
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_id}"
response = requests.get(url)
data = response.json()
image_url = data["sprites"]["front_default"]
sound_url = data['cries']['latest']
response = requests.get(image_url)
image_data = response.content
image = Image.open(BytesIO(image_data))
image = image.convert('RGB')
tk_image = ImageTk.PhotoImage(image)
image_label.configure(image=tk_image)
image_label.image = tk_image
# Get the updated name of the pokemon
name = data["name"]
# Create a label to display the name of the pokemon
name_label = tk.Label(window, text="Name: " + name)
name_label.pack()
labels.append(name_label)
# Get the moves of the pokemon
moves = data["moves"]
# Create buttons for each move
for move in moves[:4]:
move_name = move["move"]["name"]
move_button = tk.Button(window, text=move_name, command=lambda move_name=move_name: use_move(move_name))
move_button.pack()
labels.append(move_button)
# Display only the first 4 moves
""" for move in moves[:4]:
move_name = move["move"]["name"]
move_label = tk.Label(window, text=move_name)
move_label.pack()
labels.append(move_label)
"""
# Your existing code
response = requests.get(sound_url)
sound_data = response.content
with open(f"pokemon_cry{pokemon_id}.ogg", "wb+") as file:
file.write(sound_data)
pygame.mixer.music.load(f"pokemon_cry{pokemon_id}.ogg")
pygame.mixer.music.play()
# Create a new tkinter window
window = tk.Tk()
# Create a button to randomize the pokemon sprite
randomize_button = tk.Button(window, text="Randomize Sprite", command=randomize_sprite)
randomize_button.pack()
# Create a label to display the image
image_label = tk.Label(window)
image_label.pack()
# Run the tkinter event loop
window.mainloop()