Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  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 = 18
  9. NTarget = 1
  10. NFrames = 5000
  11. maxRotSpeed = .1
  12. firstFrameDur = 500
  13. targetColor = 'red'
  14. normalColor = 'black'
  15.  
  16. # Create a list of stimuli, with various properties
  17. stimList = []
  18. for i in range(NStim):
  19. x = random.randint(0, self.get('width'))
  20. y = random.randint(0, self.get('height'))
  21. a = random.random() * 2*math.pi
  22. v = 50
  23. r = 20
  24. stimList.append( (x, y, a, v, r) )
  25.  
  26. # Create a canvas
  27. # (See documentation: http://osdoc.cogsci.nl/python-inline-code/canvas-functions)
  28. myCanvas = canvas(exp)
  29.  
  30. # Loop through all frames
  31. for j in range(NFrames):
  32.  
  33. # For each frame, clear the canvas and then loop through all stimuli
  34. myCanvas.clear()
  35. for i in range(NStim):
  36. # Get the stimulus properties
  37. x, y, a, v, r = stimList[i]
  38. # Update the position of the stimulus based on the angle and the speed
  39. x += v * math.cos(a)
  40. y += v * math.sin(a)
  41. # If the stimulus leaves the screen, reverse direction by 180 deg (= 1pi radial)
  42. if x <= 0 or x >= self.get('width') or y <= 0 or y >= self.get('height'):
  43. a = a + math.pi
  44. else:
  45. # else randomly rotate the stimulus a bit
  46. a += (random.random()-.5) * maxRotSpeed
  47. # Highlight the targets on the first frame
  48. if i < NTarget and j == 0:
  49. color = targetColor
  50. else:
  51. color = normalColor
  52. # Draw the stimulus
  53. myCanvas.circle(x, y, r, fill=True, color=color)
  54. # Store the new stimulus properties back in the stimulus list
  55. stimList[i] = x, y, a, v, r
  56. # Show the canvas
  57. myCanvas.show()
  58. # Sleep after the first frame so that the participant can identify the targets
  59. if j == 0:
  60. self.sleep(firstFrameDur)
  61. # Store the coordinates of the stimuli in the last frame, in order to compare
  62. # the mouse click responses with the actual positions
  63.  
  64. stimID = 0
  65. # Loop through the stimulus list
  66. for stimProperties in stimList:
  67. stimID +=1
  68. # By setting variables with the exp.set() function you make sure the variables
  69. # are also available in the GUI (notably, the logger item)
  70. exp.set("xStim"+str(stimID), stimProperties[0])
  71. exp.set("yStim"+str(stimID), stimProperties[1])
  72.  
  73. # Collect response
  74. responseID = 0
  75. for i in range(NTarget):
  76. responseID +=1
  77. print responseID
  78. # Initiate a mouse object
  79. # (See documentation: http://osdoc.cogsci.nl/python-inline-code/mouse-functions)
  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])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement