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
- w, h = exp.resolution()
- # Define some general properties of the experiment
- NStim = 4
- NTarget = 2
- NFrames = 50
- maxRotSpeed = .1
- firstFrameDur = 1000
- targetColor = 'red'
- normalColor = 'white'
- winSpeed = 8
- winSize = 400, 400 # The size of the window
- winPos = w/2, h/2 # The position of the center of the window
- winMargin = 20 # The margin around the window used to draw the edge (so the window
- # appears a bit larger than the winSize)
- clickMargin = 100 # The margin near the edge that will trigger a move when clicked
- # Replay should be a variable that is set to 0 or 1. If replay is not enabled, the
- # trajectory will be remembered to be played back later on. If replay is enabled,
- # a previous trajectory will be used to play back.
- replay = self.get('replay')
- if not replay:
- trajectory = []
- else:
- random.shuffle(trajectories)
- trajectory = trajectories.pop()
- # Initiate a mouse object
- # (See documentation: http://osdoc.cogsci.nl/python-inline-code/mouse-functions)
- my_mouse = mouse(exp, visible=True)
- # Create a list of stimuli, with various properties
- stimList = []
- for i in range(NStim):
- x = random.randint(-winSize[0]/2, winSize[0]/2)
- y = random.randint(-winSize[1]/2, winSize[1]/2)
- a = random.random() * 2*math.pi
- v = 4.
- 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)
- # Initially the targetPosition of the window is the same as the winPos, so that the
- # window doesn't move.
- targetPos = winPos
- # Loop through all frames
- for j in range(NFrames):
- # Add the current position to the trajectory in we are not in replay mode,
- # otherwise retrieve the first winPos from the to-be-played-back trajectory.
- if not replay:
- trajectory.append(winPos)
- else:
- winPos = trajectory.pop(0)
- # For each frame, clear the canvas and then loop through all stimuli
- myCanvas.clear()
- # Draw a rectangle to indicate the position of the window. Make it a bit bigger
- # than the actual window size, so that the stimuli do not overlap the edge
- myCanvas.rect(winPos[0]-winSize[0]/2-winMargin, winPos[1]-winSize[1]/2-winMargin, winSize[0]+2*winMargin, winSize[1]+2*winMargin)
- 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 <= -winSize[0]/2 or x >= winSize[0]/2 or y <= -winSize[1]/2 or y >= winSize[1]/2:
- 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+winPos[0], y+winPos[1], 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()
- if j == 0:
- self.sleep(firstFrameDur)
- # Get a mouse click
- button, position, timestamp = my_mouse.get_click(timeout=1)
- if position != None:
- x, y = position
- # The target position is not that of the mouse cursor, but one of the edges
- if x < clickMargin:
- targetPos = winSize[0]/2, h/2
- elif x > w-clickMargin:
- targetPos = w-winSize[0]/2, h/2
- elif y < clickMargin:
- targetPos = w/2, winSize[1]/2
- elif y > h-clickMargin:
- targetPos = w/2, h-winSize[1]/2
- # Move the window nearer the target position
- if targetPos[0] < winPos[0]:
- winPos = winPos[0]-winSpeed, winPos[1]
- elif targetPos[0] > winPos[0]:
- winPos = winPos[0]+winSpeed, winPos[1]
- if targetPos[1] < winPos[1]:
- winPos = winPos[0], winPos[1]-winSpeed
- elif targetPos[1] > winPos[1]:
- winPos = winPos[0], winPos[1]+winSpeed
- # Remember this trajectory if we are not in replay mode
- if not replay:
- trajectories.append(trajectory)
- # 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
- # Collect the response
- button, position, timestamp = my_mouse.get_click()
- # Make the cursor position relative to the window
- position = position[0] - winPos[0], position[1] - winPos[1]
- # 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