我尝试了在互联网上找到的多个简单的 pygame 程序,它们运行时没有错误消息,但游戏的窗口没有打开。 这是我的代码:
# Simple pygame program
# Import and initialize the pygame library
import pygame
import os
os.environ['SDL_AUDIODRIVER'] = 'dsp'
pygame.init()
# Set up the drawing window
screen = pygame.display.set_mode([500, 500])
# Run until the user asks to quit
running = True
while running:
# Did the user click the window close button?
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the background with white
screen.fill((255, 255, 255))
# Draw a solid blue circle in the center
pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
# Flip the display
pygame.display.flip()
# Done! Time to quit.
pygame.quit()
这是输出: pygame 2.5.2(SDL 2.28.2,Python 3.10.13) 来自 pygame 社区的您好。 https://www.pygame.org/contribute.html
我尝试了在网上找到的多个不同的程序,但输出总是相同的。 我通过 Github.dev 使用 VS Code,这可能是问题所在吗?
您应该将 pygame.display.flip() 放在带有时钟的 while 循环中,以便将每个帧重新绘制到屏幕上。这会起作用:
import pygame
import os
os.environ["SDL_AUDIODRIVER"] = "dsp"
pygame.init()
clock = pygame.time.Clock() #set the clock
screen = pygame.display.set_mode([500, 500])
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the background with white
screen.fill((255, 255, 255))
# Draw a solid blue circle in the center
pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
# Flip the display
pygame.display.flip()
# clock
clock.tick(60)