Best way to save triangular / hexagonal mesh in Python

I am making a game with hexagonal tiles and decided to use a triangular / hexagonal grid. I found this question that helped me generate the coordinates and slightly modified the code to store all the coordinates as keys in a dictionary with the values ​​".". (floor) or β€œX” (wall) and includes a function that displays a string representation of the map, where each non-empty character is 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 coordinates in a radius like this:

 import hexgrid hg = hexgrid.HexGrid(radius) 

and get the following coordinate:

 hg.tiles[a,b,c] 

This works fine right now, but I'm sure there should be this way to store the card. If there are any flaws, could you please indicate them, and maybe it is best to save the map? Thanks so much for your time.

+6
source share
3 answers

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

However, you missed a very simple way to manage such a card. Think of these as rows and columns, only 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 like a regular 2D array. The odd lines are offset .5 to the right, and you need to find out the steps of the neighbors for X : above: A = (0,-2) , top right: B = (1,-1) , bottom right: C = (1,1) , bottom: D = (0,2), bottom left: E = (0,1) , top left: F = (0,-1)

If you are fine with a bit of memory, you can also leave all the other columns empty, and the neighborhood will be a little easier: (0,-2), (1,-1), (1,-1), (0,-2), (-1,-1), (-1,1)

+9
source

Do not use hg.tiles [a, b, c].

Make the tiles a three-dimensional list this way hg.tiles = [[[z for z in range(10)] for y in range(10)] for x in range(10)] you can now access the tile using hg.tiles[a][b][c]

PS: a = a+deltas[j][0] should be a += deltas[j][0] , etc. for other purposes

0
source

I did some research too and found a much simpler way to do this. You don’t need to get as complicated as you are! A table can be a simple array array without any special rules.

You want to use the hexagonal root coordination system. See Theory here: https://en.wikipedia.org/wiki/Root_system . Also https://www.redblobgames.com/grids/hexagons/

The cell (0,0) is located in the center of the structure, then it has six neighbors: as in the well-known orthogonal table (1,0), (0,1), (-1,0), (0, -1), but also (1,1), (-1-1). Other cameras also have six neighbors, there is no need for a module!

Here are some askii art for understanding:

  _____ _____ ____ __ / -2,2\_____/ 0,1 \____/2,0 \____/ \__ \_____/-1,1 \_____/ 1,0\____/3,-1\__/ /-2,1 \_____/0,0 \____/2,-1\____/ \__ \_____/-1,0 \_____/1,-1\____/3,-2\__/ /-2,0 \_____/ 0,-1\____/2,-2\____/ \__ \_____/ \_____/ \____/ \__/ 

You can calculate the position of the center of each cell in the plane (and so on the screen), because it follows the rules of vector geometry. The vector has coordinates that are 60 Β° instead of 90 Β°: a = (0,1), but b = (0,87,0,5), just multiply and add these coordinates!

You can use pyhton librairy hexy: https://github.com/RedFT/Hexy

0
source

Source: https://habr.com/ru/post/919922/


All Articles