gpt4 book ai didi

python - pygame.error : video system not initialized. pygame.init() 已经调用

转载 作者:行者123 更新时间:2023-12-01 09:32:29 25 4
gpt4 key购买 nike

这几乎是您可以在 pygame 中编写的最简单的代码。它所做的只是创建一个窗口并允许您关闭它。但是,我收到此错误pygame.error:视频系统未初始化。我在网上搜索了这个,似乎大多数人忘记调用 pygame.init()。我不确定为什么会收到此错误。

import pygame
pygame.init()
screen = pygame.display.set_mode((900,500))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()

最佳答案

只需添加 sys.exit(0) 和末尾即可完成

以下是官方文档中的一些信息

pygame.quit() uninitialize all pygame modules quit() -> None Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue. It is safe to call this function more than once: repeated calls have no effect.

Note, that pygame.quit()uninitialize all pygame modules will not exit your program. Consider letting your program end in the same way a normal python program will end.

这是给您的示例代码。

import pygame
import sys
pygame.init()
pygame.display.set_mode((900, 500 ) )


while True :
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)

关于python - pygame.error : video system not initialized. pygame.init() 已经调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49845387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com