我对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()
答案 0 :(得分:1)
用与地图每个元素相对应的blitting图像替换绘图rect。
用这样的字典替换cxx_v = @cxx compute_sum(x)
字典和颜色定义:
colors
pygame.image.load将加载图像,这当然需要事先使用图像编辑器或其他工具进行准备,并存储在系统中的某个位置。请确保提供正确的路径。
在这里,我使用了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(),
}
,但可能是其他格式,有关受支持格式的更多信息,请阅读我链接的文档。
要使图像变暗,请在主循环中将png
行替换为:
draw.rect
注意::此处假定图像的大小正确,因此它是screen.blit(tileimages[tilemap[row][column]], (column*tilesize, row*tilesize))
图像(在pygame中变为tilesize x tilesize
Surface)。如果不是这种情况,结果将很糟糕。您需要使用pygame.transform.scale将其缩放到tilesize x tilesize
表面。
所以您的tilesize x tilesize
字典应该像这样:
tileimages