【问题标题】:How do i display tiles depending where they are?我如何根据它们的位置显示图块?
【发布时间】:2023-02-12 11:21:09
【问题描述】:

我将如何制作某种算法来根据周围的图块显示正确的图像。

这就是我定义关卡的方式,然后我使用“for 循环”将每个图块绘制到屏幕上

level = [
['1','1','1','1','1','1','1','1','1','1'],
['1','0','0','0','0','0','0','0','0','1'],
['1','0','0','0','0','0','0','0','0','1'],
['1','0','0','1','1','1','0','0','0','1'],
['1','0','0','0','1','0','0','0','0','1'],
['1','0','0','0','1','1','0','0','0','1'],
['1','0','0','0','0','1','0','0','0','1'],
['1','0','0','0','0','0','0','0','0','1'],
['1','0','0','0','0','0','0','0','0','1'],
['1','1','1','1','1','1','1','1','1','1'],
]

现在,我有一个包含所有图块的 png 文件,如果我愿意,我可以显示角落和所有正确的方向,但如果我只是快速更改我的地图,我将不得不重做所有内容!

是否有一种方法可以根据周围的瓷砖在每个瓷砖上显示不同的图像(这样在左上角的角落它会检测到它下面和右边的瓷砖,然后根据位置显示正确的图像这是)

这是完整的代码,所以你可以测试它!

import pygame

# Initialize Pygame
pygame.init()

# Set the size of the window
size = (360, 360)
screen = pygame.display.set_mode(size)

# Set the title of the window
pygame.display.set_caption("TILE MAP AAAaaAH")


tilesize = 30
level = [
    ['1','1','1','1','1','1','1','1','1','1'],
    ['1','0','0','0','0','0','0','0','0','1'],
    ['1','0','0','0','0','0','0','0','0','1'],
    ['1','0','0','1','1','1','0','0','0','1'],
    ['1','0','0','0','1','0','0','0','0','1'],
    ['1','0','0','0','1','1','0','0','0','1'],
    ['1','0','0','0','0','1','0','0','0','1'],
    ['1','0','0','0','0','0','0','0','0','1'],
    ['1','0','0','0','0','0','0','0','0','1'],
    ['1','1','1','1','1','1','1','1','1','1'],
    ]
tiles = []
def build_level():
    x = 0
    y = 0
    for row in level:
        y += 1
        x = 0
        for tile in row:
            x += 1
            if tile == '1':
                build = pygame.Rect(x*tilesize, y*tilesize, tilesize, tilesize)
                tiles.append(build)
            if tile == '0':
                pass
build_level()

def draw_level():
    for tile in tiles:
        pygame.draw.rect(screen, (50, 50, 50), tile)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Main Loop
    screen.fill((50, 50, 250))
    draw_level()


    pygame.display.update()

pygame.quit()

【问题讨论】:

    标签: python pygame tiles


    【解决方案1】:

    一种快速的方法是创建一个字典,其中包含代表四个主要方向上是否存在瓷砖的元组键,例如 (0, 0, 1, 1) 的键可以表示“没有瓷砖左边”、“上面没有瓷砖”、“右边有瓷砖”和“下面有瓷砖”。使用相应的图像初始化字典键。当绘制一个图块时,获取它的位置,获取相邻的图块(无论它们是否存在),并将它们排列在一个元组中,该元组将用于从字典中获取相应的图像,这是一个类似于您的粗略示例做:

    # (left, top, right, bottom)
    images = {
        (1, 1, 1, 1): [all sides connected tile image],
        (0, 0, 0, 0): [normal tile image],
        (1, 0, 0, 0): [left connected tile image],
        (0, 1, 0, 0): [top connected tile image],
        (0, 0, 1, 0): [right connected tile image],
        (0, 0, 0, 1): [bottom connected tile image],
        (1, 1, 0, 0): [left and top connected tile image]
    }
    
    def get_tile_image(x: int, y: int) -> pygame.Surface:
        key = (level[y][x - 1], level[y - 1][x], level[y][x + 1], level[y + 1][x])
        return images[key]
    

    确保您考虑到如果您为其获取图像的图块在边缘,则会出现索引错误,一些边缘检查应该可以修复它:)

    【讨论】:

      猜你喜欢
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多