如何根据它们的位置显示瓦片?

huangapple go评论56阅读模式
英文:

How do i display tiles depending where they are?

问题

我应该如何制作一种算法,根据周围的瓷砖显示正确的图像。

这只是我定义我的关卡的方式,然后我使用“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

# 初始化Pygame
pygame.init()

# 设置窗口的大小
size = (360, 360)
screen = pygame.display.set_mode(size)

# 设置窗口的标题
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

    # 主循环
    screen.fill((50, 50, 250))
    draw_level()

    pygame.display.update()

pygame.quit()

希望这有所帮助!

英文:

How would I make some sort of an algorithm that displays the right image based on the tiles around it.

This is just how i define my level then i use a "for loop" to draw each tile to the screen

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

Now, i have a png file with all the tiles in it and i could display the corners and everything the right orientation if i wanted to, but if i were to just do a quick change to my map i would have to redo everything!

Would there be a way to display a different image on each tile based on what tiles are around it (so that in the corner in the top left it would detect the tile under it and to it's right, then display the right image depending on where it is)

Here is the entire code so you can test it!

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

答案1

得分: 3

以下是翻译好的内容:

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

# (左, 上, 右, 下)
images = {
    (1, 1, 1, 1): [所有边都连接的瓷砖图像],
    (0, 0, 0, 0): [普通瓷砖图像],
    (1, 0, 0, 0): [左侧连接的瓷砖图像],
    (0, 1, 0, 0): [上方连接的瓷砖图像],
    (0, 0, 1, 0): [右侧连接的瓷砖图像],
    (0, 0, 0, 1): [下方连接的瓷砖图像],
    (1, 1, 0, 0): [左侧和上方连接的瓷砖图像]
}

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]

确保考虑到如果要获取图像的瓷砖位于边缘,将出现索引错误,一些边缘检查可以解决这个问题 如何根据它们的位置显示瓦片?

英文:

A fast way to do this would be to create a dictionary with tuple keys representing the presence or absence of a tile in the four cardinal directions, for example a key of (0, 0, 1, 1) could represent "no tile on the left", "no tile above", "a tile on the right", and "a tile below". Initialize the dictionary keys with the corresponding images. When drawing a tile, take its position, get the neighboring tiles (whether they are there or not), and arrange them in a tuple which would be used to get the corresponding image from the dictionary, here's a crude example similar to what you would do:

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

Make sure you consider that if the tile you are getting the image for is on the edge, there will be an index error, a few edge checks should fix it 如何根据它们的位置显示瓦片?

huangapple
  • 本文由 发表于 2023年2月9日 01:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定