Skip to content

Instantly share code, notes, and snippets.

Created May 13, 2016 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a29b67a3a125bbe35c4ff9e8fe60b717 to your computer and use it in GitHub Desktop.
Save anonymous/a29b67a3a125bbe35c4ff9e8fe60b717 to your computer and use it in GitHub Desktop.
# 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")
TC=self.get("TargCol")
DS=self.get("DistCol")
PA=self.get("PresAbs")
#light=rgb(190,190,190)
#dark=rgb(160,160,160)
if TC=="dark":
T_C='rgb(255,0,0)'
if TC=="light":
T_C='rgb(0,255,0)'
if DS=="dark":
D_C='rgb(255,0,0)'
if DS=="light":
D_C='rgb(0,255,0)'
#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: 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:
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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment