Randomizing coordinates of an image

hello! im trying to make an aim “game” based on reaction time (similar to how the aim trainer mode for Human Benchmark looks). i have barely any knowledge on python and am having trouble randomizing the coordinates of the target. this is probably a very easy fix so sorry for the really beginner question haha :smiling_face_with_tear:. thank you :bowing_man:!

The main issue I can see if that you are using a code component to create and draw a target you are already creating and drawing using an image component.

Secondly you have the code component at the bottom. Put it at the top with JUST lines 10 and 11 and then set the location of the target image to $[target_x,target_y] updating each repeat in the component.

1 Like

ahh thank you so much! i have amended the code, rearranged the component sequence and marked the position in the image (target) component as “set every repeat”. the target does shows up on screen now but it’s always in the same position (specifically the centre of the screen) each trial.

initially there was this error code => “TypeError: random() takes at most 1 positional argument (2 given)” when i tried to run the experiment, so i replaced random() with randint() instead.

the target is still stuck in the same place, just not at the centre anymore.

Do you want random()-.5 ?

randint(0,1). randomly picks integers which are at least 0 and less than 1.

i would like the x and y coords to range between -0.5 to 0.5, not too sure if random()-.5 indicates that. it’s a bit of an awkward area but for some reason this is the acceptable range to fit the screen ratio for my uni’s monitor and anything beyond that would go out of bounds :sweat_smile:.

Hello

x = random()-0.5

gives you values between -0.5 and 0.5. random() returns a random floating number between 0 and1, see Python Random random() Method.

Whether this approach is really useful depends on the units chosen, see Units for the window and stimuli — PsychoPy v2024.2.5

Best wishes Jens

1 Like