pygame.error:未设置视频模式

Posted

技术标签:

【中文标题】pygame.error:未设置视频模式【英文标题】:pygame.error: no video mode has been set 【发布时间】:2017-05-28 21:09:24 【问题描述】:

错误:

File "E:/advcomp/game/mainGame.py", line 3, in <module>
    from Loading import *
  File "E:\advcomp\game\Loading.py", line 37, in <module>
    gameMap = pytmx.load_pygame("gameMap.tmx", pixelalpha = True)
  File "C:\Users\Administrator.BAC-F060-D003.000\Desktop\pythonFiles\lib\site-packages\pytmx\util_pygame.py", line 116, in load_pygame
    return pytmx.TiledMap(filename, *args, **kwargs)
  File "C:\Users\Administrator.BAC-F060-D003.000\Desktop\pythonFiles\lib\site-packages\pytmx\pytmx.py", line 285, in __init__
    self.parse_xml(ElementTree.parse(self.filename).getroot())
  File "C:\Users\Administrator.BAC-F060-D003.000\Desktop\pythonFiles\lib\site-packages\pytmx\pytmx.py", line 357, in parse_xml
    self.reload_images()
  File "C:\Users\Administrator.BAC-F060-D003.000\Desktop\pythonFiles\lib\site-packages\pytmx\pytmx.py", line 429, in reload_images
    image = loader()
  File "C:\Users\Administrator.BAC-F060-D003.000\Desktop\pythonFiles\lib\site-packages\pytmx\util_pygame.py", line 90, in load_image
    tile = smart_convert(tile, colorkey, pixelalpha)
  File "C:\Users\Administrator.BAC-F060-D003.000\Desktop\pythonFiles\lib\site-packages\pytmx\util_pygame.py", line 54, in smart_convert
    tile = original.convert_alpha()
pygame.error: No video mode has been set

当我在Loading.py中添加以下行时首先出现错误:gameMap = pytmx.load_pygame("gameMap.tmx", pixelalpha = True)

mainGame.py:

import pygame
from settings import *
from Loading import *
from characters import *

pygame.init()

class game():

    def __init__(self):

        self.gameRunning = True
        self.gameWindow = pygame.display.set_mode((gameWindowWidth, gameWindowHeight))
        pygame.display.set_caption(title)
        self.clock = pygame.time.Clock()
        self.playerSpeed = speed

    def update(self):

        self.gameWindow.blit(distantCity, (0,0))
        for layer in gameMap.visible_layers:
            for x, y, gid, in layer:
                tile = gameMap.get_tile_image_by_gid(gid)
                self.gameWindow.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))
        self.gameWindow.blit(ninjaIdle01, (player1.playerX, player1.playerY))
        pygame.display.update()

    def gameLoop(self):

        self.clock.tick(FPS)
        self.event()
        self.update()

    def event(self):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.gameRunning = False

            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                self.playerSpeed = 5
            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                self.playerSpeed = -5
            if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
                self.playerSpeed = 0
            if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
                self.playerSpeed = 0

        player1.playerX = player1.playerX + self.playerSpeed

playGame = game()
player1 = player()
while playGame.gameRunning == True:
    playGame.gameLoop()


pygame.quit()
quit()

正在加载.py:

import pygame
import pytmx

pygame.init()

ninjaIdle01 = pygame.image.load("gameImages\s_ninjaIdle_01.png")
ninjaIdle02 = pygame.image.load("gameImages\s_ninjaIdle_02.png")
ninjaIdle03 = pygame.image.load("gameImages\s_ninjaIdle_03.png")
ninjaIdle04 = pygame.image.load("gameImages\s_ninjaIdle_04.png")
ninjaIdle05 = pygame.image.load("gameImages\s_ninjaIdle_05.png")
ninjaIdle06 = pygame.image.load("gameImages\s_ninjaIdle_06.png")
ninjaIdle07 = pygame.image.load("gameImages\s_ninjaIdle_07.png")
ninjaIdle08 = pygame.image.load("gameImages\s_ninjaIdle_08.png")
ninjaIdle09 = pygame.image.load("gameImages\s_ninjaIdle_09.png")
ninjaIdle10 = pygame.image.load("gameImages\s_ninjaIdle_10.png")
ninjaJump01 = pygame.image.load("gameImages\s_ninjaJump_01.png")
ninjaJump02 = pygame.image.load("gameImages\s_ninjaJump_02.png")
ninjaJump03 = pygame.image.load("gameImages\s_ninjaJump_03.png")
ninjaJump04 = pygame.image.load("gameImages\s_ninjaJump_04.png")
ninjaJump05 = pygame.image.load("gameImages\s_ninjaJump_05.png")
ninjaJump06 = pygame.image.load("gameImages\s_ninjaJump_06.png")
ninjaJump07 = pygame.image.load("gameImages\s_ninjaJump_07.png")
ninjaJump08 = pygame.image.load("gameImages\s_ninjaJump_08.png")
ninjaJump09 = pygame.image.load("gameImages\s_ninjaJump_09.png")
ninjaJump10 = pygame.image.load("gameImages\s_ninjaJump_10.png")
ninjaRun01 = pygame.image.load("gameImages\s_ninjaRun_01.png")
ninjaRun02 = pygame.image.load("gameImages\s_ninjaRun_02.png")
ninjaRun03 = pygame.image.load("gameImages\s_ninjaRun_03.png")
ninjaRun04 = pygame.image.load("gameImages\s_ninjaRun_04.png")
ninjaRun05 = pygame.image.load("gameImages\s_ninjaRun_05.png")
ninjaRun06 = pygame.image.load("gameImages\s_ninjaRun_06.png")
ninjaRun07 = pygame.image.load("gameImages\s_ninjaRun_07.png")
ninjaRun08 = pygame.image.load("gameImages\s_ninjaRun_08.png")
ninjaRun09 = pygame.image.load("gameImages\s_ninjaRun_09.png")
ninjaRun10 = pygame.image.load("gameImages\s_ninjaRun_10.png")
distantCity = pygame.image.load("gameImages\s_distantCity.png")
gameMap = pytmx.load_pygame("gameMap.tmx", pixelalpha = True)

gameMap.tmx:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down"   tile tile nextobjectid="1">
 <tileset firstgid="1" name="platformerTiles" tile tile tilecount="6" columns="0">
  <tile id="0">
   <image   source="gameImages/dirtLeft.png"/>
  </tile>
  <tile id="1">
   <image   source="gameImages/dirtMiddle.png"/>
  </tile>
  <tile id="2">
   <image   source="gameImages/dirtRight.png"/>
  </tile>
  <tile id="3">
   <image   source="gameImages/grassLeft.png"/>
  </tile>
  <tile id="4">
   <image   source="gameImages/grassMiddle.png"/>
  </tile>
  <tile id="5">
   <image   source="gameImages/grassRight.png"/>
  </tile>
 </tileset>
 <layer name="Tile Layer 1"  >
  <data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3
</data>
 </layer>
</map>

【问题讨论】:

顺便说一句:我们为类使用CamelCase 名称以使代码更具可读性 - class Game()class Player() BTW2:Loading.py:做一个循环! 【参考方案1】:

PyGame 加载图片和使用.convert_alpha() 必须知道窗口使用什么模式,所以你必须在加载图片之前使用pygame.display.set_mode()

【讨论】:

以上是关于pygame.error:未设置视频模式的主要内容,如果未能解决你的问题,请参考以下文章

pygame.error没有足够大的视频模式

Pygame:pygame.error:无法打开文件.ogg

Iexpress:pygame.error:文件不是 Windows BMP 文件

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

退出 pygame 窗口后出现 pygame 错误

图像格式不支持 PyGame