-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
293 lines (251 loc) · 8.42 KB
/
timer.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
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
# from makerbuttons import Manager, Button,TextBox, DraggableButton, foo, MenuButton # noqa
import pyglet # noqa
import time
from pyglet.gl import * # noqa
from pyglet.window import key # noqa
from pyglet.window import mouse # noqa
import sys
def load_sound(file_name):
if file_name:
return pyglet.media.load('sounds/' + file_name, streaming=False)
return False
def play_sound(sound_file):
if sound_file:
sound = pyglet.media.Player()
sound.queue(sound_file)
sound.play() # noqa
class Button(object):
def __init__(self, upsprite, hoversprite, downsprite, x, y, callback=None, batch=None, label=None, args=None, labelbatch=None):
self.func = callback
self.upsprite = upsprite
self.downsprite = downsprite
self.hoversprite = hoversprite
self.trigger = 0
self.sprite = pyglet.sprite.Sprite(
upsprite,
x, y, batch=batch
)
if label is not None:
self.label = pyglet.text.Label(
label,
font_name='Times New Roman',
font_size=32,
x=self.sprite.x + self.sprite.width / 2,
y=self.sprite.y + self.sprite.height / 2,
anchor_x='center',
anchor_y='center',
batch=labelbatch
)
def on_mouse_motion(self, x, y, dx, dy):
if (self.sprite.x < x and
x < self.sprite.x + self.sprite.width and
self.sprite.y < y and
y < self.sprite.y + self.sprite.height):
self.sprite.image = self.hoversprite
else:
self.trigger = 0
self.sprite.image = self.upsprite
def move(self, x, y):
self.sprite.x, self.label.x = x, x
self.sprite.y, self.label.y = y, y
def on_mouse_press(self, x, y, mode):
if (self.sprite.x < x and
x < self.sprite.x + self.sprite.width and
self.sprite.y < y and
y < self.sprite.y + self.sprite.height):
if mode == 1 and self.trigger == 1:
self.trigger = 0
self.sprite.image = self.hoversprite
try:
self.do_action()
except:
pass
if mode == 0:
self.sprite.image = self.downsprite
self.trigger = 1
def do_action(self):
try:
self.func()
except:
pass
clock = 20
try:
clock = int(sys.argv[1])
except:
clock = 20
batches = [pyglet.graphics.Batch(), pyglet.graphics.Batch(), pyglet.graphics.Batch(), ]
def load_image(image, anchor=True):
try:
return image_dict(image)
except:
img = pyglet.image.load('images/' + image)
if anchor:
img.anchor_x = img.width // 2
img.anchor_y = img.height // 2
return img
fps_display = pyglet.clock.ClockDisplay()
button = load_image('button.png', False)
buttonhover = load_image('buttonhover.png', False)
buttondown = load_image('buttondown.png', False)
# sprite = pyglet.sprite.Sprite(
# button,
# 100, 100, batch=batches[0]
# )
class Game(pyglet.window.Window):
def __init__(self, height, width, batches):
super(Game, self).__init__(width, height, caption='shock_program')
self.beep = load_sound('beep.wav')
self.dorun = False
pyglet.gl.glClearColor(.5, .5, .5, 1)
self.alive = True
self.batches = batches
self.time = None
self.trial = 1
self.button = None
self.label = pyglet.text.Label(
"",
font_name='Times New Roman',
font_size=128,
x=window_width / 2,
y=window_height / 2,
anchor_x='center',
anchor_y='center',
batch=self.batches[0]
)
self.blocklabel = pyglet.text.Label(
"block 1 trial 1",
font_name='Times New Roman',
font_size=32,
x=window_width - 200,
y=window_height - 50,
anchor_x='center',
anchor_y='center',
batch=self.batches[0]
)
self.notify = None
self.setup()
def render(self, *args):
self.update()
self.clear()
for batch in self.batches:
batch.draw()
fps_display.draw()
self.flip()
def force_draw(self):
self.clear()
for batch in self.batches:
batch.draw()
fps_display.draw()
self.flip()
def make_dict(self):
pass
def update(self):
if self.dorun:
newtime = time.time()
elapsed = int(newtime - self.time)
# elapsed = int(elapsed)
# print elapsed
if clock - elapsed == 0:
play_sound(self.beep)
self.label.text = "X"
if self.trial < 5:
self.trial += 1
self.force_draw()
time.sleep(2)
self.time = time.time()
self.blocklabel.text = "block 1 trial " + str(self.trial)
else:
self.label.text = ""
self.dorun = False
self.notify_1 = pyglet.text.Label(
"Block 1 complete",
font_name='Times New Roman',
font_size=32,
x=window_width / 2,
y=window_height / 2 + 100,
anchor_x='center',
anchor_y='center',
batch=self.batches[0]
)
self.notify_2 = pyglet.text.Label(
"Notify the Experimenter",
font_name='Times New Roman',
font_size=32,
x=window_width / 2,
y=window_height / 2 + 50,
anchor_x='center',
anchor_y='center',
batch=self.batches[0]
)
self.notify_3 = pyglet.text.Label(
"Block 2 will be 50% chance shock per trial",
font_name='Times New Roman',
font_size=32,
x=window_width / 2,
y=window_height / 2 - 50,
anchor_x='center',
anchor_y='center',
batch=self.batches[0]
)
else:
self.label.text = str(clock - elapsed)
def set_time(self):
self.time = time.time()
def start(self):
self.start_label.delete()
if not self.dorun:
self.dorun = True
self.set_time()
self.button.label.delete()
self.button = None
def on_draw(self):
self.render()
def setup(self):
self.start_label = pyglet.text.Label(
"Block 1 (20% shock chance per trial)",
font_name='Times New Roman',
font_size=32,
x=window_width / 2,
y=window_height / 2 + 100,
anchor_x='center',
anchor_y='center',
batch=self.batches[0]
)
self.button = Button(
button, buttondown, buttonhover, (window_width - button.width) / 2,
100, self.start, self.batches[1], "Start", None, self.batches[2]
)
def on_close(self):
self.alive = False
def on_mouse_release(self, x, y, button, modifiers):
try:
self.button.on_mouse_press(x, y, 1)
except:
pass
def on_mouse_press(self, x, y, button, modifiers):
try:
self.button.on_mouse_press(x, y, 0)
except:
pass
def on_mouse_motion(self, x, y, dx, dy):
try:
self.button.on_mouse_motion(x, y, dx, dy)
except:
pass
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
pass
def run(self):
while self.alive:
event = self.dispatch_events()
if event:
print(event)
self.render()
window_height = 800
window_width = 1400
window = Game(window_height, window_width, batches)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
if __name__ == '__main__':
window.set_time()
pyglet.clock.set_fps_limit(10)
window.run()