pygame.error:没有设置视频模式

时间:2017-01-13 13:18:46

标签: python pygame python-3.5

错误:

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()

Loading.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" width="20" height="10" tilewidth="128" tileheight="128" nextobjectid="1">
 <tileset firstgid="1" name="platformerTiles" tilewidth="128" tileheight="128" tilecount="6" columns="0">
  <tile id="0">
   <image width="128" height="128" source="gameImages/dirtLeft.png"/>
  </tile>
  <tile id="1">
   <image width="128" height="128" source="gameImages/dirtMiddle.png"/>
  </tile>
  <tile id="2">
   <image width="128" height="128" source="gameImages/dirtRight.png"/>
  </tile>
  <tile id="3">
   <image width="128" height="128" source="gameImages/grassLeft.png"/>
  </tile>
  <tile id="4">
   <image width="128" height="128" source="gameImages/grassMiddle.png"/>
  </tile>
  <tile id="5">
   <image width="128" height="128" source="gameImages/grassRight.png"/>
  </tile>
 </tileset>
 <layer name="Tile Layer 1" width="20" height="10">
  <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>

1 个答案:

答案 0 :(得分:2)

PyGame加载图片并使用.convert_alpha()必须知道窗口使用的模式,因此您必须在加载图片之前使用pygame.display.set_mode()