Python 我怎样才能给瓷砖贴图中的瓷砖一个精灵?

Python 我怎样才能给瓷砖贴图中的瓷砖一个精灵?,python,pygame,sprite,Python,Pygame,Sprite,我对Python非常陌生,这是一个非常新的问题,但是如何在tilemap精灵中分配瓷砖呢?目前,我只是在tilemap中绘制矩形,它们应该在哪里,效果很好。但是,我想添加自己的纹理 import pygame pygame.init() tilesize = 64 mapwidth, mapheight = 20, 13 screen = pygame.display.set_mode((mapwidth*tilesize, mapheight*tilesize)) WALLTOP, WAL

我对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()

将drawing rect替换为对应于地图每个元素的布点图像

用如下字典替换
颜色和颜色定义:

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
,但也可以是其他格式,有关支持的格式的更多信息,请阅读我链接的文档

要blit图像,请在主循环中将
draw.rect
行替换为:

screen.blit(tileimages[tilemap[row][column]], (column*tilesize, row*tilesize))
小心:这里假设图像的大小正确,因此它是
tilesize x tilesize
图像(在pygame中成为
tilesize x tilesize
曲面)。如果不是这样,结果将是糟糕的。您需要使用将其重新缩放为
tilesize x tilesize
曲面

因此,您的
tileimages
dict应该如下所示:

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)),
}

哇,这成功了!非常感谢你。我忘记了blit部分,所以我被困了一段时间。干杯