python蜂窝状六边形_在Python中存储三角形/六边形网格的最佳方法

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)

and access a coordinate like this:

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.

解决方案

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

+--+--+--+--+--+--+--+

Or for hexagons:

__ __ __ __

/ \__/ \__/ \__/ \__ Even row

\__/ \__/ A\__/ \__/ Odd row

/ \__/ F\__/ B\__/ \__ Even row

\__/ \__/ X\__/ \__/ Odd row

/ \__/ E\__/ C\__/ \__ Even row

\__/ \__/ D\__/ \__/ Odd row

/ \__/ \__/ \__/ \__ Even row

\__/ \__/ \__/ \__/ Odd row

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)

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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值