我如何解决这个python / pygame程序中的“键错误”?

时间:2019-01-31 11:21:00

标签: python pygame

嗨,我正在创建一个程序,该程序使用我创建的图片创建随机地图,然后允许用户在屏幕上移动字符,我正在尝试对其进行测试,但始终会出现此错误。

  

回溯(最近通话最近):     文件 “d:\地图maker.py”,行89,在       display.blit(瓷砖[DMAP [行] [列]],(列 tileSize,行 tileSize))   KeyError:Surface(40x40x32 SW)

如果你可以看看我的代码,并告诉我如何解决它,我将非常感激。谢谢

import pygame
import sys
from pygame.locals import *
import random
MUD = 0
GRASS = 1
WATER = 2
MOUNTAIN = 3
pygame.display.set_caption('Dungeons and Dragons')
pygame.display.set_icon (pygame.image.load ('mountain.png'))
tiles = { #Creates a dictionary that links each picture to the type of ground
        MUD : pygame.image.load ('mud.png'),
        GRASS : pygame.image.load ('grass.png'),
        WATER : pygame.image.load ('water.png'),
        MOUNTAIN : pygame.image.load ('mountain.png')
        }
tileSize = 20
mapWidth = 30
mapHeight = 20
treasure = pygame.image.load ('treasure.png')
key = pygame.image.load ('key.png')
player = pygame.image.load ('player.png')
pposition = [0,0] # sets players start position
dmap = [[random.choice(tiles) for w in range(mapWidth)] for h in range (mapHeight)]
pygame.init()
display = pygame.display.set_mode ((mapWidth*tileSize, mapHeight*tileSize))
while True:#if the user quits the game closes
    for event in pygame.event.get():
        if event.type == QUIT:
             pygame.quit()
             sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT and pposition[0] < mapWidth -1:
                pposition[0] += 1
                num = random.randint (1-100) # chooses a random number between 1 and 100
                if num <= 10:
                    health -10 #if num is less than 10 than an enemy attacks and user loses health
                if health <= 0: # if the user loses all health then game is over
                    pygame.quit()
                    sys.exit()

            if event.key == K_LEFT and pposition[0] > 0: 
                pposition[0] -=1
                num = random.randint (1-100)
                if num <= 10:
                    health -10
                if health <= 0:
                    pygame.quit()
                    sys.exit()
            if event.key == K_UP and pposition[1] > 0: 
                pposition[1] -=1
                num = random.randint (1-100)
                if num <= 10:
                    health -10
                if health <= 0:
                    pygame.quit()
                    sys.exit()
            if event.key == K_DOWN and pposition[1] < mapHeight -1:
                pposition[1] +=1
                num = random.randint (1-100)
                if num <= 10:
                    health -10
                if health <= 0:
                    pygame.quit()
                    sys.exit()
    for row in range (mapHeight):
        for column in range(mapWidth):
            display.blit(tiles[dmap[row][column]], (column*tileSize,row*tileSize))
            display.blit(player,(pposition[0]*tileSize,pposition[1]*tileSize))
    pygame.display.update()

1 个答案:

答案 0 :(得分:0)

您在这里有错误;

for row in range (mapHeight):
    for column in range(mapWidth):
        display.blit(tiles[dmap[row][column]], (column*tileSize,row*tileSize))  # wrong
        display.blit(player,(pposition[0]*tileSize,pposition[1]*tileSize))

应该是这样;

for row in range (mapHeight):
    for column in range(mapWidth):
        display.blit(dmap[row][column], (column*tileSize,row*tileSize))  # fixed
        display.blit(player,(pposition[0]*tileSize,pposition[1]*tileSize))
相关问题