Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pygame Window opens then immidiatly cloeses

I am coming back to pygame after around one and a half months of taking a break.
I tried to open and create a window i tried to run it and the window immidiatly closed.
This is all of the code:

import pygame
pygame.init()
screen_x = 230
screen_y = 230
display = pygame.display.set_mode ((screen_x, screen_y))

I am using vscode if that does anything, this is the first time I have used vscode so if I do not do something right i may not know.
Thank you for helping!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need an event loop, or else the application will close immediately.
Here is some starter code to do this:

import pygame


# screen object(width,height)
screen_x = 230
screen_y = 230
screen = pygame.display.set_mode((screen_x, screen_y))

# Set the caption of the screen
pygame.display.set_caption('Title')

# Variable to keep our game loop running
running = True

# game loop
while running:
    # for loop through the event queue
    for event in pygame.event.get():
        # Check for QUIT event  
        if event.type == pygame.QUIT:
                running = False

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading