首页 > 解决方案 > 为什么我收到错误“pygame.error:没有可用的视频设备”?

问题描述

这是我的代码:

import pygame, sys
from pygame.locals import *

pygame.init()
#set up window
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32) 


#set up the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

#draw on the surface object
DISPLAYSURF.fill(WHITE)
pygame.draw.polygon(DISPLAYSURF, GREEN, ((146, 0), (291,106), (236, 277), (56, 277), (0,106)))
pygame.draw.line(DISPLAYURF, BLUE, (60,60), (120,60), 4)
pygame.draw.line(DISPLAYSURF, BLUE, (120,60), (60,120))
pygame.draw.line(DISPLAYSURF, BLUE, (60, 120), (120,120), 4)
pygame.draw.circle(DISPLAYSURF, BLUE, (300,50), 20, 0)
pygame.draw.ellipse(DISPLAYSURF, RED, (300, 250, 40, 80), 1)
pygame.draw.rect(DISPLAYSURF, RED, (200,150,100,50))

pixObj = pygame.PixelArray(DISPLAYSURF)
pixObj[480][380]= BLACK
pixObj[482][382]= BLACK
pixObj[484][384]= BLACK
pixObj[486][386]= BLACK
pixObj[488][388]= BLACK
del pixObj

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

我真的不确定这个错误是什么意思,我试着用谷歌搜索它,实际上没有其他人收到这个错误消息。它实际上与我试图在 PDF 上执行的代码完全相同,但它不起作用。我刚开始pygame。这是错误消息:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)
pygame.error: No available video device

标签: pygame

解决方案


尝试在 pygame 存储库中搜索有关特定错误的信息和示例。它似乎表明必须定义系统。

下面的代码来自
https://github.com/search?q=pygame.error+No+available+video+device&type=Code

## If you get the no available video device error, copy and paste the below code ##
import platform
if platform.system() == "Windows":
### If you get the no available video device error, copy and paste the above code ###

此外,一个带有一些答案的 SO 帖子:
pygame.error:没有可用的视频设备


**更新:下面的链接似乎在 repl.it 中有一个工作的 pygame

https://repl.it/@amasad/simple-platformer


推荐阅读