/
whack_a_mole.py
executable file
·602 lines (472 loc) · 23.7 KB
/
whack_a_mole.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
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
#!/usr/bin/env python
from __future__ import division
import sys, os, random
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),"pyviewx.client"))
sys.path.insert(1, os.path.join(os.path.dirname(os.path.realpath(__file__)),"pyviewx.pygame"))
from pyglet import resource, clock, text
from pyglet.gl import *
from pyglet.window import key
from pyglet.media import Player, StaticSource
from cocos.director import *
from cocos.menu import *
from cocos.layer import *
from cocos.text import *
from cocos.scenes.transitions import *
from cocos.sprite import Sprite
from cocos.tiles import RectMapLayer
from cocos.actions.instant_actions import CallFunc
from cocos.actions.interval_actions import RandomDelay, Delay, MoveTo, AccelDeccel, FadeTo, Accelerate
from cocos.collision_model import CollisionManagerBruteForce, AARectShape
from primitives import Polygon
from odict import OrderedDict
import pygletreactor
pygletreactor.install()
from twisted.internet import reactor
from pyviewx.client import iViewXClient, Dispatcher
from calibrator import CalibrationLayer
from menu import BetterMenu, GhostMenuItem, BetterEntryMenuItem
from handler import DefaultHandler
from scene import Scene
from pycogworks.logging import getDateTimeStamp
class OptionsMenu(BetterMenu):
def __init__(self):
super(OptionsMenu, self).__init__('Options')
self.screen = director.get_window_size()
ratio = self.screen[1] / self.screen[0]
self.font_title['font_size'] = self.screen[0] / 18
self.font_title['color'] = (255, 255, 255, 255)
self.font_item['color'] = (255, 255, 255, 255)
self.font_item['font_size'] = self.screen[1] / 16 * ratio
self.font_item_selected['color'] = (0, 0, 255, 255)
self.font_item_selected['font_size'] = self.screen[1] / 16 * ratio
self.items = OrderedDict()
self.speed_opts = ['SLOW','FAST']
self.level_opts = ['NONE','WEAK','MEDIUM','STRONG']
self.eye_opts = ['NONE','RIGHT','LEFT']
self.points_opts = ['2','5','9','13']
self.items['overlay'] = ToggleMenuItem('Debug Overlay:', self.on_overlay, director.settings['overlay'])
self.items['eyetracker'] = ToggleMenuItem('EyeTracker:', self.on_eyetracker, director.settings['eyetracker'])
self.items['eyetracker_ip'] = EntryMenuItem('EyeTracker IP:', self.on_eyetracker_ip, director.settings['eyetracker_ip'])
self.items['eyetracker_in_port'] = EntryMenuItem('EyeTracker In Port:', self.on_eyetracker_in_port, director.settings['eyetracker_in_port'])
self.items['eyetracker_out_port'] = EntryMenuItem('EyeTracker Out Port:', self.on_eyetracker_out_port, director.settings['eyetracker_out_port'])
self.items['calibration_points'] = MultipleMenuItem('Calibration Points:', self.on_cal_points, self.points_opts, self.points_opts.index(str(director.settings['calibration_points'])))
self.items['calibration_eye'] = MultipleMenuItem('Calibration Eye:', self.on_cal_eye, self.eye_opts, director.settings['calibration_eye'])
self.items['calibration_level'] = MultipleMenuItem('Calibration Check Level:', self.on_cal_level, self.level_opts, director.settings['calibration_level'])
self.items['calibration_speed'] = MultipleMenuItem('Calibration Speed:', self.on_cal_speed, self.speed_opts, director.settings['calibration_speed'])
self.items['calibration_auto'] = ToggleMenuItem('Calibration Auto-accept:', self.on_cal_auto, director.settings['calibration_auto'])
self.items['calibration_wait'] = ToggleMenuItem('Calibration Wait For Good:', self.on_cal_wait, director.settings['calibration_wait'])
self.items['calibration_random'] = ToggleMenuItem('Calibration Randomize:', self.on_cal_random, director.settings['calibration_random'])
self.create_menu(self.items.values(), zoom_in(), zoom_out())
def on_overlay(self, value):
director.settings['overlay'] = value
def on_eyetracker(self, value):
director.settings['eyetracker'] = value
def on_cal_points(self, value):
director.settings['calibration_points'] = int(self.points_opts[value])
def on_cal_eye(self, value):
director.settings['calibration_speed'] = value
def on_cal_level(self, value):
director.settings['calibration_speed'] = value
def on_cal_speed(self, value):
director.settings['calibration_speed'] = value
def on_cal_auto(self, value):
director.settings['calibration_auto'] = value
def on_cal_wait(self, value):
director.settings['calibration_wait'] = value
def on_cal_random(self, value):
director.settings['calibration_random'] = value
def on_enter(self):
super(OptionsMenu, self).on_enter()
self.orig_values = (director.settings['eyetracker_ip'],
director.settings['eyetracker_in_port'],
director.settings['eyetracker_out_port'])
def on_exit(self):
super(OptionsMenu, self).on_exit()
new_values = (director.settings['eyetracker_ip'],
director.settings['eyetracker_in_port'],
director.settings['eyetracker_out_port'])
if new_values != self.orig_values:
director.scene.dispatch_event("eyetracker_info_changed")
def on_eyetracker(self, value):
director.settings['eyetracker'] = value
def on_eyetracker_ip(self, ip):
director.settings['eyetracker_ip'] = ip
def on_eyetracker_in_port(self, port):
director.settings['eyetracker_in_port'] = port
def on_eyetracker_out_port(self, port):
director.settings['eyetracker_out_port'] = port
def on_quit(self):
self.parent.switch_to(0)
class MainMenu(BetterMenu):
def __init__(self):
super(MainMenu, self).__init__("Whack A Mole")
self.screen = director.get_window_size()
ratio = self.screen[1] / self.screen[0]
self.font_title['font_size'] = self.screen[0] / 18
self.font_title['color'] = (255, 255, 255, 255)
self.font_item['color'] = (255, 255, 255, 255)
self.font_item['font_size'] = self.screen[1] / 16 * ratio
self.font_item_selected['color'] = (0, 0, 255, 255)
self.font_item_selected['font_size'] = self.screen[1] / 16 * ratio
self.menu_anchor_y = 'center'
self.menu_anchor_x = 'center'
self.items = OrderedDict()
self.items['start'] = MenuItem('Start', self.on_start)
self.items['options'] = MenuItem('Options', self.on_options)
self.items['quit'] = MenuItem('Quit', self.on_quit)
self.create_menu(self.items.values(), zoom_in(), zoom_out())
def on_options(self):
self.parent.switch_to(1)
def on_start(self):
filebase = "WhackAMole_%s" % (getDateTimeStamp())
director.settings['filebase'] = filebase
director.scene.dispatch_event('start_task')
def on_quit(self):
reactor.callFromThread(reactor.stop)
class Mole(Sprite, AARectShape):
def __init__(self, images, x, y):
self.images = images
super(Mole, self).__init__(self.images[0], anchor=(0,0), position=(x,y))
self.position_out = self.position
self.position_in = (self.position[0],self.position[1]-130)
self.position = self.position_in
self.bottom = self.position_in[1] + 200
self.cshape = self
self.active = False
self.player_thump = StaticSource(pyglet.resource.media('ow.mp3'))
self.player_laugh = StaticSource(pyglet.resource.media('laugh.mp3'))
self.STATE_IDLE = 0
self.STATE_HIT = 1
self.STATE_MISS = 2
self.state = self.STATE_IDLE
def __getattribute__(self,name):
if name=='center':
return (self.position[0]+89, (self.position[1]+self.bottom+200)/2 - 20)
elif name=='rx':
return 89
elif name=='ry':
return (self.position[1]+200-self.bottom)/2 + 20
else:
return object.__getattribute__(self, name)
def setImage(self, index):
self.image = self.images[index]
def setImageFunc(self, index):
return CallFunc(self.setImage, index)
def checkHitState(self):
if self.state == self.STATE_IDLE:
self.laugh()
self.state = self.STATE_MISS
def setActive(self, value):
self.active = value
def setState(self, value):
self.state = value
def waitDone(self):
self.checkHitState()
self.do(AccelDeccel(MoveTo(self.position_in, self.t)) + CallFunc(self.setActive, False) + CallFunc(self.setState, 0))
def upDone(self):
self.do(RandomDelay(.25,.5) + CallFunc(self.waitDone))
def up(self):
self.t = random.uniform(.25,1.0)
self.setImage(0)
self.position = self.position_in
self.setActive(True)
self.do(RandomDelay(0.0,0.2) + AccelDeccel(MoveTo(self.position_out, self.t)) + CallFunc(self.upDone))
def laugh(self):
self.player_laugh.play()
self.do(self.setImageFunc(1)+Delay(.1)+
self.setImageFunc(2)+Delay(.1)+
self.setImageFunc(3)+Delay(.1)+
self.setImageFunc(2)+Delay(.1)+
self.setImageFunc(3)+Delay(.1)+
self.setImageFunc(1)+Delay(.1))
def down_fast(self):
self.do(Accelerate(MoveTo(self.position_in, self.t/2)) + CallFunc(self.setActive, False) + CallFunc(self.setState, 0))
def thump(self):
self.player_thump.play()
self.stop()
self.down_fast()
self.do(self.setImageFunc(4)+Delay(.1)+
self.setImageFunc(5)+Delay(.1)+
self.setImageFunc(6)+Delay(.1)+
self.setImageFunc(4)+Delay(.1)+
self.setImageFunc(5)+Delay(.1)+
self.setImageFunc(6)+Delay(.1))
class Task(ColorLayer, pyglet.event.EventDispatcher):
d = Dispatcher()
states = ["INIT", "CALIBRATE", "IGNORE_INPUT", "TASK", "DONE"]
STATE_INIT = 0
STATE_CALIBRATE = 1
STATE_IGNORE_INPUT = 2
STATE_TASK = 3
STATE_DONE = 4
BG_TILE_SIZE = (1024,128)
FG_TILE_SIZE = (1024,128)
is_event_handler = True
def __init__(self, client=None):
self.screen = director.get_window_size()
super(Task, self).__init__(0, 0, 0, 255, 1024, int(768*1.1))
self.client = client
self.state = self.STATE_INIT
self.mole_images = [resource.image('mole_1.png'),
resource.image('mole_laugh1.png'),
resource.image('mole_laugh2.png'),
resource.image('mole_laugh3.png'),
resource.image('mole_thump1.png'),
resource.image('mole_thump2.png'),
resource.image('mole_thump3.png'),
resource.image('mole_thump4.png')]
self.music = Player()
self.music.queue(pyglet.resource.media('whack.mp3'))
self.music.eos_action = 'loop'
self.cm = CollisionManagerBruteForce()
self.moles = []
for i in range(0,3):
mole = Mole(self.mole_images, (112 + (i * 310)), (127-80))
self.moles.append(mole)
self.add(mole, 55)
self.cm.add(mole)
for i in range(3,6):
mole = Mole(self.mole_images, (112 + ((i-3) * 310)), (383-80))
self.moles.append(mole)
self.add(mole, 35)
self.cm.add(mole)
for i in range(6,9):
mole = Mole(self.mole_images, (112 + ((i-6) * 310)), (639-80))
self.moles.append(mole)
self.add(mole, 15)
self.cm.add(mole)
self.text_batch = BatchNode()
self.add(self.text_batch, z=100)
self.score = 0
scorey = int((768 + int(768*1.1))/2)
self.score_num = text.Label("%06d" % self.score, font_name="Score Board", font_size=48, x=512, y=scorey, color=(255, 255, 255, 255),
anchor_x='center', anchor_y='center', batch=self.text_batch.batch)
self.fix = Label('G', font_name='Cut Outs for 3D FX', font_size=48,
position=(0,0), color=(255, 0, 0, 192), anchor_x='center', anchor_y='center')
self.fix.visible = False
self.add(self.fix, z=100000)
self.add(Sprite(resource.image('bg_dirt128.png'), anchor=(0,0), position=(0,0)))
self.add(Sprite(resource.image('bg_dirt128.png'), anchor=(0,0), position=(0,128)))
self.add(Sprite(resource.image('bg_dirt128.png'), anchor=(0,0), position=(0,256)))
self.add(Sprite(resource.image('bg_dirt128.png'), anchor=(0,0), position=(0,384)))
self.add(Sprite(resource.image('bg_dirt128.png'), anchor=(0,0), position=(0,512)))
self.add(Sprite(resource.image('bg_dirt128.png'), anchor=(0,0), position=(0,640)))
self.add(Sprite(resource.image('grass_lower128.png'), anchor=(0,0), position=(0,0)), z=60)
self.add(Sprite(resource.image('grass_upper128.png'), anchor=(0,0), position=(0,128)), z=50)
self.add(Sprite(resource.image('grass_lower128.png'), anchor=(0,0), position=(0,256)), z=40)
self.add(Sprite(resource.image('grass_upper128.png'), anchor=(0,0), position=(0,384)), z=30)
self.add(Sprite(resource.image('grass_lower128.png'), anchor=(0,0), position=(0,512)), z=20)
self.add(Sprite(resource.image('grass_upper128.png'), anchor=(0,0), position=(0,640)), z=10)
def set_score(self):
self.score_num.begin_update()
self.score_num.text = "%06d" % self.score
self.score_num.end_update()
def visit(self):
super(Task, self).visit()
if director.settings["overlay"]:
for mole in self.moles:
if mole.opacity > -1:
if mole.state == 1:
color = (.3,0.0,0.0,.7)
else:
color = (.3,0.2,0.5,.7)
p = Polygon([(mole.center[0]-mole.rx, mole.center[1]-mole.ry),
(mole.center[0]+mole.rx, mole.center[1]-mole.ry),
(mole.center[0]+mole.rx, mole.center[1]+mole.ry),
(mole.center[0]-mole.rx, mole.center[1]+mole.ry)],
color=color)
p.render()
def mole_up(self):
moles = [mole for mole in self.moles if not mole.active]
if moles:
random.choice(moles).up()
def animate(self, dt):
self.mole_up()
if random.randint(0, 3) == 0:
self.mole_up()
def game_over(self):
self.state = self.STATE_GAME_OVER
def clear(self):
self.music.pause()
self.music.seek(0)
for mole in self.moles:
mole.active = False
mole.state = 0
mole.position = mole.position_in
pyglet.clock.unschedule(self.animate)
if self.client:
self.client.removeDispatcher(self.d)
self.client.stopDataStreaming()
self.client.stopFixationProcessing()
def reset(self):
self.clear()
if self.client:
self.client.addDispatcher(self.d)
#self.client.setDataFormat('%TS %ET %SX %SY %DX %DY %EX %EY %EZ')
#self.client.startDataStreaming()
self.client.startFixationProcessing()
self.state = self.STATE_TASK
self.score = 0
pyglet.clock.schedule_interval(self.animate, .5)
self.music.play()
def on_enter(self):
if isinstance(director.scene, TransitionScene): return
super(Task, self).on_enter()
if director.settings['eyetracker']:
self.state = self.STATE_CALIBRATE
self.dispatch_event("start_calibration", self.calibration_ok, self.calibration_bad)
else:
self.reset()
def calibration_ok(self):
self.dispatch_event("stop_calibration")
self.reset()
def calibration_bad(self):
self.dispatch_event("stop_calibration")
director.scene.dispatch_event("show_intro_scene")
def on_exit(self):
if isinstance(director.scene, TransitionScene): return
super(Task, self).on_exit()
self.clear()
@d.listen('ET_FIX')
def iViewXEvent(self, inResponse):
if inResponse[0] == 'l':
x = int(float(inResponse[2]))
y = int(float(inResponse[3]))
if director.settings["overlay"]:
self.fix.position = (x, y)
self.fix.visible = True
self.handle_mouse_press(x, y)
@d.listen('ET_EFX')
def iViewXEvent(self, inResponse):
if inResponse[0] == 'l':
if director.settings["overlay"]:
self.fix.visible = False
@d.listen('ET_SPL')
def iViewXEvent(self, inResponse):
pass
def handle_mouse_press(self, x, y):
for obj in self.cm.objs_touching_point(x, y):
if obj.active:
if obj.state == 0:
obj.state = 1
obj.thump()
self.score += 10
elif obj.state == 2:
self.score -= 10
self.set_score()
def on_mouse_press(self, x, y, buttons, modifiers):
posx, posy = director.get_virtual_coordinates(x, y)
self.handle_mouse_press(posx, posy)
def on_mouse_motion(self, x, y, dx, dy):
pass
def on_key_press(self, symbol, modifiers):
if self.state < self.STATE_IGNORE_INPUT: return
if symbol == key.W and (modifiers & key.MOD_ACCEL):
director.scene.dispatch_event("show_intro_scene")
True
if self.state == self.STATE_TASK:
pass
elif self.state == self.STATE_DONE:
pass
class EyetrackerScrim(ColorLayer):
def __init__(self):
self.screen = director.get_window_size()
super(EyetrackerScrim, self).__init__(0, 0, 0, 224, self.screen[0], self.screen[1])
l = Label("Reconnecting to eyetracker...", position=(self.screen[0] / 2, self.screen[1] / 2), font_name='', font_size=32, bold=True, color=(255, 255, 255, 255), anchor_x='center', anchor_y='center')
self.add(l)
class WhackAMole(object):
title = "Whack A Mole"
def __init__(self):
if not os.path.exists("data"): os.mkdir("data")
pyglet.resource.path.append('resources')
pyglet.resource.reindex()
pyglet.resource.add_font('cutouts.ttf')
pyglet.resource.add_font('scoreboard.ttf')
director.init(width=int(1024*1.0), height=int(768*1.1),
caption=self.title, visible=False, resizable=True)
director.window.set_size(int(1024*1.0), int(768*1.1))
director.window.pop_handlers()
director.window.push_handlers(DefaultHandler())
director.settings = {'overlay': False,
'eyetracker': True,
'eyetracker_ip': '127.0.0.1',
'eyetracker_out_port': '4444',
'eyetracker_in_port': '5555',
'calibration_speed': 1,
'calibration_wait': 1,
'calibration_random': 1,
'calibration_level': 3,
'calibration_auto': 1,
'calibration_points': 2,
'calibration_eye': 0
}
self.client = None
self.client = iViewXClient(director.settings['eyetracker_ip'], int(director.settings['eyetracker_out_port']))
self.listener = reactor.listenUDP(int(director.settings['eyetracker_in_port']), self.client)
director.set_show_FPS(False)
director.window.set_fullscreen(False)
if director.settings['eyetracker']:
director.window.set_mouse_visible(False)
else:
director.window.set_mouse_visible(True)
# Intro scene and its layers
self.introScene = Scene()
self.mainMenu = MainMenu()
self.optionsMenu = OptionsMenu()
self.eyetrackerScrim = EyetrackerScrim()
self.mplxLayer = MultiplexLayer(self.mainMenu, self.optionsMenu)
self.introScene.add(self.mplxLayer, 1)
self.introScene.register_event_type('start_task')
self.introScene.register_event_type('eyetracker_info_changed')
self.introScene.push_handlers(self)
# Task scene and its layers
self.taskScene = Scene()
self.taskLayer = Task(self.client)
self.calibrationLayer = CalibrationLayer(self.client)
self.calibrationLayer.register_event_type('show_headposition')
self.calibrationLayer.register_event_type('hide_headposition')
self.calibrationLayer.push_handlers(self)
self.taskLayer.register_event_type('new_trial')
self.taskLayer.register_event_type('start_calibration')
self.taskLayer.register_event_type('stop_calibration')
self.taskLayer.push_handlers(self)
self.taskScene.add(self.taskLayer, 1)
self.taskScene.register_event_type('show_intro_scene')
self.taskScene.push_handlers(self)
director.window.set_visible(True)
def start_calibration(self, on_success, on_failure):
self.calibrationLayer.on_success = on_success
self.calibrationLayer.on_failure = on_failure
self.calibrationLayer.points = director.settings['calibration_points']
self.calibrationLayer.eye = director.settings['calibration_eye']
self.calibrationLayer.level = director.settings['calibration_level']
self.calibrationLayer.speed = director.settings['calibration_speed']
self.calibrationLayer.auto = director.settings['calibration_auto']
self.calibrationLayer.wait = director.settings['calibration_wait']
self.calibrationLayer.random = director.settings['calibration_random']
self.taskScene.add(self.calibrationLayer, 2)
def stop_calibration(self):
self.taskScene.remove(self.calibrationLayer)
def eyetracker_listen(self, _):
self.listener = reactor.listenUDP(int(director.settings['eyetracker_in_port']), self.client)
self.introScene.remove(self.eyetrackerScrim)
self.introScene.enable_handlers(True)
def eyetracker_info_changed(self):
if self.client.remoteHost != director.settings['eyetracker_ip'] or \
self.client.remotePort != int(director.settings['eyetracker_out_port']):
self.client.remoteHost = director.settings['eyetracker_ip']
self.client.remotePort = int(director.settings['eyetracker_out_port'])
else:
self.introScene.add(self.eyetrackerScrim, 2)
self.introScene.enable_handlers(False)
d = self.listener.stopListening()
d.addCallback(self.eyetracker_listen)
def show_intro_scene(self):
self.mplxLayer.switch_to(0)
director.replace(self.introScene)
def start_task(self):
director.replace(SplitRowsTransition(self.taskScene))
if __name__ == '__main__':
cal = WhackAMole()
cal.show_intro_scene()
reactor.run()