forked from prathimacode-hub/Awesome_Python_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjumbled_words.py
354 lines (327 loc) · 10.4 KB
/
jumbled_words.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import tkinter
from tkinter.constants import END
from tkinter.font import names
import random
from tkinter import Label, messagebox
from typing import Text
#import tkMessageBox
#list of categories and words in each list
fruits =["apple","pineapple","orange","mango","banana","cherry"]
countries =["india", "australia","england","germany","austria","indonesia","japan","singapore"]
superheroes = ["batman","batwoman","catwoman","hawkeye","supergirl","thor","flash"]
vegetables = ["tomato","potato","brinjal","onion","mushroom"]
sco = 0
score_txt ="Score:- "+str(sco)
#This function is called when the user presses the exit button
def destroy_fn():
#this will show a pop up window
messagebox.showinfo("EXIT MESSAGE","Thank you for playing the game")
#this is used to destroy window and exit game
gamewindow.destroy()
#This function is called when the user presses the next word button
def next_word(cat):
global word
jword =""
cat = cat.lower()
#print("catloer is ",cat)
#word = cat[random.randrange(0,len(cat))]
#jumble = random.sample(word,len(word))
#for i in jumble:
# jword += i
#print("inside next word")
#choosing a random word from the chossen category
if(cat == "fruits"):
word = random.choice(fruits)
elif(cat =="countries"):
word = random.choice(countries)
elif(cat == "superheroes"):
word = random.choice(superheroes)
elif(cat == "vegetables"):
word = random.choice(vegetables)
#print("word is ",word)
#jumble the letters of the word
jumble = random.sample(word,len(word))
#converting the lsit to string
for i in jumble:
jword += i
#print("jword is ",jword)
#displaying the word
word_label.configure(text = jword)
#deleting the existing text in the text box
get_input.delete(0, END)
#print("end of next word")
#deleting the ans shown
ans_space.configure(text ="")
#this function is called when the user presses the answer key
def showans():
global word
#showing the answer
ans_space.configure(text = word)
#This function is called when the user presses the submit button
def check_word(cat):
#guess = get_input.get().lower()
global sco,word
#print("word is ", word)
#print("inside check eord")
#user_input = get_input.get()
#storing the user input
guess = get_input.get().lower()
#print(user_word)
#checking if the guessed word is right
if(guess == word):
#print("correct")
#shoeing a po up mesage if guess is right
messagebox.showinfo("CORRECT ANS", "CONGRATULATIONS!!!!!! you found the right word")
#displaying the next word
next_word(cat)
# jword =""
# cat = cat.lower()
# #print("catloer is ",cat)
# #word = cat[random.randrange(0,len(cat))]
# #jumble = random.sample(word,len(word))
# #for i in jumble:
# # jword += i
# if(cat == "fruits"):
# word = random.choice(fruits)
# elif(cat =="countries"):
# word = random.choice(countries)
# elif(cat == "superheroes"):
# word = random.choice(superheroes)
# elif(cat == "vegetables"):
# word = random.choice(vegetables)
# print("word is ",word)
# jumble = random.sample(word,len(word))
# for i in jumble:
# jword += i
# print("jword is ",jword)
# word_label.configure(text = jword)
# get_input.delete(0, END)
#deleting the ans shown
ans_space.configure(text ="")
return
else:
#pop up box for wrong message
messagebox.showinfo("WRONG ANS", "Please try again")
#print("Wrong")
#deleting thw prv user input
get_input.delete(0, END)
#this fn is used to create the game window
def game(cat,jword):
#print("hello")
global word
#print("word is ",word)
#print("jumbled word is",jword)
#destroying the intro window
root.destroy()
global gamewindow
#creating the game window
gamewindow = tkinter.Tk()
gamewindow.geometry("600x600+600+200")
gamewindow.title(cat)
gamewindow.configure(bg="light blue")
global score
global word_label
#diplaying the word
word_label = tkinter.Label(
gamewindow,
text=jword,
pady=10,
bg="light blue",
fg="#000000",
font="Titillium 50 bold"
)
word_label.pack()
global get_input
answer = tkinter.StringVar()
# displaying the input box
get_input = tkinter.Entry(
font="none 26 bold",
borderwidth=10,
justify='center',
textvariable= answer
)
get_input.pack()
#user_word = get_input.get().upper()
#guess = get_input.get().lower()
#print(user_word)
#displaying submit buutton whixh calls check word
submit = tkinter.Button(
gamewindow,
text="SUBMIT",
width=18,
borderwidth=8,
font=("", 18),
fg="#000000",
bg="light green",
#cursor="hand2",
command=lambda:check_word(cat)
)
submit.pack(pady=(10,20))
#displaying answer button which calls showans fn
ans = tkinter.Button(
gamewindow,
text="ANSWER",
width=18,
borderwidth=8,
font=("", 18),
fg="#000000",
bg="light green",
#cursor="hand2",
command=lambda: showans(),
)
ans.pack(pady=(10,20))
#displaying next word button which calls nextword fn
nextWord = tkinter.Button(
gamewindow,
text="NEXT WORD",
width=18,
borderwidth=8,
font=("", 18),
fg="#000000",
bg="light green",
#cursor="hand2",-
command=lambda:next_word(cat)
)
nextWord.pack(pady=(10,20))
#displaing exxit button which calls destroy fn
exit_btn = tkinter.Button(
gamewindow,
text="EXIT",
width=18,
borderwidth=8,
font=("", 18),
fg="#000000",
bg="light green",
#cursor="hand2",
command=lambda:destroy_fn()
)
exit_btn.pack(pady=(10,20))
global ans_space
#shows the ans when needed initaaly it doesn not display anything
ans_space = tkinter.Label(
gamewindow,
text="",
pady=10,
bg="light blue",
fg="#000000",
font="Courier 15 bold"
)
ans_space.pack()
gamewindow.mainloop()
#this fn is used to find a random word from the choosen category and jumble it and then call the game fn
def start_game(args):
global word
jword =""
#print(args)
if(args == 1):
word = random.choice(fruits)
jumble = random.sample(word,len(word))
for i in jumble:
jword += i
# print(word,jword)
game("FRUITS",jword)
elif(args == 2):
word = random.choice(countries)
jumble = random.sample(word,len(word))
for i in jumble:
jword += i
# print(word,jumble)
game("COUNTRIES",jword)
elif(args == 3):
word = random.choice(superheroes)
jumble = random.sample(word,len(word))
for i in jumble:
jword += i
# print(word,jumble)
game("SUPERHEROES",jword)
elif(args == 4):
word = random.choice(vegetables)
jumble = random.sample(word,len(word))
for i in jumble:
jword += i
#print(word,jumble)
game("VEGETABLES",jword)
#this fn is to show the options to the user
def disp_option():
#print("hi")
#destroying all the elements which were previously displayed
start_btn.destroy()
label.destroy()
#Displaying fruits button
sel_btn1 = tkinter.Button(
root,
text="Fruits",
width=30,
borderwidth=10,
font=("", 18),
fg="#000000",
bg="#99ffd6",
cursor="hand2",
command=lambda: start_game(1),
)
#Displaying countries button
sel_btn2 = tkinter.Button(
root,
text="Countries",
width=30,
borderwidth=10,
font=("", 18),
fg="#000000",
bg="#99ffd6",
cursor="hand2",
command=lambda: start_game(2),
)
#Displaying superheores button
sel_btn3 = tkinter.Button(
root,
text="Superheroes",
width=30,
borderwidth=10,
font=("", 18),
fg="#000000",
bg="#99ffd6",
cursor="hand2",
command=lambda: start_game(3),
)
#Displaying vegetables button
sel_btn4 = tkinter.Button(
root,
text="Vegetables",
width=30,
borderwidth=10,
font=("", 18),
fg="#000000",
bg="#99ffd6",
cursor="hand2",
command=lambda: start_game(4),
)
#alliging the buttons
sel_btn1.grid(row=0, column=100, pady=(10, 0), padx=450 )
sel_btn2.grid(row=2, column=100, pady=(10, 0), padx=450 )
sel_btn3.grid(row=4, column=100, pady=(10, 0), padx=450 )
sel_btn4.grid(row=6, column=100, pady=(10, 0), padx=450 )
#creating the start window
root = tkinter.Tk()
root.geometry("600x600+600+200")
root.title("JUMBLED WORDS GUESSING GAME")
root.configure(bg="#856ff8")
label = tkinter.Label(root,font='times 35',text= "WELCOME TO JUMBLED WORDS GUESSING GAME ",bg= "#856ff8")#.place(x=30,y=60)
label.pack(pady =(150,0))#pady=30,ipady=150,ipadx=10)
#name = tkinter.StringVar()
#e1 = tkinter.Entry(root,textvariable=name)
#e1.pack(ipady=0,ipadx= 0 )
#print(name)
#displying the start button which when clicked will call the disp_option fn
start_btn = tkinter.Button(
root,
text="Start",
width=30,
borderwidth=10,
fg="#000000",
bg="#99ffd6",
font=("", 13),
cursor="hand2",
command=disp_option,
)
start_btn.pack(pady=(50, 20))
root.mainloop()