Howdy, Stranger!

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

Supported by

Trying to show random 6 rects and python has crushed.

edited May 2023 in OpenSesame

Hello everyone,

I would like to show 6 same size white rectangles in random places on sketchpad. I also want to be sure that these rectangles will never overlap. But every time I try the code I wrote, python stops working and my computer freezes completely (My computer is 8 gb ram, macbook 2020). I would appreciate any help.


Thanks in advance.


Here is the code:

import pygame
import random


width = 192
height = 128


win_width = 512
win_height = 384


white = (255, 255, 255)


rects = []
for i in range(6):

    x = random.randint(0, win_width - width)
    y = random.randint(0, win_height - height)
    while any(abs(x - r.x) < width and abs(y - r.y) < height for r in rects):
        x = random.randint(0, win_width - width)
        y = random.randint(0, win_height - height)
    rects.append(pygame.Rect(x, y, width, height))


pygame.init()
win = pygame.display.set_mode((win_width, win_height))


for r in rects:
    pygame.draw.rect(win, white, r)

pygame.display.flip()
while True:
    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             pygame.quit()
quit()


Comments

  • Hi,

    I don't think it is possible to fit 6 rectangles into that window, you can try with a much bigger window (or smaller rectangles) to check. That should not cause an infinite loop.

    Eduard

    Buy Me A Coffee

  • Thank you so much! I extended the window. Now, I am able to see six random rectangles but I still have that mistake code:

    "The experiment did not finish normally for the following reason:


    Python seems to have crashed. This should not happen. If Python crashes often, please report it on the OpenSesame forum."

    item-stack: ``


    I do not know what to do, can you help me?

  • Can you share the experiment? This error message is not really helpful, as it doesn't provide information on what exactly has crashed. Oftentimes it is not even related to the specific experiment, but the setup of Opensesame. I can have a look if you send the experiment.

    Eduard

    Buy Me A Coffee

  • That's a very beginning of my experiment. Thanks in advance!

  • Hi,

    I don't have that issue. Could you try to remove the pygame.quit(), and quit() part? They are not really necessary actually, Opensesame provides the overhead for you (incl. handling the windows). If I remove these two lines, the experiment finishes successfully for me.

    Btw, you don't to import pygame if you want to present stimuli with code. You can just use Opensesame's Canvas object (see the documentation fo rmore info)

    Eduard

    Buy Me A Coffee

  • Hello again,


    I have tried to make it work with Canvas object but it didn't work. Is it possible to check it for you?


    Thank you so much!


  • I slightly modified your code, and tried a few trials and it seems to work. But better to test your logic more thoroughly!

    Eduard


    Buy Me A Coffee

  • Thank you for your kind an patient answers!



    Beril

Sign In or Register to comment.