-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpenaltyshot.py
353 lines (303 loc) · 12.9 KB
/
penaltyshot.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
# Penalty Kick game by Jean-Francois Gariepy, overhauled by Bill
# Broderick. This version rewritten mostly from the ground up
# in PsychoPy by John Pearson.
from __future__ import division, print_function # so that 1/3=0.333 instead of 1/3=0
import numpy as np
from psychopy import gui, visual, event, core, logging, data
from psychopy.constants import * # things like STARTED, FINISHED
from psychopy.hardware import joystick
from input_handler import JoystickServer
import physics
from datetime import datetime
import sys
import os
import json
from utils import Flicker
from settings import *
############## Paths, config, logging ######################
# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__)).decode(sys.getfilesystemencoding())
os.chdir(_thisDir)
# This is the current date and time. The date will be scrubbed from it
# before saving to only record the day time in hours, mins, secs, microseconds.
t = datetime.now()
settings['overallStartTime'] = '%d.%d.%d' % (t.hour, t.minute, t.second)
# get setup info about current session:
config = get_settings()
# Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
filename = _thisDir + os.sep + u'data' + os.sep + u'%s_%s_%s' %(config['SubjName'], 'penaltyshot', settings['overallStartTime'])
logname = _thisDir + os.sep + u'data' + os.sep + u'%s_%s_%s' %(config['SubjName'], 'penaltyshot', settings['overallStartTime'])
#save a log file for detail verbose info
logFile = logging.LogFile(logname+'.log', level=logging.EXP)
logging.console.setLevel(logging.WARNING) # this outputs to the screen, not a file
# log start time for the experiment
logging.log(level=logging.EXP, msg='Task start time: {}:{}:{}'.format(t.hour, t.minute, t.second))
# set up an object to store data as json
metadata = ({'experiment': 'penaltyshot',
'subject': config['SubjName'],
'start_time': '{}:{}:{}'.format(t.hour, t.minute, t.second),
'day': config['Day'],
'psychopy_start_time': core.getAbsTime(),
'settings': settings, 'config': config
})
# write out metadata
json_fp = open(filename+'.json', 'w', buffering=1)
json.dump(metadata, json_fp)
json_fp.write('\n')
# Remove currentDate information for privacy/identification purposes.
del t
########## Set up hardware #####################
full = config['full']
win = visual.Window(size=(800, 600), units='pix', winType = 'pygame', screen=1,
monitor='testMonitor', fullscr=full, colorSpace='rgb255',
color=(0, 0, 0))
# turn off mouse display
win.mouseVisible = False
# set up screen geometry based on window size
setup_geometry(settings, win, **config)
# Make sure one joystick is connected.
joystick.backend = 'pygame'
nJoysticks = joystick.getNumJoysticks()
logging.log(level=logging.EXP, msg='{} joysticks detected'.format(nJoysticks))
if nJoysticks == 0:
print('There is no joystick connected!')
core.quit()
else:
J0 = JoystickServer(0, settings['Joystick0_DeadZone'])
if nJoysticks > 1:
J1 = JoystickServer(1, settings['Joystick1_DeadZone'])
else:
print('You need two joysticks to play!')
core.quit()
breakTrials = []
########## Set up stims #####################
fixation = visual.TextStim(win, text='+',
alignHoriz='center',
alignVert='center', units='norm',
pos=(0, 0), height=0.3,
color=[255, 255, 255], colorSpace='rgb255',
wrapWidth=2, name='fix_cross', autoLog=True)
message_text = visual.TextStim(win, text='Your text here',
font='Helvetica', alignHoriz='center',
alignVert='center', units='norm',
pos=(0, 0), height=0.1,
color=[255, 255, 255], colorSpace='rgb255',
wrapWidth=2, name='message_text',
autoLog=True)
line = visual.Line(win, start=(settings['FinalLine'],
settings['FinalLineHalfHeight']),
end=(settings['FinalLine'],
-settings['FinalLineHalfHeight']),
name='goal_line')
ball = visual.Circle(win, radius=settings['BallRadius'], fillColor='gray',
lineColor='gray', pos=(settings['BallStartingPosX'],
settings['BallStartingPosY']), name='ball')
barVertices = [ [-settings['BarWidth']/2., settings['BarLength']/2.],
[settings['BarWidth']/2., settings['BarLength']/2.],
[settings['BarWidth']/2., -settings['BarLength']/2.],
[-settings['BarWidth']/2., -settings['BarLength']/2.] ]
bar = visual.ShapeStim(win,vertices=barVertices,fillColor='gray',
lineColor='gray', pos=(settings['BarStartingPosX'],
settings['BarStartingPosY']), name='bar')
# set up photodiode trigger
trigger = Flicker(win)
############# finalize setup ###############
# log all settings
logging.log(level=logging.EXP, msg='settings = {}'.format(repr(settings)))
# write out everything logged so far
logging.flush()
# Create some handy timers
globalClock = core.Clock() # to track the time since experiment started
trialClock = core.Clock() # time within trial
playClock = core.Clock() # time within trial
############# prepare to start main experiment loop ###############
endExpNow = False # flag for 'escape' or other condition => quit the exp
thisTrial = 0
logging.log(level=logging.EXP, msg='Starting task')
metadata['task_start_time'] = globalClock.getTime()
#new block logic--subject will always be the shooter
ball.joystick = J0
bar.joystick = J1
#if ball.joystick is J0:
jmsg = 'Joysticks: Ball = 0, Bar = 1'
#else:
# jmsg = 'Joysticks: Ball = 1, Bar = 0'
logging.log(level=logging.EXP, msg=jmsg)
while not endExpNow: # main experiment loop
###### set up for trial ############
thisTrial += 1
logging.log(level=logging.EXP, msg='Start trial {}'.format(thisTrial))
# sentinel variables for task state
endTrialNow = False # flag for escape from trial
winner = None # has the trial completed
playOn = False # has play commenced
blockSwitch = False
showMessage = False
# old block logic
#if thisTrial % config['trials_in_block'] == 1:
# if thisTrial == 1:
# # assign joysticks
# ball.joystick = J0
# bar.joystick = J1
# else:
# blockSwitch = True
# # swap joysticks
# J = ball.joystick
# ball.joystick = bar.joystick
# bar.joystick = J
# del J
# log it
# reset stims
trialComponents = [fixation, message_text, ball, bar, line]
for thisComponent in trialComponents:
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# setup stim timing
# have to reset these to None because they don't necessarily
# get set during trial
tTrialStart = None
tMsgOn = None
tMsgOff = None
tFixOn = None
tFixOff = None
tPlayStart = None
tPlayEnd = None
tTrialEnd = None
if blockSwitch:
showMessage = True
msgStart = 0.0
msgTime = settings['BlockMessageTime']
message_text.setText('Switch roles!', log=False)
fixStart = msgStart + msgTime
else:
fixStart = 0.0
fixTime = settings['FixCrossJitterMean']
playStart = fixStart + fixTime
outcomeOverTime = np.inf
# reset players
ball.setPos((settings['BallStartingPosX'], settings['BallStartingPosY']), log=False)
ball.history = []
ball.jhistory = []
bar.setPos((settings['BarStartingPosX'], settings['BarStartingPosY']), log=False)
bar.history = []
bar.jhistory = []
bar.accel = []
bar.maxmove = []
# reset clocks
t = 0 # time in trial
frameN = -1 # frame within trial
tTrialStart = globalClock.getTime()
trialClock.reset() # reset trial clock
###### end trial setup ###############
while not endTrialNow: # trial loop
global_time = globalClock.getTime() # current experiment time
t = trialClock.getTime() # current trial time
frameN += 1 # increment frame number
# handle keyboard input
theseKeys = event.getKeys(keyList=['escape','space'])
# check for quit:
if 'escape' in theseKeys:
endTrialNow = True
endExpNow = True
# clear event buffer
event.clearEvents()
# check for requested break:
if 'space' in theseKeys:
event.waitKeys()
logging.log(level=logging.EXP, msg='Sub needed break on trial {}'.format(thisTrial))
endExpNow = False
breakTrials.append(thisTrial)
#endTrialNow = True
# update message
if showMessage and t >= msgStart and message_text.status == NOT_STARTED:
message_text.setAutoDraw(True, log=False)
tMsgOn = global_time
logging.log(level=logging.EXP, msg='Message on')
trigger.flicker(1)
if message_text.status == STARTED and t >= (msgStart + (fixTime - win.monitorFramePeriod*0.75)): #most of one frame period left
message_text.setAutoDraw(False, log=False)
showMessage = False
tMsgOff = global_time
logging.log(level=logging.EXP, msg='Message off')
# update fixation cross
if t >= fixStart and fixation.status == NOT_STARTED:
fixation.setAutoDraw(True, log=False)
tFixOn = global_time
logging.log(level=logging.EXP, msg='Fixation on')
trigger.flicker(1)
if fixation.status == STARTED and t >= (fixStart + (fixTime - win.monitorFramePeriod*0.75)): #most of one frame period left
fixation.setAutoDraw(False, log=False)
tFixOff = global_time
logging.log(level=logging.EXP, msg='Fixation off')
# update other stims
if t >= playStart and ball.status == NOT_STARTED:
ball.setAutoDraw(True, log=False)
bar.setAutoDraw(True, log=False)
line.setAutoDraw(True, log=False)
tPlayStart = global_time
logging.log(level=logging.EXP, msg='Start play')
trigger.flicker(4) # mark start of play; synced to playClock
playOn = True
playClock.reset()
# handle actual game play
if playOn:
tt = playClock.getTime()
physics.update_bar(global_time, tt, bar, settings)
physics.update_ball(global_time, tt, ball, settings)
# check outcome
winner = physics.check_outcome(ball, bar, settings)
# conclusion of play
if winner and playOn:
playOn = False
tPlayEnd = global_time
logging.log(level=logging.EXP, msg='End play')
# start of outcome period
trigger.flicker(16)
outcomeOverTime = t + settings['TimeToWaitAfterOutcome']
# end of outcome period
if t > outcomeOverTime:
# trial stim teardown
ball.setAutoDraw(False, log=False)
bar.setAutoDraw(False, log=False)
line.setAutoDraw(False, log=False)
endTrialNow = True
# update screen
win.flip()
# clean up after trial
tTrialEnd = global_time
logging.log(level=logging.EXP, msg='End trial {}'.format(thisTrial))
logging.flush()
# save events to data object
this_dat = ({'ball_history': ball.history,
'ball_joystick_history': ball.jhistory,
'bar_history': bar.history,
'bar_joystick_history': bar.jhistory,
'bar_acceleration': bar.accel,
'bar_max_move': bar.maxmove,
'breakTrials' : breakTrials,
'winner': winner,
'times': ({'trial_start': tTrialStart,
'message_on': tMsgOn,
'message_off': tMsgOff,
'fixation_on': tFixOn,
'fixation_off': tFixOff,
'play_start': tPlayStart,
'play_end': tPlayEnd,
'trial_end': tTrialEnd
})
})
json.dump(this_dat, json_fp) # dump to json
json_fp.write('\n') # write newline to flush buffer
event.clearEvents()
# clean up after task
logging.log(level=logging.EXP, msg='Ending task')
# log end time for the experiment
t = datetime.now()
logging.log(level=logging.EXP, msg='Task finish time: {}:{}:{}'.format(t.hour, t.minute, t.second))
logging.flush()
# close out data object
metadata['psychopy_end_time'] = core.getAbsTime()
metadata['task_end_time'] = globalClock.getTime()
metadata['end_time'] = '{}:{}:{}'.format(t.hour, t.minute, t.second)
# re-dump metadata with end times included
json.dump(metadata, json_fp)