python - 如何根据图 block 的位置显示图 block ?

标签 python pygame tiles

我如何制定某种算法,根据周围的图 block 显示正确的图像。

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

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'],
    ]

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

是否有一种方法可以根据周围的图 block 在每个图 block 上显示不同的图像(以便在左上角它会检测到其下方及其右侧的图 block ,然后显示正确的图像取决于它在哪里)

这是完整的代码,您可以对其进行测试!

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

最佳答案

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

# (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]

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

关于python - 如何根据图 block 的位置显示图 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75389860/

相关文章:

Python正则表达式在字符串的开头和结尾提取子字符串

python - Pygame - 检测组中的碰撞时出错

python - pygame“screen.fill command”,我把它放在哪里?

python - 替代pygame资源

java - Spring Boot Tiles - java.lang.NoClassDefFoundError : org/apache/commons/beanutils/MethodUtils

python - 如何根据调色板设置 y 标签的颜色

Python 使用 Selenium 切换窗口

java - 使用磁贴动态组合页面

jquery - 使用磁贴对 WebFlow 进行 Ajax 调用

python - tfidf 上的 scikit-learn NearestNeighbors .kneighbors() 给出 ValueError : UPDATEIFCOPY base is read-only