Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. # Import the necessary modules
  2. import random
  3. import math
  4. from openexp.canvas import canvas
  5. from openexp.mouse import mouse
  6.  
  7. # Define some general properties of the experiment
  8. NStim = 8
  9. NTarget = 2
  10. NFrames = 500
  11. maxRotSpeed = 0.1
  12. firstFrameDur = 2000
  13. yref = 204
  14. xref = 272
  15. targetColor = 'red'
  16. normalColor = 'white'
  17.  
  18. # Create a list of stimuli, with various properties
  19. stimList = []
  20. for i in range(NStim):
  21.         x = random.randint(0, 480)
  22.         y = random.randint(0, 360)
  23.         a = random.random() * 2*math.pi
  24.         v = 5.
  25.         r = 15
  26.         stimList.append( (x, y, a, v, r) )
  27.                
  28. # Create a canvas
  29. myCanvas = canvas(exp)
  30.        
  31. # Loop through all frames
  32. for j in range(NFrames):
  33.  
  34.         # For each frame, clear the canvas and then loop through all stimuli
  35.         myCanvas.clear()
  36.         for i in range(NStim):
  37.                 # Get the stimulus properties
  38.                 x, y, a, v, r = stimList[i]
  39.                 # Update the position of the stimulus based on the angle and the speed
  40.                 x += v * math.cos(a)
  41.                 y += v * math.sin(a)  
  42.                 # If the stimulus leaves the box, reverse direction by 180 deg (= 1pi radial)
  43.                 if x <= 0 or x >= 480 or y <= 0 or y >= 360:
  44.                         a = a + math.pi
  45.                 else:
  46.                         # else randomly rotate the stimulus
  47.                         a += (random.random()-.5) * maxRotSpeed                
  48.                 # Highlight the targets on the first frame
  49.                 if i < NTarget and j == 0:
  50.                         color = targetColor
  51.                 else:
  52.                         color = normalColor                    
  53.                 # Draw the stimulus
  54.                 myCanvas.circle((x+ xref), (y+ yref), r, fill=True, color=color)
  55.                 # Store the new stimulus properties back in the stimulus list
  56.                 stimList[i] = x, y, a, v, r
  57.         # Show the canvas      
  58.         myCanvas.show()
  59.         # Sleep after the first frame so that the participant can identify the targets
  60.         if j == 0:
  61.                 self.sleep(firstFrameDur)
  62. # Store the coordinates of the stimuli in the last frame, in order to compare
  63. # the mouse click responses with the actual positions
  64.  
  65. stimID = 0
  66. # Loop through the stimulus list
  67. for stimProperties in stimList:
  68.         stimID +=1
  69.         # Setting variables with the exp.set() function  to make sure  the variables
  70.         # are also available in the the logger item
  71.         exp.set("xStim"+str(stimID), stimProperties[0])
  72.         exp.set("yStim"+str(stimID), stimProperties[1])
  73.  
  74. # Collect response
  75. responseID = 0
  76. for i in range(NTarget):
  77.         responseID +=1
  78.         print responseID
  79.         # Initiate a mouse object
  80.         my_mouse = mouse(exp, visible = True)
  81.         # Collect the response
  82.         button, position, timestamp = my_mouse.get_click()
  83.         # Set the x and y coordinates of the response so that they will be logged by the
  84.         # logger item. Position is an (x,y) tuple in screen coordinates
  85.         exp.set("xMouseClick"+str(responseID), position[0])
  86.         exp.set("yMouseClick"+str(responseID), position[1])