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.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games