This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRUNIT.py
146 lines (120 loc) · 3.36 KB
/
RUNIT.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
#LEDGAME
import RPi.GPIO as GPIO
from time import sleep
import Backend
import os
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#GPIO PINS
reda = 26
redb = 19
green = 13
redc = 6
redd = 12
buttonPin = 21
lights = [reda,redb,green,redc,redd]
sc1 = 22
sc2 = 27
sc3 = 17
sc4 = 4
scorelights = [sc1,sc2,sc3,sc4]
GPIO.setup(lights[0], GPIO.OUT)
GPIO.setup(lights[1], GPIO.OUT)
GPIO.setup(lights[2], GPIO.OUT)
GPIO.setup(lights[3], GPIO.OUT)
GPIO.setup(lights[4], GPIO.OUT)
GPIO.setup(scorelights[0], GPIO.OUT)
GPIO.setup(scorelights[1], GPIO.OUT)
GPIO.setup(scorelights[2], GPIO.OUT)
GPIO.setup(scorelights[3], GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#Game Settings
sleepTime = .1
stepCount = 0
score = 0
lives = 3
def clear():
os.system("clear")
class BreakIt(Exception): pass
def scoreboard():
clear()
print("SPEED : " + str(sleepTime))
print("SCORE : " + str(score))
print("LIVES : " + str(lives))
def switchled(who):
for i in lights:
GPIO.output(i, False)
GPIO.output(who, True)
def game():
try:
scoreboard()
while True:
#Scoreboard
if score >= 1:
GPIO.output(sc1, True)
if score >= 2:
GPIO.output(sc2, True)
if score >= 3:
GPIO.output(sc3,True)
if score >= 4:
GPIO.output(sc4,True)
if score >= 4:
print("You won")
Backend.gamewon(lights,scorelights)
sleepTime = .1
stepCount = 0
score = 0
lives = 3
#Check if player is dead
if lives <= 0:
print("Oh dear, you died")
Backend.gameover(lights)
sleep(1)
#RESET
for l in scorelights:
GPIO.output(l,False)
sleepTime = .1
stepCount = 0
score = 0
lives = 3
scoreboard()
for i in lights:
stepCount = i
switchled(i)
if not GPIO.input(buttonPin):
raise BreakIt
sleep(sleepTime)
for y in reversed(lights):
stepCount = y
switchled(y)
if not GPIO.input(buttonPin):
raise BreakIt
sleep(sleepTime)
#I DONT KNOW BUT It WORKS okay :(
except BreakIt:
switchled(stepCount)
sleep(1)
#WINNER
if stepCount == 13:
global score
score += 1
scoreboard()
Backend.winnerblink(lights)
global sleepTime
sleepTime -= .005
#LOSER
else:
global lives
lives -= 1
scoreboard()
Backend.loselife(lights)
game()
try:
while True:
game()
finally:
for l in lights:
GPIO.output(l,False)
for l in scorelights:
GPIO.output(l,False)
GPIO.cleanup()