-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
217 lines (192 loc) · 6.47 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
from random import *
import playsound
from tkinter import *
from PIL import Image, ImageTk
from threading import Thread
import speech_recognition as sr
import pyttsx3
import time
from pynput.keyboard import Key, Controller
def closeWindow():
keyboard = Controller()
keyboard.press(Key.alt_l)
keyboard.press(Key.f4)
keyboard.release(Key.f4)
keyboard.release(Key.alt_l)
try:
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id) #male
engine.setProperty('volume', 1)
except Exception as e:
print(e)
def speak(text):
print(text)
engine.say(text)
engine.runAndWait()
def record():
global userchat
userchat['text'] = "Listening..."
r = sr.Recognizer()
# r.dynamic_energy_threshold = False
# r.energy_threshold = 4000
r.pause_threshold = 1
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(f"\nUser said: {said}")
except Exception as e:
print(e)
speak("I think it is invalid move...")
return "None"
return said.lower()
moves = ['rock', 'paper', 'scissor']
class RockPaperScissor:
def __init__(self):
self.playerScore = 0
self.botScore = 0
self.total_moves = 0
self.intro()
def intro(self):
speak("Welcome to the Rock Paper Scissor Game. To STOP the Match, say STOP or Cancel. Let's Play.")
def nextMove(self, move):
global userchat, botchat, totalLabel, botMoveLBL
userchat['text'] = move.upper()
botMove = randint(0,2)
playerMove = moves.index(move)
botchat['text'] = moves[botMove].upper()
self.total_moves += 1
if botMove==playerMove:
self.botScore += 1
self.playerScore += 1
elif botMove==0:
if playerMove==1:
self.playerScore += 1
else:
self.botScore += 1
elif botMove==1:
if playerMove==2:
self.playerScore += 1
else:
self.botScore += 1
else:
if playerMove==0:
self.playerScore += 1
else:
self.botScore += 1
totalLabel['text'] = str(self.botScore)+' | '+str(self.playerScore)
if botMove==0: botMoveLBL['image'] = rockImg
if botMove==1: botMoveLBL['image'] = paperImg
if botMove==2: botMoveLBL['image'] = scissorImg
speak('I choose: ' + str(moves[botMove]))
return botMove+1
def whoWon(self):
result = ""
if self.playerScore == self.botScore:
result = "The match is draw !\n"
elif self.playerScore > self.botScore:
result = "You won the match Sir! Well Done !\n"
else:
result = "You lose the match Sir! Haha!\n"
for el in root.winfo_children():
el.destroy()
if 'won' in result:
Label(root, image=winImg).pack(pady=30)
elif 'lose' in result:
Label(root, image=loseImg).pack(pady=30)
else:
Label(root, image=drawImg).pack(pady=30)
result += "You have won " +str(self.playerScore)+"/"+str(self.total_moves)+" matches."
Label(root, text='Score', font=('Arial Bold', 50), fg='#FE8A28', bg='white').pack()
Label(root, text=str(self.playerScore)+' / '+str(self.total_moves), font=('Arial Bold', 40), fg='#292D3E', bg='white').pack()
speak(result)
time.sleep(1)
closeWindow()
return
rockImg, paperImg, scissorImg, userchat, botchat, totalLabel, botMoveLBL, userMoveLBL, winImg, loseImg, drawImg = None, None, None, None, None, None, None, None, None, None, None
def playRock():
rp = RockPaperScissor()
while True:
global botMoveLBL, userMoveLBL
move = record()
if isContain(move, ["don't", "cancel", "stop"]):
rp.whoWon()
break
else:
img = None
if 'rock' in move:
userMoveLBL['image'] = rockImg
img = rp.nextMove('rock')
elif 'paper' in move:
userMoveLBL['image'] = paperImg
img = rp.nextMove('paper')
elif 'scissor' in move or 'caesar' in move:
userMoveLBL['image'] = scissorImg
img = rp.nextMove('scissor')
def rockPaperScissorWindow():
global root, rockImg, paperImg, scissorImg, userchat, botchat, totalLabel, botMoveLBL, userMoveLBL, winImg, loseImg, drawImg
root = Tk()
root.title('Rock Paper Scissor')
# root.resizable(0,0)
# root.attributes('-toolwindow', True)
w_width, w_height = 400, 650
s_width, s_height = root.winfo_screenwidth(), root.winfo_screenheight()
x, y = (s_width/2)-(w_width/2), (s_height/2)-(w_height/2)
root.geometry('%dx%d+%d+%d' % (w_width,w_height,x,y-30)) #center location of the screen
root.configure(bg='white')
rockImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/1.jpg'))
paperImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/2.jpg'))
scissorImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/3.jpg'))
grayImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/grayQuestion.png'))
orangeImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/orangeQuestion.jpg'))
winImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/win.jpg'))
loseImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/lose.jpg'))
drawImg = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/draw.jpg'))
toplbl = Label(root, text='Total Score', font=('Arial Bold', 20), fg='#FE8A28', bg='white').pack()
totalLabel = Label(root, text='0 | 0', font=('Arial Bold', 15), fg='#1F1F1F', bg='white')
totalLabel.pack()
#bottom image
img = ImageTk.PhotoImage(Image.open('extrafiles/ROCKPAPERSCISSOR/rockPaperScissor.jpg'))
downLbl = Label(root, image=img)
downLbl.pack(side=BOTTOM)
#user response
userchat = Label(root, text='Listening...', bg='#FE8A28', fg='white', font=('Arial Bold',13))
userchat.place(x=300, y=120)
userMoveLBL = Label(root, image=orangeImg)
userMoveLBL.place(x=260, y=150)
#bot response
botchat = Label(root, text='Waiting...', bg='#EAEAEA', fg='#494949', font=('Arial Bold',13))
botchat.place(x=12, y=120)
botMoveLBL = Label(root, image=grayImg)
botMoveLBL.place(x=12, y=150)
Thread(target=playRock).start()
root.iconbitmap("extrafiles/images/game.ico")
root.mainloop()
def isContain(text, lst):
for word in lst:
if word in text:
return True
return False
def play(gameName):
speak('')
if isContain(gameName, ['dice','die']):
playsound.playsound('extrafiles/audios/dice.mp3')
result = "You got " + str(randint(1,6))
return result
elif isContain(gameName, ['coin']):
playsound.playsound('extrafiles/audios/coin.mp3')
p = randint(-10,10)
if p>0: return "You got Head"
else: return "You got Tail"
elif isContain(gameName, ['rock','paper','scissor','first']):
rockPaperScissorWindow()
return
else:
print("Game Not Available")
def showGames():
return "1. Rock Paper Scissor"
if __name__ == "__main__":
rockPaperScissorWindow()