Reputation: 51
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
Upvotes: 0
Views: 332
Reputation: 210978
You window closes immediately, because your application is immediately terminated. You need an application loop. The typical PyGame application loop has to:
pygame.time.Clock.tick
pygame.event.pump()
or pygame.event.get()
.blit
all the objects)pygame.display.update()
or pygame.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()
Upvotes: 1
Reputation: 307
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()
Upvotes: 0