Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import the necessary modules
- import random
- import math
- from openexp.canvas import canvas
- from openexp.mouse import mouse
- # Define some general properties of the experiment
- NStim = 18
- NTarget = 1
- NFrames = 5000
- maxRotSpeed = .1
- firstFrameDur = 500
- targetColor = 'red'
- normalColor = 'black'
- # Create a list of stimuli, with various properties
- stimList = []
- for i in range(NStim):
- x = random.randint(0, self.get('width'))
- y = random.randint(0, self.get('height'))
- a = random.random() * 2*math.pi
- v = 50
- r = 20
- stimList.append( (x, y, a, v, r) )
- # Create a canvas
- # (See documentation: http://osdoc.cogsci.nl/python-inline-code/canvas-functions)
- myCanvas = canvas(exp)
- # Loop through all frames
- for j in range(NFrames):
- # For each frame, clear the canvas and then loop through all stimuli
- myCanvas.clear()
- for i in range(NStim):
- # Get the stimulus properties
- x, y, a, v, r = stimList[i]
- # Update the position of the stimulus based on the angle and the speed
- x += v * math.cos(a)
- y += v * math.sin(a)
- # If the stimulus leaves the screen, reverse direction by 180 deg (= 1pi radial)
- if x <= 0 or x >= self.get('width') or y <= 0 or y >= self.get('height'):
- a = a + math.pi
- else:
- # else randomly rotate the stimulus a bit
- a += (random.random()-.5) * maxRotSpeed
- # Highlight the targets on the first frame
- if i < NTarget and j == 0:
- color = targetColor
- else:
- color = normalColor
- # Draw the stimulus
- myCanvas.circle(x, y, r, fill=True, color=color)
- # Store the new stimulus properties back in the stimulus list
- stimList[i] = x, y, a, v, r
- # Show the canvas
- myCanvas.show()
- # Sleep after the first frame so that the participant can identify the targets
- if j == 0:
- self.sleep(firstFrameDur)
- # Store the coordinates of the stimuli in the last frame, in order to compare
- # the mouse click responses with the actual positions
- stimID = 0
- # Loop through the stimulus list
- for stimProperties in stimList:
- stimID +=1
- # By setting variables with the exp.set() function you make sure the variables
- # are also available in the GUI (notably, the logger item)
- exp.set("xStim"+str(stimID), stimProperties[0])
- exp.set("yStim"+str(stimID), stimProperties[1])
- # Collect response
- responseID = 0
- for i in range(NTarget):
- responseID +=1
- print responseID
- # Initiate a mouse object
- # (See documentation: http://osdoc.cogsci.nl/python-inline-code/mouse-functions)
- my_mouse = mouse(exp, visible = True)
- # Collect the response
- button, position, timestamp = my_mouse.get_click()
- # Set the x and y coordinates of the response so that they will be logged by the
- # logger item. Position is an (x,y) tuple in screen coordinates
- exp.set("xMouseClick"+str(responseID), position[0])
- exp.set("yMouseClick"+str(responseID), position[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement