如何为我的 tilemap 中的图块赋予 sprite?
How can I give the tiles in my tilemap a sprite?
我是 Python 的新手,这是一个新手问题,但是我如何在我的 tilemap 精灵中分配图块?目前,我只是在 tilemap 中它们应该在的位置绘制矩形,它工作正常。但是,我想添加自己的纹理。
import pygame
pygame.init()
tilesize = 64
mapwidth, mapheight = 20, 13
screen = pygame.display.set_mode((mapwidth*tilesize, mapheight*tilesize))
WALLTOP, WALLBOT, GRASS = range(3)
wallTop = (0, 102, 0)
wallBot = (51, 51, 0,)
grass = (0, 65, 0)
colors = {
WALLTOP : wallTop,
WALLBOT : wallBot,
GRASS : grass
}
# Tilemap
tilemap = [
[WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP],
[WALLTOP, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP, WALLTOP, WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP, WALLTOP, WALLBOT, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLBOT, WALLBOT, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP],
# Main loop
running = True
while running:
# Loop through the rows of the tilemap
for row in range(mapheight):
# Loop through the columns of the tilemap
for column in range(mapwidth):
# Draw the picture at the position in the tilemap
pygame.draw.rect(screen, colors[tilemap[row][column]], (column*tilesize, row*tilesize, tilesize, tilesize))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
用与地图的每个元素对应的 blitting 图像替换绘图矩形。
用这样的字典替换 colors
字典和颜色定义:
tileimages = {
WALLTOP : pygame.image.load("path/to/image_walltop.png").convert(),
WALLBOT : pygame.image.load("path/to/image_wallbot.png").convert(),
GRASS : pygame.image.load("path/to/image_grass.png").convert(),
}
pygame.image.load 将加载图像,当然需要使用图像编辑器或其他任何工具提前准备好图像并将其存储在系统的某个位置。请务必提供正确的路径。
我在这里使用 png
,但也可以是其他格式,有关支持格式的更多信息,请阅读我链接的文档。
要 blit 图像,请在主循环中将 draw.rect
行替换为:
screen.blit(tileimages[tilemap[row][column]], (column*tilesize, row*tilesize))
注意:这里假设图片大小正确,所以它是一张tilesize x tilesize
图片(变成tilesize x tilesize
表面在pygame).如果不是这样,结果会很糟糕。您需要使用 pygame.transform.scale 将其重新缩放到 tilesize x tilesize
表面。
因此您的 tileimages
字典应该如下所示:
tileimages = {
WALLTOP : pygame.transform.scale(pygame.image.load("path/to/image_walltop.png").convert(), (tilesize, tilesize)),
WALLBOT : pygame.transform.scale(pygame.image.load("path/to/image_wallbot.png").convert(), (tilesize, tilesize)),
GRASS : pygame.transform.scale(pygame.image.load("path/to/image_grass.png").convert(), (tilesize, tilesize)),
}
我是 Python 的新手,这是一个新手问题,但是我如何在我的 tilemap 精灵中分配图块?目前,我只是在 tilemap 中它们应该在的位置绘制矩形,它工作正常。但是,我想添加自己的纹理。
import pygame
pygame.init()
tilesize = 64
mapwidth, mapheight = 20, 13
screen = pygame.display.set_mode((mapwidth*tilesize, mapheight*tilesize))
WALLTOP, WALLBOT, GRASS = range(3)
wallTop = (0, 102, 0)
wallBot = (51, 51, 0,)
grass = (0, 65, 0)
colors = {
WALLTOP : wallTop,
WALLBOT : wallBot,
GRASS : grass
}
# Tilemap
tilemap = [
[WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP],
[WALLTOP, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP, WALLTOP, WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP, WALLTOP, WALLBOT, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLBOT, WALLBOT, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP],
[WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP, WALLTOP],
# Main loop
running = True
while running:
# Loop through the rows of the tilemap
for row in range(mapheight):
# Loop through the columns of the tilemap
for column in range(mapwidth):
# Draw the picture at the position in the tilemap
pygame.draw.rect(screen, colors[tilemap[row][column]], (column*tilesize, row*tilesize, tilesize, tilesize))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
用与地图的每个元素对应的 blitting 图像替换绘图矩形。
用这样的字典替换 colors
字典和颜色定义:
tileimages = {
WALLTOP : pygame.image.load("path/to/image_walltop.png").convert(),
WALLBOT : pygame.image.load("path/to/image_wallbot.png").convert(),
GRASS : pygame.image.load("path/to/image_grass.png").convert(),
}
pygame.image.load 将加载图像,当然需要使用图像编辑器或其他任何工具提前准备好图像并将其存储在系统的某个位置。请务必提供正确的路径。
我在这里使用 png
,但也可以是其他格式,有关支持格式的更多信息,请阅读我链接的文档。
要 blit 图像,请在主循环中将 draw.rect
行替换为:
screen.blit(tileimages[tilemap[row][column]], (column*tilesize, row*tilesize))
注意:这里假设图片大小正确,所以它是一张tilesize x tilesize
图片(变成tilesize x tilesize
表面在pygame).如果不是这样,结果会很糟糕。您需要使用 pygame.transform.scale 将其重新缩放到 tilesize x tilesize
表面。
因此您的 tileimages
字典应该如下所示:
tileimages = {
WALLTOP : pygame.transform.scale(pygame.image.load("path/to/image_walltop.png").convert(), (tilesize, tilesize)),
WALLBOT : pygame.transform.scale(pygame.image.load("path/to/image_wallbot.png").convert(), (tilesize, tilesize)),
GRASS : pygame.transform.scale(pygame.image.load("path/to/image_grass.png").convert(), (tilesize, tilesize)),
}