-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspellingGame.py
226 lines (169 loc) · 6.88 KB
/
spellingGame.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
import tkinter as tk
from PIL import ImageTk, Image
import random
from random import shuffle
import pyttsx3
import pygame
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()
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","dog","fish","bird"]
images=['../cat.jpg','../dog.png','../fish.jpg','../bird.jpg']'''
#WORDS = {'cat': '../cat.jpg', 'dog': '../dog.png', 'bird': '../bird.jpg','fish':'../fish.jpg'}
WORDS = {'cat': 'media/cat.gif', 'dog': 'media/dog.gif', 'bird': 'media/bird.gif','fish':'media/fish.gif'}
# Define the word to be guessed
word = random.choice(list(WORDS.keys()))
#image = WORDS[word]
# Create a list of letters from the word and shuffle them
letters = list(word)
shuffle(letters)
# Create a list of underscore placeholders for each letter in the word
placeholders = ["_" for _ in word]
# Create a window
root = tk.Tk()
root.title("Word Game")
root.configure(bg='beige')
gif = Image.open(WORDS[word])
'''gif2 = gif1.resize((250, 250), Image.ANTIALIAS)
gif=ImageTk.PhotoImage(gif2)
label = tk.Label(root, image=gif)
label.config(image=gif)
label.pack()'''
# 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)
'''img = Image.open(image)
img = img.resize((250, 250), Image.ANTIALIAS) # resize the image
img_tk = ImageTk.PhotoImage(img)
# Create a label widget to display the image
label = tk.Label(root, image=img_tk)
label.pack()'''
# Create a label for the title
title_label = tk.Label(root, text="Unscramble the word!")
title_label.pack()
# Initialize the text-to-speech engine
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
# Set properties
engine.setProperty('rate', 150) # Speed percent (can go over 100)
engine.setProperty('volume', 1) # Volume 0-1
for i in word:
engine.say(i)
engine.runAndWait()
engine.say(word)
engine.runAndWait()
# Create a frame for the tiles
tile_frame = tk.Frame(root)
tile_frame.pack(side=tk.BOTTOM)
# Create tile labels for each letter and pack them into the frame
tile_labels = []
for letter in letters:
tile_label = tk.Label(tile_frame, text=letter, relief=tk.RAISED, borderwidth=2)
tile_label.pack(side=tk.LEFT, padx=5, pady=5)
tile_labels.append(tile_label)
# Create a frame for the placeholders
placeholder_frame = tk.Frame(root)
placeholder_frame.pack(side=tk.TOP)
# Create placeholder labels for each letter and pack them into the frame
placeholder_labels = []
for placeholder in placeholders:
placeholder_label = tk.Label(placeholder_frame, text=placeholder, font=("Arial", 24), relief=tk.SUNKEN, borderwidth=2, width=2, height=1)
placeholder_label.pack(side=tk.LEFT, padx=5, pady=5)
placeholder_labels.append(placeholder_label)
# Create a function to handle tile label clicks
def on_tile_click(event):
# Get the clicked tile label
tile_label = event.widget
# Find the leftmost free placeholder label
for i, placeholder_label in enumerate(placeholder_labels):
if placeholder_label["text"] == "_":
break
else:
# If there are no free placeholders, return
return
# Put the letter from the clicked tile into the leftmost free placeholder
placeholder_label.configure(text=tile_label["text"])
tile_label.pack_forget()
# Check if all placeholders have been filled
if all([placeholder_label["text"] != "_" for placeholder_label in placeholder_labels]):
# If all placeholders are filled, check if the word is correct
guessed_word = "".join([placeholder_label["text"] for placeholder_label in placeholder_labels])
if guessed_word == word:
play_mp3_file('media/winbanjo-96336.mp3')
window2_main=window2("Congratulations! You've guessed correctly")
gif = Image.open("media/gif1.gif")
# Create a list of GIF frames
frames = []
for frame in range(0, gif.n_frames):
gif.seek(frame)
frames.append(ImageTk.PhotoImage(gif))
# Create a tkinter label to display the GIF
label = tk.Label(window2_main, image=frames[0])
label.pack()
# Define a function to animate the GIF
def animate(frame):
label.configure(image=frames[frame])
window2_main.after(50, animate, (frame + 1) % len(frames))
# Start the animation
window2_main.after(0, animate, 0)
button1=tk.Button(window2_main,text="Exit", command=window2_main.destroy)
button1.pack()
window2_main.mainloop()
play_mp3_file('media/yay-6326.mp3')
else:
print("Sorry, you failed. Try again.")
play_mp3_file('media/good-6081.mp3')
window2_main=window2("You've guessed incorrectly, but that's okay! Try again!")
gif = Image.open("media/gif2.gif")
# Create a list of GIF frames
frames = []
for frame in range(0, gif.n_frames):
gif.seek(frame)
frames.append(ImageTk.PhotoImage(gif))
# Create a tkinter label to display the GIF
label = tk.Label(window2_main, image=frames[0])
label.pack()
# Define a function to animate the GIF
def animate(frame):
label.configure(image=frames[frame])
window2_main.after(50, animate, (frame + 1) % len(frames))
# Start the animation
window2_main.after(0, animate, 0)
button1=tk.Button(window2_main,text="Exit", command=window2_main.destroy)
button1.pack()
window2_main.mainloop()
'''#engine.say("Sorry, you failed. Try again.")
# Re-shuffle the tiles
shuffle(letters)
for i, tile_label in enumerate(tile_labels):
tile_label.configure(text=letters[i])
# Reset the placeholders
for placeholder_label in placeholder_labels:
placeholder_label.configure(text="_")'''
# Bind tile label clicks to the on_tile_click function
for tile_label in tile_labels:
tile_label.bind("<Button-1>", on_tile_click)
# Start the tkinter mainloop
root.mainloop()