-3

I am a beginner to python & pygame, and im trying to make a pygame window. However, whenever the window opens, the window crashes and says not responding. can someone help me?

import pygame

pygame.init()
screen=pygame.display.set_mode((640,480))

while True:
    screen.fill((0,0,0))
    pygame.display.update()

pygame.quit()

3
  • You have an infinite loop up there Commented Oct 25 at 0:49
  • 1
    Add import sys; sys.exit() after pygame.quit() Commented Oct 25 at 1:00
  • This question is similar to: Why does my pygame window crash after some time?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 25 at 3:00

1 Answer 1

-3

make sure your pygame.quit() is outside the event loop and inside the while loop, otherwise any action will cause the window to close. Hope this helps!

Sign up to request clarification or add additional context in comments.

5 Comments

it is true that sys.exit is quite useful as it can completely stop running the code whereas pygame.quit() will just uninitialize, but you will get errors that say basically that you can't update because pygame not initialized. Anyway, that's not the point here : I launched your code, but it won't work because the loop is completely infinite. You have 2 options : 1/ add a running bool that turns false when you quit, which is done by : [...]running = false
import pygame, sys pygame.init() screen=pygame.display.set_mode((640,480)) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((0,0,0)) pygame.display.update() pygame.quit() sys.exit() 2/ import pygame, sys pygame.init() screen=pygame.display.set_mode((640,480)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.fill((0,0,0)) pygame.display.update() This way, you
*r window will reply correctly
I haven't seen that you mentioned the fact that the window did not respond. That is because you can't interact as once it is launched, you can theoretically never quit. I say theoretically because to solve that problem (to clear this window from your computer) you can force it to quit. To really solve it, it must respond to the quit event from the user, which you do by adding the pygame.QUIT. Hope you can understand better this way
@zulu27 if you have something that conflicts with the author's intent, please post a different answer instead of editing an existing answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.