-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimals.py
163 lines (121 loc) · 4.13 KB
/
animals.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
import tkinter as tk
from PIL import ImageTk, Image
import random
from random import shuffle
import pyttsx3
import pygame
global win
global inp
global correct
inp=-1
correct=-1
def gif_Player(root,txt):
gif = Image.open(txt)
# Create a list of GIF frames
frames = []
for frame in range(0, gif.n_frames):
gif.seek(frame)
gif2 = gif.resize((250, 250), Image.ANTIALIAS)
frames.append(ImageTk.PhotoImage(gif2))
# Create a tkinter label to display the GIF
label = tk.Label(root, image=frames[0])
label.pack()
# Define a function to animate the GIF
def animate(frame):
label.configure(image=frames[frame])
root.after(50, animate, (frame + 1) % len(frames))
# Start the animation
root.after(0, animate, 0)
return label
def button_function_correct():
print("correct button pressed")
return 0
def button_function_wrong():
print("wrong button pressed")
return 0
def play_mp3_file(filename):
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
pygame.time.delay(3000) # wait for the sound to finish playing
#pygame.mixer.music.stop()
# Define a function to play an MP3 file
def play_audio():
pygame.mixer.init()
pygame.mixer.music.load("media/audio.mp3")
pygame.mixer.music.play()
def window2(txt):
root.destroy()
window2_main = tk.Tk()
window2_main.configure(bg='beige')
tk.Label(window2_main, text=txt).pack()
return window2_main
WORDS = {'cat': 'media/cat.gif', 'dog': 'media/dog.gif', 'bird': 'media/bird.gif'}
SOUNDS = {'cat': 'media/cat-meow-14536.mp3','dog': 'media/dog_barking-6296.mp3','bird':'media/cardinal-37075.mp3' }
word = random.choice(list(WORDS.keys()))
sound = random.choice(list(SOUNDS.keys()))
if word == sound:
correct = 1
else:
correct = 0
# Initialize pygame mixer
pygame.mixer.init()
# Create the tkinter root window
root = tk.Tk()
#tk.reload_state(root)
root.title("Animal Sound Game")
root.configure(bg='beige')
label=gif_Player(root,WORDS[word])
# Create a label for the title
title_label = tk.Label(root, text="Is the animal making the right sound?")
title_label.pack()
# Load the audio file
audio = pygame.mixer.Sound(SOUNDS[sound])
# Create a callback function to play the audio when the image is clicked
def play_audio(event):
audio.play()
# Bind the callback function to the label's click event
# Bind the play_audio function to the left mouse button event of the GIF label
label.bind("<Button-1>", lambda event: play_audio(audio))
# Create a frame for the buttons and pack it below the image
button_frame = tk.Frame(root)
button_frame.pack()
win = -1
# Create the "Correct" button and pack it into the frame
correct_button = tk.Button(button_frame, text="Correct", command=button_function_correct)
correct_button.pack(side="left")
# Create the "Wrong" button and pack it into the frame
wrong_button = tk.Button(button_frame, text="Wrong", command=button_function_wrong)
wrong_button.pack(side="left")
tile_labels = [correct_button,wrong_button]
def on_tile_click(event):
global inp
tile_label=event.widget
if(tile_label['text']=="Correct"):
inp=1
if(tile_label['text']=="Wrong"):
inp=0
print("*inp", inp)
if(inp==correct):
win=1
else:
win=0
if(win==1):
play_mp3_file('media/crowd-cheering-143103.mp3')
window2_main=window2("Congratulations! You've guessed correctly")
label=gif_Player(window2_main,"media/gif1.gif")
button1=tk.Button(window2_main,text="Exit", command=window2_main.destroy)
button1.pack()
window2_main.mainloop()
else:
print("Sorry, you failed. Try again.")
play_mp3_file('media/medieval-fanfare-6826.mp3')
window2_main=window2("You've guessed incorrectly, but that's okay! Try again!")
label=gif_Player(window2_main,"media/gif2.gif")
button1=tk.Button(window2_main,text="Exit", command=window2_main.destroy)
button1.pack()
window2_main.mainloop()
for tile_label in tile_labels:
tile_label.bind("<Button-1>", on_tile_click)
# Start the tkinter main loop
root.mainloop()