0

I started to make a game with pygame and i am only at that point, where i should have 10*10 grey squares. When I start the program, the squares appear but the program is not responding. Is there anything wrong with the script or is it about my laptop performance?

import pygame
pygame.init()
win=pygame.display.set_mode((768,768))
left=50
up=50
difx=20
dify=20
sx=50
sy=50
grey=(90,90,90)
black=(0,0,0)
blue=(0,0,250)
def draw():
    pygame.Surface.fill(win,black)
    for x in range(0,100):
        pygame.draw.rect(win,grey,(left+x%10*(sx+difx),up+round((x-x%10)/10)*(sy+dify),sx,sy))
    pygame.display.update()
while True:
    pygame.time.delay(100)
    draw()

Python 3.7 Windows 10

Sebi19
  • 33
  • 5

1 Answers1

0

You can try this.

import pygame

def main():
    pygame.init()
    win=pygame.display.set_mode((768,768))
    left=50
    up=50
    difx=20
    dify=20
    sx=50
    sy=50
    grey=(90,90,90)
    black=(0,0,0)
    blue=(0,0,250)
    def draw():
        pygame.Surface.fill(win,black)
        for x in range(0,100):
            pygame.draw.rect(win,grey,(left+x%10*(sx+difx),up+round((x-x%10)/10)*(sy+dify),sx,sy))

    while True:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                return 
            pygame.display.update()
            draw()

if __name__ == '__main__':
    main()

You are missing a for e in pygame.event.get():

Axois
  • 1,961
  • 2
  • 11
  • 22
  • That works, thank you. Can you explain why my version didn't work? Is this some kind of bug or did i make a mistake? – Sebi19 Aug 07 '19 at 11:53
  • Im following the online example of `pygame`. I would think its a careless mistake of yours. You are missing the code `for e in pygame.event.get():` in the while True loop which updates the window on a certain event. The reason it freezes its because your `while True` loop constantly updates regardless of any events. If you think I've helped, you can choose to select this as an answer to close this question! – Axois Aug 07 '19 at 11:56