def present_trial(objects, probabilities, trial,i, writer): start_time = time() in_right = False in_left = False left_box = TobiiSprite( screen, spot.c1, kstimulus("misc/box.png"), tobii_controller, scale=BOX_SCALE) left_boxfront = TobiiSprite(screen, spot.c1, kstimulus("misc/boxfront.png"), tobii_controller,scale=BOX_SCALE) left_lid = TobiiSprite( screen, LID_SPOT1, kstimulus("misc/lid.png"), tobii_controller,scale=BOX_SCALE) right_box = TobiiSprite(screen, spot.c4, kstimulus("misc/box.png"),tobii_controller, scale=BOX_SCALE) right_boxfront = TobiiSprite(screen, spot.c4, kstimulus("misc/boxfront.png"), tobii_controller,scale=BOX_SCALE) right_lid = TobiiSprite(screen, LID_SPOT2, kstimulus("misc/lid.png"),tobii_controller, scale=BOX_SCALE) left_object = TobiiSprite( screen, spot.c1, objects[0],tobii_controller, scale=IMAGE_SCALE) right_object = TobiiSprite(screen, spot.c4, objects[1],tobii_controller, scale=IMAGE_SCALE) #the boxes keep opening every when_open seconds, and object appearance is stochastic # A queue of animation operations Q = DisplayQueue() Q.append(obj=left_lid, action='wait', duration=1) Q.append(obj=right_lid, action='wait', duration=1) Q.append_simultaneous(obj=left_lid, action = 'move', pos=left_lid_MOVE, duration=0.25) Q.append_simultaneous(obj=right_lid, action='move', pos=right_lid_MOVE, duration=0.25) #with certain probability, reveal object: flip1 = random.random() if flip1 < probabilities[0]: Q.append_simultaneous(obj=left_object, action='move', pos=spot.b1, duration=.5) in_left = True #with other probability, reveal object flip2 = random.random() if flip2 < probabilities[1]: Q.append_simultaneous(obj=right_object, action='move', pos=spot.b4, duration=.5) in_right = True Q.append(obj=left_object, action='wait', duration=.25) Q.append(obj=right_object, action='wait', duration=.25) Q.append(obj=left_lid, action='wait', duration=.25) Q.append(obj=right_lid, action='wait', duration=.25) Q.append_simultaneous(obj=left_object, action='move', pos=spot.c1, duration=.5) Q.append_simultaneous(obj=right_object, action='move', pos=spot.c4, duration=.5) Q.append_simultaneous(obj=left_lid, action='move', pos=LID_SPOT1,duration=.5) Q.append_simultaneous(obj=right_lid, action='move', pos=LID_SPOT2, duration=.5) Q.append(obj=left_object, action='move', pos=OFF_SOUTH, duration=0.0) Q.append(obj=right_object, action='move', pos=OFF_SOUTH, duration=0.0) # What order do we draw sprites and things in? dos = OrderedUpdates([left_box,left_object,left_boxfront, right_box,right_object,right_boxfront,left_lid,right_lid]) # Draw and update in this order #main ticker loop for event in kelpy_standard_event_loop(screen, Q, dos, throw_null_events=True): # output trial info to csv writer.writerow([subject, session, trial, i, start_time, time(), objects[0], probabilities[0], in_left, left_box.is_looked_at(), left_object.is_looked_at(), left_lid.is_looked_at(), objects[1],probabilities[1], in_right, right_box.is_looked_at(), right_object.is_looked_at(), right_lid.is_looked_at()]) print file_header #print subject, session, trial, i, start_time, time(), objects[0], probabilities[0], in_left, left_box.is_looked_at(), left_object.is_looked_at(), left_lid.is_looked_at(), objects[1],probabilities[1], in_right, right_box.is_looked_at(), right_object.is_looked_at(), right_lid.is_looked_at() if (time() - start_time > MAX_DISPLAY_TIME): break # If the event is a click: #if is_click(event): # break # need to do a check for exiting here if event.type == KEYDOWN: if event.key == K_ESCAPE: print("escaping now") quit() # make sure to close the data file when exiting, otherwise it'll hang if not use_tobii_sim: tobii_controller.stop_tracking() tobii_controller.close_data_file() tobii_controller.destroy()
def open_box(object, box, probability, trial, writer, BGCOLOR, lookaway_start_time,TRIAL_START): print TRIAL_START pygame.display.set_mode((1400,900),pygame.FULLSCREEN) play_sound(kstimulus('music/hothothot.wav')) start_time = time() lookaway_time = 2.0 started_looking_away = False screen.fill(BGCOLOR) box = TobiiSprite( screen, spot.center, box, tobii_controller, scale=BOX_SCALE) object = TobiiSprite( screen, spot.south, object,tobii_controller, scale=IMAGE_SCALE) blankbox = TobiiSprite( screen, spot.center, '../../kelpy/stimuli/misc/blankbox.png', tobii_controller, scale=BOX_SCALE) Q = DisplayQueue() Q.append(obj=box, action='wait', duration=1) for i in range(1000): #with certain probability, reveal object: flip = random.random() #print flip if flip < probability: Q.append_simultaneous(obj=object, action='move', pos=spot.center, duration=0) present = True Q.append(obj=box, action='move', pos=spot.north,duration=1) Q.append(obj=box, action='wait',duration=.25) Q.append(obj=box, action='move', pos=spot.center,duration=1) Q.append(obj=box, action='wait',duration=.25) Q.append_simultaneous(obj=object, action='move', pos=spot.north, duration=0) dos = OrderedUpdates([blankbox, object, box ]) #main ticker loop for event in kelpy_standard_event_loop(screen, Q, dos, throw_null_events=True): # output trial info to csv pygame.draw.rect(screen,(0,0,0),((screen.get_width() /2 )-200, (screen.get_height() /2 )-200,400,400),4) pygame.draw.rect(screen,BGCOLOR,((screen.get_width() /2 )-200, (screen.get_height() /2 )-601,400,400),0) pygame.draw.rect(screen,BGCOLOR,((screen.get_width() /2 )-200, (screen.get_height() /2 )+201,400,400),0) pygame.display.update() #writer.writerow([subject, session, trial, i, start_time, time(), objects[0], probabilities[0], in_left, left_box.is_looked_at(), left_object.is_looked_at(), left_lid.is_looked_at(), objects[1],probabilities[1], in_right, right_box.is_looked_at(), right_object.is_looked_at(), right_lid.is_looked_at()]) #print file_header #print subject, session, trial, i, start_time, time(), objects[0], probabilities[0], in_left, left_box.is_looked_at(), left_object.is_looked_at(), left_lid.is_looked_at(), objects[1],probabilities[1], in_right, right_box.is_looked_at(), right_object.is_looked_at(), right_lid.is_looked_at() if (time() - start_time > MAX_DISPLAY_TIME): break if not blankbox.is_looked_at(): if not started_looking_away: lookaway_start_time = time() started_looking_away = True #check if they've looked away for long enough if (time() - lookaway_start_time > lookaway_time): final_time = time() - TRIAL_START print str(final_time) + "is final time!!!!!!!!!!!" break #or whatever else you need to do at the end of the trial elif blankbox.is_looked_at(): started_looking_away = False #resets timer # If the event is a click: #if is_click(event): # break # need to do a check for exiting here if event.type == KEYDOWN: if event.key == K_ESCAPE: print("escaping now") quit() # make sure to close the data file when exiting, otherwise it'll hang if not use_tobii_sim: tobii_controller.stop_tracking() tobii_controller.close_data_file() tobii_controller.destroy() if event.key == K_p: pygame.mixer.music.pause() sleep(3) pygame.mixer.music.unpause()