This repository has been archived by the owner on Jan 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
1Reaction.py
174 lines (129 loc) · 3.87 KB
/
1Reaction.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
import pygame, random, datetime
import time as ti
from pygame.locals import *
from astro_pi import AstroPi
ap = AstroPi()
pygame.init()
pygame.display.set_mode((640, 480))
def handle_event(event):
if event.key == pygame.K_DOWN:
return "DOWN"
elif event.key == pygame.K_UP:
return "UP"
elif event.key == pygame.K_LEFT:
return "LEFT"
elif event.key == pygame.K_RIGHT:
return "RIGHT"
elif event.key == pygame.K_RETURN:
return "RETURN"
#Gets a joystick input with optional timeout
def joystick(timeout=-1):
running = True
startTime = datetime.datetime.now()
while running:
for event in pygame.event.get():
if event.type == KEYUP:
jPressed = handle_event(event)
running = False
if timeout != -1:
endTime = datetime.datetime.now()
totalTime = endTime - startTime
if totalTime.seconds >= timeout:
return "NONE"
return jPressed
def countdown():
colour = [100,0,0]
ap.clear()
ap.show_message("3 2 1",0.125, colour)
randomWait = random.randint(0,5)
if joystick(randomWait) != "NONE":
return True
else:
return False
#GAMES
def reaction():
global direction
b = [0,0,0]
p = [0,100,100]
UP = [b,b,b,p,p,b,b,b,
b,b,p,p,p,p,b,b,
b,p,p,p,p,p,p,b,
b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b]
DOWN=[b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b,
b,b,b,p,p,b,b,b,
b,p,p,p,p,p,p,b,
b,b,p,p,p,p,b,b,
b,b,b,p,p,b,b,b]
LEFT=[b,b,b,b,b,b,b,b,
b,b,p,b,b,b,b,b,
b,p,p,b,b,b,b,b,
p,p,p,p,p,p,p,p,
p,p,p,p,p,p,p,p,
b,p,p,b,b,b,b,b,
b,b,p,b,b,b,b,b,
b,b,b,b,b,b,b,b]
RIGHT=[b,b,b,b,b,b,b,b,
b,b,b,b,b,p,b,b,
b,b,b,b,b,p,p,b,
p,p,p,p,p,p,p,p,
p,p,p,p,p,p,p,p,
b,b,b,b,b,p,p,b,
b,b,b,b,b,p,b,b,
b,b,b,b,b,b,b,b]
direction = random.randint(0,3)
startTime = datetime.datetime.now()
notPressed = True
if direction == 0:
ap.set_pixels(UP)
elif direction == 1:
ap.set_pixels(DOWN)
elif direction == 2:
ap.set_pixels(LEFT)
else:
ap.set_pixels(RIGHT)
while notPressed:
pressed = joystick()
if direction == 0:
if pressed == "UP":
direction = "UP"
notPressed = False
elif direction == 1:
if pressed == "DOWN":
direction = "DOWN"
notPressed = False
elif direction == 2:
if pressed == "LEFT":
direction = "LEFT"
notPressed = False
elif direction == 3:
if pressed == "RIGHT":
direction = "RIGHT"
notPressed = False
endTime = datetime.datetime.now()
totalTime = endTime - startTime
timeTaken = (totalTime.seconds * 1000000) + totalTime.microseconds
print(timeTaken/1000,"miliseconds")
return timeTaken/1000
for i in range(3):
playing = True
while playing:
cheat = countdown()
if not cheat:
time = reaction()
ap.show_message(str(time), text_colour=[100,0,0])
playing = False
else:
ap.show_message("Too Quick")
playing = True
fileObject = open("1Reaction/data.txt","a")
toWrite = "\n"+str(datetime.datetime.now())+","+str(time)+","+direction
fileObject.write(toWrite)
fileObject.close()
pygame.quit()