I have a problem with a pygame window disappearing immediately after it has been opened I know this can be resolved with a loop around the pygame.quit but i cant be able to solve it.
enter code he enter codeimport sys
import pygame
pygame.init()
quit = 1
if(quit == 2):
pygame.quit
if (quit == 1):
wind = pygame.display.set_mode((600,600))
width = 300
height = 300
x = 300
y = 300
vel = 1re
CodePudding user response:
I think it quits because the code has finished executing, and I don't see a loop in your code currently...
Maybe something like:
# Initialise your pygame stuff
isRunning = true
while isRunning:
# Respond to events from pygame
if certainCondition:
isRunning = false
pygame.quit()
CodePudding user response:
You window closes immediately, because your application is immediately terminated. You need an application loop. The typical PyGame application loop has to:
- limit the frames per second to limit CPU usage with
pygame.time.Clock.tick
- handle the events by calling either
pygame.event.pump()
orpygame.event.get()
. - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (
blit
all the objects) - update the display by calling either
pygame.display.update()
orpygame.display.flip()
e.g.:
import pygame
pygame.init()
window = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
# main application loop
run = True
while run:
# limit frames per second
clock.tick(100)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the display
window.fill(0)
# draw the scene
pygame.draw.circle(window, (255, 0, 0), (250, 250), 100)
# update the display
pygame.display.flip()
pygame.quit()
exit()