-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.py
More file actions
155 lines (111 loc) · 2.42 KB
/
Copy pathgame.py
File metadata and controls
155 lines (111 loc) · 2.42 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
import RPi.GPIO as GPIO
import time
leds = [40,38,36,37,35,33,31,32,29,11,12,13,15]
score_led = 31 #score indicating led
target_p1 = 15 #player 1 target led
target_p2 = 40 #player 2 target led
b1 = 16
b2 = 18
GPIO.setmode(GPIO.BOARD)
for led in leds:
GPIO.setup(led,GPIO.OUT)
GPIO.output(led,GPIO.LOW)
GPIO.setup(b1,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(b2,GPIO.IN, pull_up_down=GPIO.PUD_UP)
p1_score = 0
p2_score = 0
game = False
main_menu = True
game_over = False
def victory(player):
for i in range(20):
GPIO.output(player, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(player, GPIO.LOW)
time.sleep(0.1)
def score(loop):
global score_led
start = 0
while start < loop:
GPIO.output(score_led, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(score_led, GPIO.LOW)
time.sleep(0.2)
start+=1
def warning():
pass
def key_press():
b1_state = GPIO.input(b1)
b2_state = GPIO.input(b2)
if b1_state == False and b2_state == False:
return "multi"
if b1_state == False:
return "single_p1"
if b2_state == False:
return "single_p2"
def runner(timing):
global main_menu
global game
global game_over
global p1_score
global p2_score
global target_p1
global target_p2
for led in leds:
state = key_press()
GPIO.output(led,GPIO.HIGH)
time.sleep(timing)
GPIO.output(led,GPIO.LOW)
if main_menu == True:
if state == "multi":
main_menu = False
game = True
break
if game == True:
if state == "single_p1":
GPIO.output(led,GPIO.HIGH)
time.sleep(1.5)
GPIO.output(led,GPIO.LOW)
if led == target_p1 and p1_score < 5:
print(p1_score)
p1_score+=1
score(p1_score)
if p1_score == 5:
game = False
game_over = True
if state == "single_p2":
GPIO.output(led,GPIO.HIGH)
time.sleep(1.5)
GPIO.output(led,GPIO.LOW)
if led == target_p2 and p2_score < 5:
p2_score+=1
score(p2_score)
if p2_score == 5:
game = False
game_over = True
leds.reverse()
try:
#global p1_score
#global p2_score
while True:
while main_menu == True:
runner(0.5)
#print(b1_state)
#print(b2_state)
while game_over == True:
state = key_press()
if p1_score == 5:
victory(target_p2)
if p2_score ==5:
victory(target_p1)
p1_score = 0
p2_score = 0
game_over = False
game = False
main_menu = True
while game == True:
runner(0.03)
finally:
#GPIO.setup(15,GPIO.OUT)
#GPIO.output(15,GPIO.HIGH)
GPIO.cleanup()