Python(Pygame)键错误

时间:2015-06-07 01:14:42

标签: python pygame

我正在使用tilemap在pygame中制作游戏,我遇到了以下错误

"Traceback (most recent call last):
    File "C:\Users\Dusty\Dropbox\~MAIN FOLDER~\Projects\Sunless\game.py", line 52, in <module>
    surface.blit(textures[tileMap[row][column]], (column*tileSize, row*tileSize, tileSize, tileSize))
KeyError: 174"

使用此代码:

import pygame

gameIsRunning = True

tileSize = 32
mapHeight = 19
mapWidth = 25

surface = pygame.display.set_mode((tileSize * mapWidth, tileSize * mapHeight))
pygame.display.set_caption("Game")

floor = 109
wall = 166

textures = {
            floor : pygame.image.load('data/floor.png'),
            wall: pygame.image.load('data/wall.png'),

            }

tileMap = [
           [109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109],
           [109, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 109, 166, 166, 166, 166, 166, 166, 166, 166, 109],
           [109, 166, 109, 109, 109, 109, 109, 109, 109, 166, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 166, 109, 109, 109],
           [109, 166, 109, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 109, 109, 109, 109, 166, 109, 109, 109, 109, 109, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 166, 166, 166, 109, 166, 166, 166, 166, 166, 166, 109, 166, 109, 166, 166, 166, 166, 166, 166, 109],
           [109, 166, 166, 166, 166, 166, 109, 166, 166, 166, 109, 109, 166, 109, 166, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 166, 109, 109, 109, 166, 166, 166, 166, 109, 166, 109, 166, 166, 166, 166, 109, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 166, 166, 166, 166, 166, 109, 109, 166, 109, 166, 166, 166, 109, 166, 109, 166, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 166, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 166, 166, 166, 109, 166, 166, 166, 166, 166, 166, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 109, 109, 166, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109, 166, 166, 109, 166, 166, 166, 109],
           [109, 109, 109, 166, 166, 166, 166, 166, 166, 166, 109, 166, 109, 109, 166, 109, 166, 109, 166, 109, 109, 166, 109, 109, 109],
           [109, 166, 109, 166, 109, 109, 166, 109, 109, 166, 109, 166, 109, 166, 166, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109],
           [109, 166, 109, 166, 109, 109, 166, 109, 109, 166, 109, 166, 166, 166, 109, 109, 166, 109, 166, 109, 109, 166, 109, 166, 109],
           [109, 166, 109, 166, 166, 166, 166, 166, 109, 166, 109, 166, 166, 109, 109, 166, 166, 109, 166, 109, 109, 166, 109, 166, 109],
           [109, 166, 109, 109, 109, 109, 109, 166, 109, 166, 166, 166, 166, 166, 166, 166, 166, 109, 166, 109, 109, 174, 166, 166, 109],
           [109, 166, 166, 166, 166, 166, 166, 166, 166, 166, 109, 109, 109, 109, 166, 166, 166, 109, 166, 166, 166, 166, 109, 166, 109],
           [109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109]
            ]

def gameQuit():
    pygame.quit()
    quit()
while gameIsRunning == True:
    surface.fill((0, 0, 0))

    for row in range(mapHeight):
        for column in range(mapWidth):
            surface.blit(textures[tileMap[row][column]], (column*tileSize, row*tileSize, tileSize, tileSize))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameQuit()

1 个答案:

答案 0 :(得分:1)

小提示,如果你想使用瓷砖地图使用Tiled制作游戏,也可以使用TMX import library by Richard Jones

此外,您似乎正在尝试访问纹理中不存在的元素。 tileMap中的值为174(距离结尾的第3行)。

surface.blit(textures[tileMap[row][column]]

在这里,您可以访问纹理词典中不存在的元素。它被解释为:

textures[174]

由于纹理中没有这样的键,你就会出错。