-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgame.py
340 lines (272 loc) · 10.5 KB
/
game.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
#Word Master game
#importing modules
from random import randint
import threading
import pyttsx3
import datetime
import speech_recognition as sr
import time
import requests
#importing the contents of the file game.txt
#f = open("game.txt","r")
#game = f.read().split("\n")
'''lev = int(input("enter your choice :"))
if(lev == 1) :
print(" You choose easy level ")
speak(" You choose easy level ")
f = open("easy.txt","r")
game = f.read().split("\n")
elif(lev == 2):
print(" You choose medium level ")
speak(" You choose medium level ")
f = open("medium.txt","r")
game = f.read().split("\n")
elif (lev == 3):
print(" You choose easy level ")
speak(" You choose easy level ")
f = open("hard.txt","r")
game = f.read().split("\n")
else:
print(" You did not choose a proper level ")
speak(" You did not choose a proper level ")'''
engine = pyttsx3.init('sapi5')#Initializing the voice given by microsoft
voices = engine.getProperty('voices')#getting voice
engine.setProperty('voice',voices[0].id)#setting the male voice of david for use
engine.setProperty('rate',150)#speed property of voice
#function for text to speech conversion
def speak(audio):
engine.say(audio)
engine.runAndWait()
#function taking command as a speech and converting it to text
def takeCommand():
#it takes audio from microphone and returns a string
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening.............")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing..............")
query = r.recognize_google(audio, language="en-in")
print(f"you said.......{query}\n")
except Exception:
print("Say again.................")
return "none"
return query
def wishMe():
hour = int(datetime.datetime.now().hour)
if(hour>=5 and hour<12):
speak("Good Morning")
elif(hour>=12 and hour<16):
speak("Good Afternoon")
else:
speak("Good Evening")
speak("Hello game player we are team ligers How can i help you!")
#class for calculating the score
class Score(object):
score = 0
k = 0
life = 3
flag = 0
#for threading timer
def timeUp():
print("Times Up!.....\nYou can still guess the correct answer but your score will not be increased : ")
speak("Time's up ! but you can still guess the correct answer but your score will not be calculated")
Score.flag = 1
class Scene(object):#Scene class
def enter(self):
print("Scene yet not configured............")
lev = int(input("enter your choice :"))
if(lev == 1) :
print(" You choose easy level ")
speak(" You choose easy level ")
f = open("easy.txt","r")
game = f.read().split("\n")
elif(lev == 2):
print(" You choose medium level ")
speak(" You choose medium level ")
f = open("medium.txt","r")
game = f.read().split("\n")
elif (lev == 3):
print(" You choose easy level ")
speak(" You choose easy level ")
f = open("hard.txt","r")
game = f.read().split("\n")
else:
print(" You did not choose a proper level ")
speak(" You did not choose a proper level ")
class Game(Scene):#inheritng from scene class
def enter(self):
Score.flag = 0
l2 = []
b = game[Score.k]
Score.k = Score.k+1#next question
c = list(b)
d = randint(2,len(b) - 1)
for i in range(d):
h = randint(0,len(c) - 1)
if(h in l2):
i = i-1
else:
l2.append(h)
#printing incomplete word
print(f"Your remaining life : {Score.life}")
speak(f"Your remaining life is {Score.life}")
speak("Your incomplete word is")
print("Word : ",end = " ")
for j in range(len(c)):
if(j in l2):
print("__",end = " ")
else:
print(c[j],end = " ")
print("\n")
#for another thread for the timer
timer = threading.Timer(8.0,timeUp)
timer.start()#starting the second thread
print("Guess the correct word.....")
answe = "none"
while(answe=="none"):
answe = takeCommand()
answer = answe.split(" ")
app_id = '0d2e058d' #dictionary api app id
app_key = '1e3a9ae05fa064c632aec887792d61f0' #app key to authenticate requests
url= "https://od-api.oxforddictionaries.com/api/v2/lemmas" + "/" + "en-us" + "/" + answer.lower()
r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})
if(b in answer):
#if the user does not give answer
if(Score.flag==1):
Score.flag = 0
print(f"Time is up but Your answer is correct so we will not make any changes to your current score.\nYour Score is : {Score.score}")
speak(f"Time is up but Your answer is correct so we will not make any changes to your current score and Your Score is : {Score.score}")
#if the user gave the answer
else:
timer.cancel()#ending the second thread
Score.score = Score.score + 10
print(f"Wohoo! You made it.\nYour score is : {Score.score}\n")
speak(f"Wohoo! You made it and Your score is : {Score.score}")
if(Score.k == len(game)):
return "over"
#else:
# level()
#if answer exists in dictionary and time is not up
elif("error" not in r.text and Score.flag!=1):
timer.cancel()#ending the second thread
Score.score = Score.score + 10
print(f"Wohoo! You made it.\nYour score is : {Score.score}\n")
speak(f"Wohoo! You made it and Your score is : {Score.score}")
if(Score.k == len(game)):
return "over"
else:
if(Score.flag==1):
Score.flag = 0
Score.score = Score.score - 5
Score.life = Score.life - 1
print(f"Time is up and your answer is wrong .\nCorrect answer is : {b}\nYour score is : {Score.score}\n")
speak(f"Time is up and your answer is wrong and Correct answer is : {b} and Your score is : {Score.score}")
else:
timer.cancel()
Score.score = Score.score - 5
Score.life = Score.life - 1
print(f"Ahaa!! Wrong answer.\nCorrect answer is : {b}\nYour score is : {Score.score}\n")
speak(f"Ahaa!! Wrong answer and Correct answer is : {b} and Your score is : {Score.score}")
if(Score.life == 0):
return "lifeN"
elif(Score.k == len(game)):
return "over"
print("Would you like to continue[Yes(y)/No(n)] : ")
speak("Would you like to continue.")
prompt = takeCommand().split(" ")
if("no" in prompt):
return "end"
else:
return "game"
class End(Scene):#inheritng from scene class
def enter(self):
print("You wished to discontinue the game.........\n")
speak("You wished to discontinue the game")
return "finished"
# adding level function
'''def level():
if Score.score < -10 :
f = open("easy.txt","r")
game = f.read().split("\n")
elif Score.score > -10 :
f = open("medium.txt","r")
game = f.read.split("\n")
else:
f = open("game.txt","r")
game = f.read().split("\n")
return "lev" '''
class GameOver(Scene):#inheritng from scene class
def enter(self):
print("The game is over you made it to the second round.........\n")
speak("The game is over you made it to the second round")
return "finished"
class LifeOver(Scene):
def enter(self):
print("Your life is over..........\n")
speak("Your life is over")
return "finished"
class Finished(Scene):#inheritng from scene class
def enter(self):
print("********************Thanks for playing**********************")
print(f"Your Score is ||{Score.score}||\n")
speak("Thanks for playing")
speak(f"Your Score is {Score.score}")
class Map(object):
mapping = {"game":Game(),
"end":End(),
"over":GameOver(),
"lifeN":LifeOver(),
"finished":Finished(),
"lev":level()
}
def __init__(self,start):
self.start = start
def nextScene(self,scene):
val = Map.mapping[scene]
return val
def OpeningScene(self):
return self.nextScene(self.start)
class Engine(object):
def __init__(self,map):
self.map = map
def play(self):
currentScene = self.map.OpeningScene()
lastScene = self.map.nextScene("finished")
while(currentScene!=lastScene):
next = currentScene.enter()
currentScene = self.map.nextScene(next)
currentScene.enter()
if __name__=="__main__":
wishMe()
while True :
query = takeCommand()
if (("game" in query) or ("play" in query)):
print("*********************Welcome to the game of Word Master***************************")
speak("Welcome to the game of Word Master.")
print("""The rules of the game are :
1.You have only 3 lives.
2.Each correct answer will give you 10 points.
3.Each wrong answer will deduct 5 points from your total score
4.You have 5 seconds to answer a particular question
Choose your difficulty level :
1. Easy
2. Medium
3. Hard """)
speak("""The rules of the game are :
1 You have only 3 lifes.
2 Each correct answer will give you 10 points.
3 Each wrong answer will deduct 5 points from your total score
4 You have 5 seconds to answer a particular question
Choose your difficulty level :
1. Easy
2. Medium
3. Hard """)
time.sleep(1)
a = Map("game")
gi = Engine(a)
gi.play()
elif("exit" or "quit" in query):
break
#query = takeCommand()