forked from SydneyTechnologies/riddleGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriddle_game.py
More file actions
235 lines (204 loc) · 6.49 KB
/
riddle_game.py
File metadata and controls
235 lines (204 loc) · 6.49 KB
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
import random
from tkinter.constants import END, FIRST, LAST, LEFT, RIGHT
import pyttsx3
import turtle
import tkinter
from tkinter.ttk import *
from datamuse import datamuse
import time
from requests.models import StreamConsumedError
from something import removeCat
# the code above is import the external libraries we need to make this game work
# --- INITIALIZING THE GAME SCREEN --- #
TK = tkinter.Tk(className="Riddle Game")
screen = tkinter.Canvas(master= TK, width=500, height=500)
screen.pack()
pen = turtle.RawTurtle(screen)
bottom_frame = tkinter.LabelFrame(TK, bd=1, text="Your Score")
bottom_frame.pack(side=LEFT)
scoreLabel = tkinter.Label(bottom_frame, bd=1)
scoreLabel.pack()
pen.hideturtle()
pen.penup()
# scoreTurtle.hideturtle()
# scoreTurtle.penup()
# scoreTurtle.goto(150, 250)
score = 0
# scoreTurtle.color('grey')
# scoreTurtle.write('SCORE: ' + str(score), move=False, font=['Courier', 16])
api = datamuse.Datamuse()
engine = pyttsx3.init()
end_game = False
ans = pen.clone()
user_input_answer = "$"
answer = ""
trun_answer = ""
hintGlobal = ""
question = ""
# style.configure('TkButton', font=('calibri', 20,'bold'), borderwidth = '2')
# -- INITIALIZE GAME VOICE -- #
def Init_voice():
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.setProperty('rate', 175)
Init_voice()
# -- GETTING RIDDLES --- #
asset_file = open("output.txt")
game_dictionary = []
for line in asset_file:
x = line.split("?")
game_dictionary.append(x)
# -- PICKING A QUESTION -- #
def Pick_question():
index = random.randint(0, len(game_dictionary) - 1)
print(index, len(game_dictionary))
question_list = game_dictionary[index]
print(question_list[0], index, question_list[1])
game_dictionary.remove(question_list)
print(question_list[1])
# The question_list is a list containing the question and the answer
raw_answer = question_list[1].replace(",", "", 1)
raw_answer = raw_answer.replace(".", "")
# formatting answer to a single word
x = raw_answer.split()
print(x)
print(raw_answer)
primary_answer = x[len(x)-1]
print(primary_answer)
hint_rhymes = api.words(rel_rhy = primary_answer, max = 5)
return [question_list[0], question_list[1], hint_rhymes, primary_answer]
# Pick_question()
print(game_dictionary[1])
def formatString(sentence, max):
#print(len(sentence))
count = 0
start = 0
spaces = 0
sentence_list = []
for i in range(len(sentence)):
if sentence[i] == ' ':
spaces +=1
if spaces < max:
sentence_list.append(sentence)
return sentence_list
for i in range(len(sentence)):
if sentence[i] == ' ':
count +=1
if count % max == 0:
new_sentence = sentence[start:i]
sentence_list.append(new_sentence)
print(new_sentence)
start = i
if count == (spaces // max) * max:
sentence_list.append(sentence[start:])
print(sentence[start:])
return sentence_list
def verify_answer():
normalized_answer = trun_answer.lower()
normalized_user_answer = user_input.get().lower()
correct = True
# print(hintGlobal)
if normalized_user_answer == normalized_answer:
print('Correct')
global score
score += 50
user_input.delete(0, END)
scoreLabel.configure(text="Score: " + str(score))
writeAnswer(normalized_user_answer, correct)
time.sleep(2)
pen.clear()
ans.clear()
if score >= 100:
print("Game has been completed")
print("Replay button Created")
GenericWrite("YOU HAVE COMPLETED THE GAME", completed=True)
return
gameLoop()
# then we increase the score or something
else:
correct = False
writeAnswer(normalized_user_answer, correct)
def enter_key_validate(event):
if user_input.get() == "":
return
else:
print(trun_answer)
print(user_input.get())
verify_answer()
def writeAnswer(answer, isCorrect):
y = pen.ycor()
ans.goto(0, y - 100)
concat = " is correct"
if isCorrect == True:
ans.color('green')
else:
ans.color("red")
concat = " is wrong, start again"
ans.write(answer + " " + concat, move= False, font=['Courier', 16], align='center')
def GenericWrite(input_text: str, completed: bool):
if completed == True:
pen.goto(-100, 100)
pen.color('black')
pen.write(input_text)
else:
current_y = pen.ycor()
pen.goto(0, current_y + 14)
pen.write(input_text)
def gameLoop():
riddle_list = Pick_question()
global question
question = riddle_list[0]
pen.goto(-100, 100)
pen.color('black')
new_question = formatString(question, 7)
new_question.reverse()
for i in range(len(new_question)):
current_y = pen.ycor()
pen.goto(0, current_y + 14)
print(new_question[i])
pen.write(new_question[i], False, font=["Courier", 13], align='center')
global trun_answer
trun_answer = riddle_list[3]
GenericWrite(input_text=trun_answer, completed=False)
global answer
answer = riddle_list[1]
global hintGlobal
hintGlobal = riddle_list[2]
# engine.say(question)
# engine.say("The answer is " + answer)
# engine.runAndWait()
# user_input = screen.textinput("INPUT", "what am i?")
# verify_answer(answer, user_input_answer)
user_input = tkinter.Entry(TK)
user_input.pack()
btn = tkinter.Button(master = TK, text="Submit")
btn.pack()
btn.configure(command= verify_answer)
def complete_game():
if score >= 100:
print("Game has been completed")
print("Replay button Created")
return
def hint_generator():
global hint
hint = hintGlobal
# print(hint)
hintIndex = random.randint(0, 4)
if hint == []:
wordToSay = "Sorry, there are no hints for this question"
else:
wordToSay = "I'll give you a word that rhymes with it: " + hint[hintIndex]['word']
engine.say(wordToSay)
engine.runAndWait()
hintBtn = tkinter.Button(master = TK, text="Generate Hint")
hintBtn.pack(side=RIGHT)
hintBtn.configure(command= hint_generator)
def repeatQuestion():
engine.say(question)
engine.runAndWait()
repeatBtn = tkinter.Button(master= TK, text="Repeat Question", bg="#6495ED")
repeatBtn.pack(side=RIGHT)
repeatBtn.configure(command= repeatQuestion)
TK.bind("<Return>", enter_key_validate)
gameLoop()
screen.mainloop()