如何检查特定磁贴周围的磁贴?

2024-09-29 01:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

2条回答

我先生成一组偏移量

// 1
//0 2  Neighbour directions
// 3
//       0  1  2  3  Corresponding offsets
dx   = [-1, 0, 1, 0]
dy   = [0, -1, 0, 1]
Nmax = 4

现在,我可以得到相邻墙的n值,如下所示:

^{pr2}$

现在,我可以将列表转换为互动程序名称:

#nwalls is a list of all the directions pointing to a wall
nwalls.sort()            #Sort list, e.g. 3,1,2 -> 1,2,3
nwalls = map(str,nwalls) #Convert from numbers to strings
nwalls = ''.join(nwalls) #Append strings together: ['1','2','3'] -> '123'
if nwalls:               #If the string is not empty
  nwalls = 'corner_tile_{0}.jpg'.format(nwalls) #Convert string to tile name

现在我只需要一堆名为的瓷砖,例如:

corner_tile_01.jpg
corner_tile_013.jpg

然后我可以说:

if nwalls: #Only true if this was a corner
  Code to display the tile whose name is stored in `nwalls`

从评论来看,我不确定问题出在哪里。。。让我告诉你我会怎么做,这样我们可以在需要时进一步讨论:

for x in range(MAPWIDTH):
    for y in range(MAPHEIGHT):
        if tilemap[x][y] == WALL:
            # there is a wall at indices x and y
            # get neighbouring tiles (check for limits)
            go_left  = x > 1
            go_right = x < MAPWIDTH - 1
            go_up    = y > 1
            go_down  = y < MAPHEIGHT - 1

            if go_left:
                # you can use x-1
                tilemap[x-1][y] = WALL  # set left tile to WALL
                if go_up:
                    # do something in the diagonal with x-1 y-1?
                    pass
            if go_right:
                # you can use x+1
                tilemap[x+1][y] = WALL  # set right tile to WALL
            if go_up:
                pass # same story
            if go_down:
                pass # and again

编辑这里有一个简单(因此希望容易理解)的方法

为了使纹理干净,我首先必须旋转墙和角,以获得所有可能的配置(垂直/水平的墙,所有四种可能的角落)

^{pr2}$

我创建了一个wall dict,用in walls快速检查墙的所有6种可能性

walls = {
    VWALL : None,
    HWALL : None,
    CORNERLD: None,
    CORNERRD: None,
    CORNERLU: None,
    CORNERRU: None,
} 

然后我检查地图

for x in range(MAPWIDTH):
    for y in range(MAPHEIGHT):
        if tilemap[x][y] in walls:
            # there is a wall at indices x and y
            # get neighbouring tiles (check for limits)
            go_left  = x > 1
            go_right = x < MAPWIDTH - 1
            go_up    = y > 1
            go_down  = y < MAPHEIGHT - 1

            l_wall = False
            r_wall = False
            u_wall = False
            d_wall = False

            if go_left and tilemap[x-1][y] in walls:
                # left tile is WALL
                l_wall = True
            if go_right and tilemap[x+1][y] in walls:
                # right tile is WALL
                r_wall = True
            if go_up and tilemap[x][y-1] in walls:
                u_wall = True
            if go_down and tilemap[x][y+1] in walls:
                d_wall = True

            if l_wall and u_wall:
                # upper left corner
                tilemap[x][y] = CORNERLU
            elif l_wall and d_wall:
                # down left corner
                tilemap[x][y] = CORNERRU
            elif r_wall and u_wall:
                # upper left corner
                tilemap[x][y] = CORNERLD
            elif r_wall and d_wall:
                # down left corner
                tilemap[x][y] = CORNERRD
            elif (l_wall or r_wall) and not (u_wall or d_wall):
                # tiles in a vertical wall, use VWALL
                tilemap[x][y] = VWALL
            elif (u_wall or d_wall) and not (l_wall or r_wall):
                # tiles in a horizontal wall, use HWALL
                tilemap[x][y] = HWALL

我们得到了

Your new Minecraft 2D

请注意,有一些随机的墙壁配置看起来不太好,尽管(T形…),但这些将需要额外的精灵。在

我使用的完整代码可以找到here

EDIT2请注意,为了使一切顺利进行,您还需要更新一些内容(例如库存中的墙摄取) 另外,运行这个check every循环的代价很高,因此您应该声明一个env_changed布尔值,以便只在环境发生更改时进行测试。在

对于库存,您需要

if currentTile in walls:
    inventory[VWALL] += 1

这使得VWALL成为库存中的默认墙,并且循环负责将其切换到一个合适的墙以供显示。 剩下的,嗯。。。这是你的游戏,所以我让你想办法;)

相关问题 更多 >