python - pygame 瓷砖 map 错误

标签 python pygame tile

我知道以前曾发布过类似主题的问题,但我遇到了一个在任何其他问题中都没有见过的错误。

我试图从二维数组制作平铺背景,但是当我运行它时,我收到此错误:

current_tile = 纹理[tilemap[x,y]]
类型错误:列表索引必须是整数,而不是元组

这是我的代码:

import pygame, sys
from pygame.locals import *
pygame.init()

tilesize = 32
tileswidth = 11
tilesheight = 12

screenwidth = tilesize*tileswidth
screenheight = tilesize*tilesheight
screen = pygame.display.set_mode((screenwidth,screenheight))

tilemap = [
    [9,3,2,2,2,4,2,2,2,3,9],
    [8,11,10,10,10,10,10,10,10,11,5],
    [8,1,6,9,9,9,9,9,8,1,5],
    [8,1,5,9,9,9,9,9,7,1,5],
    [8,1,6,9,9,9,9,9,7,1,5],
    [8,1,5,9,9,9,9,9,8,1,5],
    [8,1,12,4,2,3,2,4,13,1,5],
    [7,11,10,10,10,11,10,10,10,11,6],
    [9,9,9,9,7,1,6,9,9,9,9],
    [9,9,9,9,7,1,6,9,9,9,9],
    [9,9,9,9,7,1,6,9,9,9,9]
    ]

textures = {
    1 : pygame.image.load("data/tiles/1.jpg").convert_alpha(),
    2 : pygame.image.load("data/tiles/2.jpg").convert_alpha(),
    3 : pygame.image.load("data/tiles/3.jpg").convert_alpha(),
    4 : pygame.image.load("data/tiles/4.jpg").convert_alpha(),
    5 : pygame.image.load("data/tiles/5.jpg").convert_alpha(),
    6 : pygame.image.load("data/tiles/6.jpg").convert_alpha(),
    7 : pygame.image.load("data/tiles/7.jpg").convert_alpha(),
    8 : pygame.image.load("data/tiles/8.jpg").convert_alpha(),
    9 : pygame.image.load("data/tiles/9.jpg").convert_alpha(),
    10 : pygame.image.load("data/tiles/10.jpg").convert_alpha(),
    11 : pygame.image.load("data/tiles/11.jpg").convert_alpha(),
    12 : pygame.image.load("data/tiles/12.jpg").convert_alpha(),
    13 : pygame.image.load("data/tiles/13.jpg").convert_alpha()
    }

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    for x in range(tilesheight):
        for y in range(tileswidth):
            current_tile = textures[tilemap[x,y]]
            screen.blit(current_tile, (x*tilesize, y*tilesize))


    screen.fill(0,0,0)
    pygame.display.update()

如有任何帮助,我们将不胜感激。

最佳答案

TypeError: list indices must be integers, not tuple

要修复此错误,您必须使用整数(数字)而不是元组 (1,2) 作为列表索引。据我所知,您正在尝试执行二维列表[1,2],这是错误的,因为二维数组/列表是列表的列表。 这就是为什么你必须使用tilemap[x][y]。 Tilemap[x] 从tilemap 返回子列表,tilemap[x][y] 是来自 x 处的子列表,返回元素 y

关于python - pygame 瓷砖 map 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36251495/

相关文章:

python - 消除 "Game"单例中的耦合和全局状态

python - JupyterLab : WebGL is not supported by your browser

python - `time.sleep()` 和 `pygame.time.wait()` 有哪些替代方案?

python - 延迟加载 Python 模块的最佳实践

Python、pygame、py2exe

image - 使用ImageMagick重复或“平铺”图像

java - 如何在 Java 中生成 map ?

c++ - 在 SFML 中由 RectangleShape 制成的 Tilegrid 给出转换错误

Python MySQL 连接器光标打印 -1... 为什么?

python - 如何在生产环境中运行 gevent