Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# 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
#self.get() - the get method
SS = self.get("SetSize")
if TC=="dark":
if TC=="light":
if DS=="dark":
if DS=="light":
#NStim = random.randint(1, 4)
NStim = SS
NTarget = 1
NFrames = 100
maxRotSpeed = .9
firstFrameDur = 500
#targetColor = 'red'
targetColor = T_C
normalColor = D_C
# Create a list of stimuli, with various properties. v = item velocity; r = object size
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 =8
r = 3
stimList.append( (x, y, a, v, r) )
# Create a canvas
# (See documentation:
myCanvas = canvas(exp)
# Loop through all frames
for j in range(NFrames):
# For each frame, clear the canvas and then loop through all stimuli
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 randomly rotate the stimulus a bit
a += (random.random()-.5) * maxRotSpeed
# Highlight the targets on the first frame
if i < NTarget:
color = targetColor
color = normalColor
# Draw the stimulus, 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
# Sleep after the first frame so that the participant can identify the targets
if j == 0:
# 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:
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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment