Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

draw circles in random positions in sketchpad

Hello! I am trying to draw a random number of circles in a sketchpad. This number should be different in each trial. I tried to do it with inline script. I draw 4 circles in a sketchpad and then i inserted the following code:

from random import shuffle

positions = xy_random(4, 500,300,40)

shuffle(positions)

standard_1 = items['standard_1'].canvas

standard_1['c1'].x,standard_1['c1'].y = positions[0]

standard_1['c2'].x,standard_1['c2'].y = positions[2]

standard_1['c3'].x,standard_1['c3'].y = positions[3]

standard_1['c4'].x,standard_1['c4'].y = positions[1]


Although the experiment runs, circles don't change positions in each trial. Could someone please tell me what i am doing wrong?

Thank you in advance!😊

Comments

  • Hi @rania_tac,

    Can you provide a little more information or upload your task (or a stripped down version of it). Are you running this code within the trial sequence? (if you run it before the loop, the same random arrangement will be used for all trials of the loop). It would help to see the task and how exactly you implement this code and draw the circles to know what the issue may be.

    Best,

    Fabrice.

    Buy Me A Coffee

  • Hi @rania_tac ,


    In addition to @Fab 's response: Perhaps I'm missing something, but wouldn't it be easier to

    • First determine (x,y) coordinates in an inline_script item...
    • and only then use these coordinates in a sketchpad item...

    instead of the reverse order that you are referring to?


    Like so:


    In the `Run tab` of your inline_script item, put something like:


    from random import shuffle
    positions = xy_random(4, 500,300,40)
    shuffle(positions)
    
    var.x1,var.y1 = positions[0]
    var.x2,var.y2 = positions[1]
    var.x3,var.y3 = positions[2]
    var.x4,var.y4 = positions[3]
    


    And then use the `square-bracket notation` to refer to the variables x1, y1, etc. in the sketchpad item:


    set duration keypress
    set description "Displays stimuli"
    draw circle color=red fill=1 name=c1 penwidth=1 r=71.55417527999327 show_if=always x="[x1]" y="[y1]" z_index=0
    draw circle color=yellow fill=1 name=c2 penwidth=1 r=71.55417527999327 show_if=always x="[x2]" y="[y2]" z_index=0
    draw circle color=green fill=1 name=c3 penwidth=1 r=71.55417527999327 show_if=always x="[x3]" y="[y3]" z_index=0
    draw circle color=blue fill=1 name=c4 penwidth=1 r=71.55417527999327 show_if=always x="[x4]" y="[y4]" z_index=0
    


    See attached a working example.


    Cheers,


    Lotje


    Did you like my answer? Feel free to Buy Me A Coffee :)

  • Hello again! Thank you so much @Fab and @lvanderlinden !!

    I included it as inline script and it work!!!

    Again thank you for all the help! ☺️

Sign In or Register to comment.