'How do i make a moving polygon disappear at a certain position?
I'm new to Python and Psychopy and have to do a project for university. I think it's a rather easy question, but i can't seem to figure it out.
I'm supposed to make a time-to-contact-task. The idea is that i have a polygon (a triangle) that moves on the x-axis from the left side of the screen to the right. At the right side of the screen is another polygon (a vertical line) that doesn't move. The testperson should press a key if the triangle is at the same position as the line. The moving triangle should disappear at a certain point (around the middle of the screen), so that the testperson should have to guess the arrival time of the triangle at the line.
Right now, this is my code:
at 'Begin routine':
Zeit = core.MonotonicClock()
origpos = meinPolygon.pos
aktpos = origpos
at 'Each frame':
aktpos[0] = origpos[0] + float(Geschwindigkeit) *(Zeit.getTime()*Beschleunigung)
aktpos[1] = origpos[1] + float(0) * Zeit.getTime()
meinPolygon.pos = aktpos
if aktpos = [1]:
polygon.opacity(0)
if aktpos[0] = polygon.pos[0]:
print(Zeit)
So i was thinking, that if the moving polygon reaches a certain position (eg. x=1), the opacity should change to 0, so that the polygon isn't visible anymore. But my code doesn't seem to work this way (Syntax Error). And i'm not sure how to put the x and y value of the position? I don't care about y, because its always 0.
And we want the time printed, when the triangle meets the line, so that we can calculate the reaction-time of the testperson.
I hope i described the situation understandable. Thanks in advance for your help!
Solution 1:[1]
here is a version for using PsychoPy without the Builder tool:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from psychopy import core, info, visual, event
from psychopy import monitors
from psychopy.constants import NOT_STARTED, STARTED, FINISHED
from psychopy.iohub.client import launchHubServer
#INIT
io = launchHubServer()
display = io.devices.display
kb = io.devices.kb; mouse = io.devices.mouse
mon = monitors.Monitor(name = display.getPsychopyMonitorName(), autoLog = True)
win = visual.Window([1440, 900], units='pix', monitor = mon, winType='pyglet',
fullScr = False, waitBlanking = True, useFBO = True, useLights = False,
allowStencil=False, allowGui = True, screen = display.getIndex())
win._setupGL(); core.wait(.1)
runtime_info = info.RunTimeInfo(win = win, refreshTest = 'grating', verbose = True)
win_w = runtime_info['windowSize_pix'][0]; win_h = runtime_info['windowSize_pix'][1]
#suppose BEGIN ROUTINE?
LOCATION_TOLERANCE = 4 #screen pixels
POLYGON_SPEED = 0.1 #pixels per frame
POLYGON_ACCEL = 1.005
mainPolygon = visual.Polygon(win, edges=3, radius=50, units='pix', pos=(-win_w/2*0.75,0),
lineColor=[0, 1, 0], fillColor=[0, 1, 0], interpolate=False, autoDraw=True)
targetPolygon = visual.Polygon(win, edges=4, radius=20, units='pix', pos=(win_w/2*0.60,0), ori=45,
lineColor=[1, 1, 1], fillColor=[1, 1, 1], interpolate=False, autoDraw=True)
#ROUTINE
global_monotonic = core.MonotonicClock(); trial_clock = core.Clock()
future_flip_time = win.getFutureFlipTime(clock='now')
trial_clock.reset(-future_flip_time)
t = 0; frame_n = -1
mainPolygon.status = NOT_STARTED; routine_running = True
mainPolygon.t_thresholded, mainPolygon.t_targetreached, mainPolygon.t_keypressed = 0.0, 0.0, 0.0
while routine_running:
t = trial_clock.getTime(); frame_n = frame_n + 1
#suppose EACH FRAME?
if routine_running:
curr_xpos, curr_ypos = mainPolygon.pos[0], mainPolygon.pos[1]
mainPolygon.setPos((curr_xpos + POLYGON_SPEED, curr_ypos), log=True)
POLYGON_SPEED *= POLYGON_ACCEL
if curr_xpos > LOCATION_TOLERANCE and not mainPolygon.t_thresholded:
mainPolygon.t_thresholded = global_monotonic.getTime()
mainPolygon.setOpacity(0)
print(f"THRESHOLD LOCATION: trial_clock = {t:.3f}, frame_n = {frame_n:d}")
#FIXME use psychopy.visual.helpers.pointInPolygon() or polygonsOverlap()
if targetPolygon.pos[0] - curr_xpos < LOCATION_TOLERANCE and not mainPolygon.t_targetreached:
mainPolygon.t_targetreached = global_monotonic.getTime()
print(f"TARGET REACHED: trial_clock = {t:.3f}; monotonic = {mainPolygon.t_targetreached:.3f}")
if len(event.getKeys(keyList=['space'])) and not mainPolygon.t_keypressed:
mainPolygon.t_keypressed = global_monotonic.getTime()
print(f"KEY PRESSED: trial_clock = {t:.3f}; frame_n = {frame_n:d}")
win.flip()
if (mainPolygon.t_keypressed and mainPolygon.t_targetreached):
POLYGON_SPEED = 0; mainPolygon.setOpacity(1)
routine_running = False;
if not routine_running:
break
#REPORT
print(f"")
print(f"REACTION DIFFERENCE: monotonic_diff = {mainPolygon.t_keypressed-mainPolygon.t_targetreached:.3f}")
win.flip(); event.waitKeys()
#FINALIZE
io.quit(); win.close()
core.quit()
Solution 2:[2]
A syntax error indicates you haven't got the characters correct in your code, like brackets that don't match etc. The logic may be fine. Take note of what the syntax error is pointing you to and check that line as well as the lines just above (sometimes an error on one line only shows up on the next).
In your case you have if aktpos = [1]: but a single = is for assignment. To test equality you need ==. This is a really common syntax error, even for experienced will make this typo so watch out for it.
There's actually at least one more problem though in your code because aktpos is always going to be a position with an x,y value. It will never be equal to [1] which is what you're currently (trying) to test. So the comparison isn't quite right. And how exact do you need that location to be? Think about what distance in x and y before you consider it to be a single location because I expect it will never be EXACTLY the same given that the stimulus can make sub-pixel movements
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jon |

