我知道像這樣的主題的問題已經發布之前,但我有一個錯誤,我沒有看到任何其他問題。Tilemap error pygame
我試圖讓從一個二維數組平鋪背景,但是當我運行它,我得到這個錯誤:
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()
任何幫助,將不勝感激。
你還應該注意'screenheight'應該等於11,否則代碼會出現索引錯誤,因爲'tilemap'中只有11個項目,'screen.fill(0,0,0)'應該更改爲'screen.fill((0,0,0))'以避免值錯誤。 – Flutterguy135