【问题标题】:Pygame Window opens then immediately cloeses [duplicate]Pygame窗口打开然后立即关闭[重复]
【发布时间】:2022-01-09 04:18:40
【问题描述】:
在休息了大约一个半月后,我将回到 pygame。
我试图打开并创建一个窗口我试图运行它并且窗口立即关闭。
这是所有的代码:
import pygame
pygame.init()
screen_x = 230
screen_y = 230
display = pygame.display.set_mode ((screen_x, screen_y))
我正在使用 VSCode,如果它可以做任何事情,这是我第一次使用 VSCode,所以如果我没有做正确的事情,我可能不知道。
【问题讨论】:
标签:
python
visual-studio-code
pygame
【解决方案1】:
您需要一个事件循环,否则应用程序将立即关闭。
这是一些执行此操作的起始代码:
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