Ending a Polygon after a certain distance

OS : Win 10
PsychoPy version : 2020.1.3
Standard Standalone? (y/n) : y
What are you trying to achieve?:
I have a Polygon which moves across the screen. The Position is set every frame with (X - speed + (speed*t), Y). Speed is defined in my conditionsfile and I use pix as unit
I want the Polygon to fade after a certain distance

What did you try to make it work?:
I added a code component (I tried Each frame and End Routine because I was not sure where to set it)
My Code is
if poygon.position == (X+120,Y):
continueRoutine = False

I guess, i just need to change the code but i’ve been running out of ideas.

What specifically went wrong when you tried that?:

My Polygon never stops, it just moves out of the window.

Hi There,

Thanks for sharing your code component (you can also format it using the </> symbol).

Currently you are trying to end the routine once the polygon hits a certain position, is that what you need? If you want it to fade at a certain position you want to be creating a variable for the “opacity” parameter of your polygon, i.e.:

if polygon.position ==(X+120, Y):
    thisOpacity = 0
    thisOpacity = 1

then set the Opacity parameter in your polygon to be “thisOpacity”.
To stop your polygon you need to edit the code you have in the “every frame” tab to only adjust the polygon position IF the current position is less than a target position.


Hey Becca,
Thanks for your quick reply.

I want to stop the polygon, so the trial can move on to another Polygon.
I tried to set my code (at every frame) to

if polygon.pos < (X+120,Y):
continueRoutine = True
continueRoutine =False

My Polygon ends now after some time which is nice, however it does not altways stops after 120 Pixels, instead the pixels passed are somewhere between 120 and 60 and differ every trial.
Do you have any idea, where I went wrong?
Thanks a lot,

Hi Paula,

so that I can tell a bit more about how you set X and Y there, please could you copy and paste the full code that you have in each of the tabs of your code component there? :slight_smile:

(assuming your polygon ‘position’ parameter is set as ($X, $Y) and ‘set every frame’)


Thank you so much!
I have uploaded the Properties of my Polygon, and the Begin Routine and Each Frame tab of my Code.

SSvel is the velocity which is defined in my conditionsfile and is currently set to 60.

Thanks, Paula

Inspired by this Post i made it work.

The Code who works is

if polygon.pos[0] <= (x+120):
continueRoutine = True
continueRoutine = False

in case anyone is facing the same problem

1 Like