# 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 = 8
NTarget = 2
NFrames = 500
maxRotSpeed = 0.1
firstFrameDur = 2000
yref = 204
xref = 272
targetColor = 'red'
normalColor = 'white'
# Create a list of stimuli, with various properties
stimList = []
for i in range(NStim):
x = random.randint(0, 480)
y = random.randint(0, 360)
a = random.random() * 2*math.pi
v = 5.
r = 15
stimList.append( (x, y, a, v, r) )
# Create a canvas
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 box, reverse direction by 180 deg (= 1pi radial)
if x <= 0 or x >= 480 or y <= 0 or y >= 360:
a = a + math.pi
else:
# else randomly rotate the stimulus
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+ xref), (y+ yref), 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
# Setting variables with the exp.set() function to make sure the variables
# are also available in the 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
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])