每当我试图执行我的pygame
代码:pygame.error: video system not initialized
时,我都会收到这个错误。
from sys import exit
import pygame
from pygame.locals import *
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
screen = screen_width, screen_height = 600, 400
clock = pygame.time.Clock()
pygame.display.set_caption("Physics")
def game_loop():
fps_cap = 120
running = True
while running:
clock.tick(fps_cap)
for event in pygame.event.get(): # error is here
if event.type == pygame.QUIT:
running = False
screen.fill(white)
pygame.display.flip()
pygame.quit()
exit()
game_loop()
#!/usr/bin/env python
发布于 2014-11-05 21:40:02
你哪儿都没给pygame.init()
打电话。
在你做游戏之前,你需要初始化它。最常见的方法就是打个电话。
pygame.init()
这将尝试为您初始化所有的游戏模块。并不是所有的游戏模块都需要初始化,但这将自动初始化那些需要初始化的模块。您还可以轻松地手工初始化每个游戏模块。例如,只初始化要调用的字体模块。
在您的特殊情况下,可能是pygame.display
在抱怨您调用它的set_caption
或flip
而没有首先调用它的init
。但是,实际上,正如本教程所述,最好只是init
顶部的所有东西,而不是试图准确地确定什么时候需要初始化。
发布于 2021-01-11 00:04:22
将代码更改为此,可以避免该错误。运行时: clock.tick(fps_cap)
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
pygame.quit()
if running:
screen.fill(white)
pygame.display.flip()
发布于 2021-09-20 23:42:33
你只需要加上
exit()
要停止运行代码示例,请执行以下操作:
for event in pygame.event.get(): #error is here
if event.type == pygame.QUIT:
running = False
exit() # Solution
https://stackoverflow.com/questions/26767591
复制相似问题