| Reference | Downloads | Github

Creating a LOT of polygons using code componet

Hi everyone,

I need to create a routine with around 100 rectangles in a fixed position (all in a horizontal line at a fixed distance). These rectangles should be clickable (e.g. they should change color when the participant clicks on them, but that part is not an issue).

I can use 100 polygon component inside the builder, but I guess that there is a better way to draw all 100 polygon using a code component. I tried looking around the forum and I think I should use DotStim, but I am not sure how.

Does anyone have an example on how to code a large amount of polygons being drawn?

Thank you!

Check my crib sheets and code snippets. One of them definitely has code I use for polygon arrays.

Maybe working code examples in the 2020 crib sheet plus the code snippets document

This is what I worked out so far:

#in the Begin Experiment Tab
def makeSquare(pos):
    square = visual.ShapeStim(win=win, pos=pos, size=(0.015605, 0.21), fillColor=[1, 1, 1], lineColor=[-1, -1, -1], vertices='rectangle')
    return square

#in the Begin Routine Tab
for i in range (100):
    x = (-0.7731975 + 0.015605*i)
    makeSquare((x, -0.03445))

I have a couple of problems:

  1. How do I clear the rectangles at the end of the routine?
  2. I need to have the rectangles to appear over a background image, but they are always drawn under it, even if i move the code component below the image. Is there a way around this?
  3. I need to identify each rectangle in a unique way, so I can write a piece of code for what happen when I click on them. How should I do this?

Thank you!

Since I don’t use makesquare then I’d recommend that you try to use append to add visual.rect definitions to an array, as per my code.

I’m still on my phone so it’s not easy for me to search my crib sheet and snippets page for the code I use.