python - Python3 错误:“pygame.error:视频系统未初始化”
问题描述
我正在尝试学习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
我看了很多帖子,但没有用。请告诉我如何解决这个问题。
解决方案
如果您初始化需要使用 pygame.init() 的 pygame“模块”并引发错误,则不会显示此错误或类似这样的错误。您可以使用 pygame.display.init() 之类的方法初始化所有“内部 pygame 模块”,如果您这样做,则会打印回溯。我也没有任何问题使用 pycharm 来执行这个程序。也许这与您使用的 unbuntu 终端有关。尝试使用 pygame.display.init() 初始化显示类 这里有一个关于我所说的链接:pygame.org
推荐阅读
- c++ - 如何使用 gnu gcc 标志 -mpc32、-mpc64 和 -mpc80?
- logging - 我正在从 cmd 连接到我的服务器并运行一些脚本。我需要将这些脚本的日志保存到我的本地桌面文件夹中
- database - 将 UserState 存储在外部数据库中的最佳密钥
- python - 使用 Beautiful Soup 从选定标签中提取文本
- ios - AVAudioEngineImpl::IOUnitConfigurationChanged() 崩溃
- java - 使用 org.json 库格式化 LocalDateTime 的问题
- postfix-mta - DKIM 仍被声明为无
- cypress - 赛普拉斯测试 - 期望输入为空
- node.js - 为什么反应创建应用程序中缺少 src 和公用文件夹?
- python - 如何使用 Python 从 PDF 文件中读取简单文本?