简体   繁体   中英

Pygame Window opens then immediately 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 immediately 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM