smathot

Run phase

Jan 21st, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import pygame
  2. from openexp.exceptions import response_error
  3. # Start by playing the sound file:
  4. my_sampler.play()
  5. sampler_paused = False
  6. for i in range(len(patch_list)):
  7.     if sampler_paused:
  8.         my_sampler.resume()
  9.     # Determine the stimulus by selecting one item from the
  10.     # stimulus list (without replacement).
  11.     stim = patch_list.pop()
  12.     # Set the stimulus for future use in the GUI (notably, the logger
  13.     # item):
  14.     exp.set("stim", stim)
  15.     # Determine the absolute path to the image in
  16.     # the file pool:
  17.     path = exp.get_file(stim)
  18.     # Show the stimulus:
  19.     # Start with a gray background for a certain duration:
  20.     my_canvas.clear()
  21.     # We have to draw something in order to clear the background.
  22.     my_canvas.circle(0,0,0,color=self.get('background'))
  23.     my_canvas.show()
  24.     self.sleep(100)
  25.     # Present the patch
  26.     my_canvas.image(path)
  27.     my_canvas.show()
  28.     start_time = self.time()
  29.     time_passed = 0
  30.     while time_passed < ISI:
  31.         # Keep checking whether the duration  of this
  32.         # while loop is still below the maximum duration
  33.         # (i.e. ISI).
  34.         time_passed = self.time()-start_time
  35.         print time_passed
  36.         # To poll the keyboard and mouse at the same time, we use pygame
  37.         # directly. This means that this piece of code requires the xpyriment
  38.         # or pygame backend! See:
  39.         # <http://www.pygame.org/docs/ref/event.html>
  40.         button = False
  41.         key = False
  42.         # Loop through all 'events', which are key presses or button clicks
  43.         for event in pygame.event.get([pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN]):
  44.             if event.type == pygame.MOUSEBUTTONDOWN:
  45.                 button = True
  46.             elif event.type == pygame.KEYDOWN:
  47.                 if event.key == pygame.K_ESCAPE:
  48.                     raise response_error("The escape key was pressed.")
  49.                 elif event.key == pygame.K_SPACE:
  50.                     key = True
  51.         # If a key was pressed or a button was clicked, exit the loop
  52.         if button or key:
  53.             break
  54.     # Set the given responses so that they will be logged by
  55.     # the logger item. Note that for the variable "mouse_click" (see below)
  56.     # True' indicates that a mouse click was given, whereas'False'
  57.     # indicates that no mouse response was given. This, in combination
  58.     # with the variable 'stim', will enable you to evaluate participants'
  59.     # performance (i.e. whether a mouse click was correct or a false alarm, etc.):
  60.     exp.set("mouse_click", button)
  61.     # If a mouse response was collected, the for loop will be
  62.     # continued. However, if the spacebar was pressed, we need to
  63.     # do something else:
  64.     if key:
  65.         # Pause the sampler:
  66.         my_sampler.pause()
  67.         sampler_paused = True
  68.         # Show the in-the-GUI-prepared form items:
  69.         self.experiment.items['mc'].prepare()
  70.         self.experiment.items['mc'].run()
  71.     # Finally, log the responses:
  72.     self.experiment.items['logger'].prepare()
  73.     self.experiment.items['logger'].run()
  74.     # And set the variable 'form_response' to 'NA' again.
  75.     exp.set('form_response', "NA")
Advertisement
Add Comment
Please, Sign In to add comment