Python3错误。"pygame.error: video system not initialized"

问题描述 投票:0回答:1

我正在努力学习用以下方法制作游戏 pygame 模组 python.我在Windows 10中使用ubuntu终端。这是我的程序。

# 1 - Import library
import os
os.environ['SDL_AUDIODRIVER'] = 'dummy'
import pygame
from pygame.locals import *

# 2 - Initialize the game
print("-1")
pygame.init()
pygame.display.list_modes()

print("0")
width, height = 640, 480
print("00")
screen=pygame.display.set_mode((width, height))
print("1")

# 3 - Load images
player = pygame.image.load("resources/images/virus.png")
print("3")
# 4 - keep looping through
while 1:
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    print("4")
    # 6 - draw the screen elements
    screen.blit(player, (100,100))
    print("5")
    # 7 - update the screen
    pygame.display.flip()
    # 8 - loop through the events
    for event in pygame.event.get():
        # check if the event is the X button 
        if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit() 
            exit(0) 

只要我运行程序,我就会得到这个错误。

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
-1
Traceback (most recent call last):
  File "pythongame.py", line 10, in <module>
    pygame.display.list_modes()
pygame.error: video system not initialized

我看了很多帖子,但没有用。请告诉我如何解决这个问题。

python python-3.x pygame
1个回答
0
投票

如果你使用pygame.init()初始化需要的pygame "模块",并且抛出一个错误,这个错误就不会显示或类似于这样。你可以使用 pygame.display.init()这样的方法初始化所有的 "内部 pygame 模块",如果你这样做,会有一个跟踪打印出来。我使用pycharm执行这个程序也没有任何问题。也许是你使用的unbuntu终端有问题。试着用 pygame.display.init()初始化显示类,这里有一个关于我说的链接。pygame.org

© www.soinside.com 2019 - 2024. All rights reserved.