-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame code final.py
More file actions
616 lines (463 loc) · 14.2 KB
/
game code final.py
File metadata and controls
616 lines (463 loc) · 14.2 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# Import tkinter library
import serial
import tkinter as tk
import threading
from time import sleep
import random
from PIL import ImageTk as imtk
from PIL import Image as im
import RPi.GPIO as GPIO
#%%
# Create an instance of tkinter frame or window
root = tk.Tk()
root.attributes('-fullscreen',True)
mainframe = tk.Frame(root,width=1920, height=1080, bg="black")
mainframe.pack()
textFrame = tk.Frame(mainframe, bg="black")
textFrame.place(y=700,height=240,width = 1920)
imageframe = tk.Frame(mainframe, bg="black")
imageframe.place(height=700,width = 1920)
rock = imtk.PhotoImage(im.open('Images/rock.png'))
paper = imtk.PhotoImage(im.open('Images/paper.png'))
scissors = imtk.PhotoImage(im.open('Images/scissors.png'))
three = imtk.PhotoImage(im.open('Images/three.png'))
two = imtk.PhotoImage(im.open('Images/two.png'))
one = imtk.PhotoImage(im.open('Images/one.png'))
go = imtk.PhotoImage(im.open('Images/go.png'))
instruction = imtk.PhotoImage(im.open('Images/instruction.png'))
welcome = imtk.PhotoImage(im.open('Images/welcome.png'))
global move_list
move_list = ["Rock", "Paper", "Scissors"]
# IT IS THE GPIO NUMBER NOT THE PIN NUMBER ON THE SCHEMATIC
#Thumb
# 0.7F, 0.6B
in1=17 #pin 11
in2=18 #pin 12
#0.7 for both
#Index
in3=7 #pin 26
in4=8 #pin 24
#0.7 for both
#Middle
in5=22 #pin 15
in6=27 #pin 13
#0.6F, 0.9B
#Ring
in7=5 #pin 29
in8=6 #pin 31
#0.6F, 0.7B
#Pinky
in9=19 #pin 35
in10=16 #pin 36
#enable pin
enable =10 #pin 19
gpio=[in1,in2,in3,in4,in5,in6,in6,in7,in8,in9,in10]
#Time delay to determine angle of rotation
delayTime=3
GPIO.setmode(GPIO.BCM)
for i in gpio:
GPIO.setup(i,GPIO.OUT)
GPIO.setup(enable,GPIO.OUT)
#%%
def move_finger(pin1,pin2):
GPIO.output(pin1, GPIO.HIGH)
GPIO.output(pin2, GPIO.LOW)
# sleep(delayTime)
#%% NEUTRAL TO ROCK, PULL FORWARD ALL 5 FINGERS
def N_Rock():
move_finger(in1,in2) #Thumb
move_finger(in3,in4) #Index
move_finger(in5,in6) #Middle
move_finger(in7,in8) #Ring
move_finger(in9,in10) #pinky
sleep(0.7)
stop_finger(in2,in1) #Thumb
stop_finger(in4,in3) #Index
stop_finger(in6,in5) #Middle
stop_finger(in8,in7) #Ring
stop_finger(in10,in9) #pinky
pass
#%% NEUTRAL TO PAPER, DO NOTHING
def N_Paper():
pass
def stop_finger(inp1,inp2):
GPIO.output(inp1, GPIO.LOW)
GPIO.output(inp2, GPIO.LOW)
#%% NEUTRAL TO SCISSORS, PULL FORWARD FINGERS 1,4 AND 5
def N_Scissors():
move_finger(in1,in2) #Thumb
move_finger(in7,in8) #Ring
move_finger(in9,in10) #pinky
sleep(0.7)
stop_finger(in2,in1) #Thumb
stop_finger(in8,in7) #Ring
stop_finger(in10,in9) #pinky
#%% PAPER TO NEUTRAL, DO NOTHING
def P_Neutral():
pass
#%% ROCK TO NEUTRAL, PULL BACKWARD ALL 5 FINGERS
def R_Neutral():
move_finger(in2,in1) #Thumb
move_finger(in4,in3) #Index
move_finger(in6,in5) #Middle
move_finger(in8,in7) #Ring
move_finger(in10,in9) #pinky
sleep(0.8)
stop_finger(in2,in1) #Thumb
stop_finger(in4,in3) #Index
stop_finger(in6,in5) #Middle
stop_finger(in8,in7) #Ring
stop_finger(in10,in9) #pinky
#%% SCISSORS TO NEUTRAL, PULL BACKWARDS FINGERS 1,4 AND 5
def S_Neutral():
move_finger(in2,in1) #Thumb
move_finger(in8,in7) #Ring
move_finger(in10,in9) #pinky
sleep(0.8)
stop_finger(in2,in1) #Thumb
stop_finger(in8,in7) #Ring
stop_finger(in10,in9) #pinky
#%%
#All TO LOW
def all_to_low():
for i in gpio:
GPIO.output(i, GPIO.LOW)
# GPIO.cleanup()
#%%
def hand_movement(move):
if move == "Paper":
N_Paper()
elif move == "Rock":
N_Rock()
elif move== "Scissors":
N_Scissors()
def reverse_hand_movement(move):
if move == "Paper":
P_Neutral()
elif move == "Rock":
R_Neutral()
elif move== "Scissors":
S_Neutral()
#%%
def clear_frame(frame):
for widgets in frame.winfo_children():
widgets.destroy()
#%%
def change_screen(old_panel, item, name):
if (name == ""):
clear_frame(textFrame)
else:
text.delete("1.0","end")
text.insert(tk.INSERT, name)
old_panel.config(image='')
new_panel = tk.Label(imageframe, image = item, bg = "black")
new_panel.pack(side = "top", fill = "both", expand = "yes")
root.update()
return new_panel
#%%
def define_winner(robot_move,player_move):
global w_streak
global l_streak
global player_wins
global robot_wins
global game_counter
outcome = ""
if player_move == robot_move:
outcome = "draw"
w_streak = 0
l_streak = 0
elif player_move == "Rock" and robot_move == "Scissors" or player_move == "Scissors" and robot_move == "Paper" or player_move == "Paper" and robot_move == "Rock":
outcome = "winner"
w_streak += 1
l_streak = 0
player_wins +=1
game_counter += 1
elif player_move == "Scissors" and robot_move == "Rock" or player_move == "Paper" and robot_move == "Scissors" or player_move == "Rock" and robot_move == "Paper":
outcome = "loser"
w_streak = 0
l_streak += 1
robot_wins +=1
game_counter += 1
elif player_move == "None":
none_detected()
outcome = ""
elif player_move == "quit":
return
return outcome
#%%
def game_algorithm(outcome, player):
global w_streak
global l_streak
move_list = ["Rock", "Paper", "Scissors"]
if (outcome == "winner" and w_streak >=2):
next_move = "Rock"
elif (outcome == "loser" and l_streak >=2):
next_move = "Paper"
elif outcome == "winner" and w_streak ==1:
if player == "Rock":
next_move = "Paper"
elif player == "Paper":
next_move = "Scissors"
elif player =="Scissors":
next_move = "Rock"
elif outcome == "loser" and l_streak ==1:
if player == "Rock":
next_move = "Scissors"
elif player == "Paper":
next_move = "Rock"
elif player =="Scissors":
next_move = "Paper"
elif outcome == "draw":
next_move = random.choice(move_list)
return next_move
#%%
def outcome_screen(outcome):
global text
global win
global lose
global tie
response_message=""
if outcome == "winner":
response_message = "PLAYER WINS!!"
screen = random.choice(win)
elif outcome == "loser":
response_message = "PLAYER LOSES!"
screen = random.choice(lose)
elif outcome == "draw":
response_message = "IT'S A TIE!"
screen = random.choice(tie)
text = tk.Text(textFrame, font="Calibri, 40",bd=15)
text.insert(tk.INSERT, response_message)
text.insert(tk.INSERT, "\n\nPress the 'z' key to start the next round!")
text.pack()
return screen
#%%
def instruction_screen():
instructionFrame = tk.Frame(mainframe)
instructionFrame.place(x=0,y=0,height=1920,width = 1080)
instruc_panel = tk.Label(instructionFrame, image = instruction,bg = "black")
instruc_panel.pack()
root.update()
sleep(5)
clear_frame(instructionFrame)
instructionFrame.place_forget()
root.update()
#%%
def final_screen(outcome):
global text
global win_img
global lose
global panel
if (outcome == "win"):
text = tk.Text(textFrame, font="Calibri, 40",bd=15)
text.insert(tk.INSERT, "OH NO I CAN'T BELIEVE YOU BEAT ME \n")
text.insert(tk.INSERT, "You win! Press the 'z' key to play again! \n")
text.pack()
panel = tk.Label(imageframe, image = random.choice(win_img),bg = "black")
panel.pack(side = "top", fill = "both", expand = "yes")
elif(outcome == "lose"):
text = tk.Text(textFrame, font="Calibri, 40",bd=15)
text.insert(tk.INSERT, "HA HA OF COURSE I WON \n")
text.insert(tk.INSERT, "You lose! Press the 'z' key to play again! \n")
text.pack()
panel = tk.Label(imageframe, image = random.choice(lose),bg = "black")
panel.pack(side = "top", fill = "both", expand = "yes")
#%%
def number_to_move(buffer):
buffer = buffer.decode('UTF-8')
response = {'-1':"None", '0':'Rock', '1':'Paper', '2':'Scissors'}
return response[buffer]
#%%
def detections_thread():
serial_1 = serial.Serial(port = '/dev/ttyS0',baudrate= 460800, bytesize=8, timeout = 1)
global buffer
buffer = None
while(True):
serial_1.write(b"RXed")
print("waiting to RX input...")
buffer = serial_1.read()
sleep(0.03)
data_left = serial_1.inWaiting()
buffer+= serial_1.read(data_left)
print(buffer)
#%%
def main():
all_to_low()
GPIO.output(enable,GPIO.HIGH)
#tie
global tie
annoyed = imtk.PhotoImage(im.open('Images/annoyed.png'))
annoyed2 = imtk.PhotoImage(im.open('Images/annoyed2.png'))
confused = imtk.PhotoImage(im.open('Images/confused.png'))
confused2 = imtk.PhotoImage(im.open('Images/confused2.png'))
tie = [annoyed, annoyed2, confused, confused2]
#win
global win
angry = imtk.PhotoImage(im.open('Images/angry.png'))
angry2= imtk.PhotoImage(im.open('Images/angry2.png'))
cry = imtk.PhotoImage(im.open('Images/cry.png'))
cry2 = imtk.PhotoImage(im.open('Images/cry2.png'))
sad = imtk.PhotoImage(im.open('Images/sad.png'))
sad2 = imtk.PhotoImage(im.open('Images/sad2.png'))
win = [angry, angry2, cry, cry2,sad,sad2]
#lose
global lose
smug = imtk.PhotoImage(im.open('Images/smug.png'))
smug2 = imtk.PhotoImage(im.open('Images/smug2.png'))
happy = imtk.PhotoImage(im.open('Images/happy.png'))
happy2 = imtk.PhotoImage(im.open('Images/happy2.png'))
happy3 = imtk.PhotoImage(im.open('Images/happy3.png'))
happy4 = imtk.PhotoImage(im.open('Images/happy4.png'))
lose = [smug, smug2,happy,happy2,happy3,happy4]
global win_img
win_img = [annoyed,annoyed2, cry, cry2,sad,sad2]
global panel
panel = tk.Label(imageframe, image = welcome,bg = "black")
panel.pack(side = "top", fill = "both", expand = "yes")
global player_wins
player_wins = 0
global robot_wins
robot_wins =0
global w_streak
w_streak = 0
global l_streak
l_streak = 0
global game_counter
game_counter =1
global text
global move_list
rx = threading.Thread(target = detections_thread, daemon= True)
rx.start()
text = tk.Text(textFrame, font="Calibri, 40",bd=15)
text.insert(tk.INSERT, "Welcome! Press the 'z' Key to Play")
text.pack()
global response
response = random.choice(move_list)
root.bind('<KeyPress>',key_press)
root.mainloop()
#%%
def game_code():
global response
global game_counter
global panel
global buffer
global player_wins
global robot_wins
global mode
print(game_counter)
three_panel = change_screen(panel,three, "")
sleep(1)
two_panel = change_screen(three_panel,two, "")
sleep(1)
one_panel = change_screen(two_panel,one, "")
sleep(1)
clear_frame(textFrame)
clear_frame(imageframe)
goFrame = tk.Frame(mainframe, bg="black")
goFrame.place(x=540,height=700,width = 1080)
new_panel = tk.Label(goFrame, image = go, bg = "black")
new_panel.pack(side = "bottom", fill = "none", expand = "yes")
text = tk.Text(textFrame, font="Calibri, 40",bd=15)
move_message = "Robot Played: " + response
text.insert(tk.INSERT, move_message)
text.pack()
root.update()
sleep(1)
player=number_to_move(buffer)
if (mode == "party"):
if (player == "Rock"):
response = "Paper"
elif (player == "Paper"):
response = "Scissors"
elif (player == "Scissors"):
response = "Rock"
hand_movement(response)
clear_frame(goFrame)
clear_frame(textFrame)
goFrame.place_forget()
root.update()
result = define_winner(response,player)
if (result == ""):
reverse_hand_movement(response)
return
if (player_wins >2):
final_screen("win")
sleep(2)
reverse_hand_movement(response)
return
elif (robot_wins > 2):
final_screen("lose")
sleep(2)
reverse_hand_movement(response)
return
else:
screen = outcome_screen(result)
panel = tk.Label(imageframe, image = screen,bg = "black")
panel.pack(side = "top", fill = "both", expand = "yes")
root.update()
sleep(2)
reverse_hand_movement(response)
if game_counter >4:
response = random.choice(move_list)
else:
response = game_algorithm(result, player)
#%%
def none_detected():
global panel
global text
global tie
im = tie[2]
text = tk.Text(textFrame, font="Calibri, 40",bd=15)
text.insert(tk.INSERT, "Oh no I didn't see anything! \n")
text.insert(tk.INSERT, "Press the 'z' key to try again")
text.pack()
panel = tk.Label(imageframe, image = im,bg = "black")
panel.pack(side = "top", fill = "both", expand = "yes")
#%%
def key_press(e):
global player_wins
global robot_wins
global mode
if (player_wins >2 or robot_wins >2):
clear_frame(textFrame)
clear_frame(imageframe)
main()
if (e.keysym =='i'):
instruction_screen()
elif (e.keysym =='z'):
mode = "game"
game_code()
elif (e.keysym =='s'):
mode = "party"
game_code()
elif (e.keysym =='p'):
N_Scissors()
elif (e.keysym =='o'):
S_Neutral()
elif (e.keysym =='y'):
N_Rock()
elif (e.keysym =='u'):
R_Neutral()
# IT IS THE GPIO NUMBER NOT THE PIN NUMBER ON THE SCHEMATIC
#Thumb
# 0.7F, 0.6B
# in1=17 #pin 11
# in2=18 #pin 12
# #0.7 for both
# #Index
# in3=7 #pin 26
# in4=8 #pin 24
# #0.7 for both
# #Middle
# in5=22 #pin 15
# in6=27 #pin 13
# #0.6F, 0.9B
# #Ring
# in7=5 #pin 29
# in8=6 #pin 31
# #0.6F, 0.7B
# #Pinky
# in9=19 #pin 35
# in10=16 #pin 36
#%%
main()