在Python中存储三角形/六边形网格的最佳方法 [英] Best Way to Store a Triangular/Hexagonal Grid in Python

查看:179
本文介绍了在Python中存储三角形/六边形网格的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用六角形瓷砖制作游戏,并决定使用三角形/六边形网格.我发现了这个问题,该问题帮助我生成了坐标,并对代码进行了少许修改以存储所有坐标作为字典中键的值为."的键. (地板)或"X"(墙),并包含一个打印地图字符串表示的函数,其中每个非空白字符都代表一个六边形图块.这是新代码:

I'm making a game with hexagonal tiles, and have decided upon using a triangular/hexagonal grid. I found this question which helped me generate coordinates, and slightly modified the code to store all the coordinates as keys in a dictionary with values of either "." (floor) or "X" (wall,) and included a function that prints out a string representation of the map where each non-blank character represents a hexagonal tile. This is the new code:

deltas = [[1,0,-1],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0]]
class HexGrid():
    def __init__(self, radius):
        self.radius = radius
        self.tiles = {(0, 0, 0): "X"}
        for r in range(radius):
            a = 0
            b = -r
            c = +r
            for j in range(6):
                num_of_hexas_in_edge = r
                for i in range(num_of_hexas_in_edge):
                    a = a+deltas[j][0]
                    b = b+deltas[j][1]
                    c = c+deltas[j][2]           
                    self.tiles[a,b,c] = "X"

    def show(self):
        l = []
        for y in range(20):
            l.append([])
            for x in range(60):
                l[y].append(".")
        for (a,b,c), tile in self.tiles.iteritems():
            l[self.radius-1-b][a-c+(2*(self.radius-1))] = self.tiles[a,b,c]
        mapString = ""
        for y in range(len(l)):
            for x in range(len(l[y])):
                mapString += l[y][x]
            mapString += "\n"
        print(mapString)

使用此代码,我可以像这样生成半径内的所有坐标:

With this code, I can generate all the coordinates within the radius like so:

import hexgrid
hg = hexgrid.HexGrid(radius)

并访问这样的坐标:

hg.tiles[a,b,c]

这似乎现在可以正常工作,但是我确定以这种方式存储地图肯定会有一些缺点.如果有任何不利之处,可以请您指出,并提出一种更好的存储地图的方法吗?非常感谢您的宝贵时间.

This seems to work fine for now, but I'm sure there must be some disadvantages to storing the map this way. If there are any disadvantages, could you please point them out, and maybe present a better way to store the map? Thanks a lot for your time.

推荐答案

使用数组进行存储可以节省一些CPU时间,但是区别可能很小.

Using an array for storage may save you some CPU time, but the difference is probably neglible.

但是,您错过了一种非常简单的管理此类地图的方法.认为它是行和列,只是单元格的形状略有不同.

However, you missed a very simple way of managing such a map. Consider it to be rows and columns, just the cells have slightly different shapes.

+--+--+--+--+--+--+--+
 \/ \/ \/ \/ \/ \/ \/    Even row

  /\ /\ /\ /\ /\ /\ /\   Odd row
 +--+--+--+--+--+--+--+

或者对于六边形:

  __    __    __    __
 /  \__/  \__/  \__/  \__ Even row
 \__/  \__/ A\__/  \__/   Odd  row
 /  \__/ F\__/ B\__/  \__ Even row
 \__/  \__/ X\__/  \__/   Odd  row
 /  \__/ E\__/ C\__/  \__ Even row
 \__/  \__/ D\__/  \__/   Odd  row
 /  \__/  \__/  \__/  \__ Even row
 \__/  \__/  \__/  \__/   Odd  row

然后,您可以将数据存储为常规2D数组.奇数行向右偏移.5,并且您需要计算X的邻居步骤:上:A = (0,-2),右上:B = (1,-1),右下:C = (1,1),下:D = (0,2),左:E = (0,1),左上:F = (0,-1)

Then you can store the data just as a regular 2D array. Odd rows are offset .5 to the right, and you need to figure out the neighborship steps for X: above: A = (0,-2), up right: B = (1,-1), bottom right: C = (1,1), below: D = (0,2), bottom left: E = (0,1), top left: F = (0,-1)

如果您可以浪费一点内存,也可以将其他所有列都留空,并且邻居关系会变得更简单:(0,-2), (1,-1), (1,-1), (0,-2), (-1,-1), (-1,1)

If you are ok with wasting a bit of memory, you can also leave every other column empty, and the neighborship becomes a bit simpler: (0,-2), (1,-1), (1,-1), (0,-2), (-1,-1), (-1,1)

这篇关于在Python中存储三角形/六边形网格的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆