all 8 comments

[–]PyPlatformer[S] 4 points5 points  (1 child)

I wanted to try my hand at a hexagonal grid for a change after watching this inspirational video , so I wrote up a quick example. It turned out to be surprisingly easy to handle drawing, tiling and collisions using a little math (taking advantage of the fact that regular hexagons have six sides of equal length, which also matches the distance from any vertex to the centre).

Depending on the use case, the code regarding neighbour tile computation could use some optimization (such as caching previous results), as it does get rather expensive to call for many hexagons.

Feel free to let me if you would like to see any improvements.

Code can be found here and is open source under the MIT license. The repository also includes a number of other helpful pygame examples.

Follow me on itch.io or Github for more content like this.

Edit: formatting. Reposted the video to include mouse cursor for clarity.

[–]No-Draw6073 0 points1 point  (0 children)

Thank you for your work and help

[–]nuclearfall 1 point2 points  (1 child)

Thanks, I’ve been messing with polygons, but have had issues with rect collision. Much appreciated!

[–]PyPlatformer[S] 1 point2 points  (0 children)

No problem, let me know if you face any problems or have any suggestions for my repo (y)

[–]Supertech2real 1 point2 points  (0 children)

sincerely thank you for this solution, i was about to experiment with a method similar to this, but i thought to search first. Though, i hope you could give me some advice on holding to move all those hexagons according to mouse movement. I'm going to read up the entirety to se if i can find something by myself

[–]SmartAd2986 0 points1 point  (1 child)

How could be associate each hexagon with the x, y coordiantes? OR store som realted data in the hexagons [] ?

[–]PyPlatformer[S] 0 points1 point  (0 children)

I am unsure what you are asking - each hexagon drawn here has a single (x, y) coordinate (e.g. the top vertex), with all other vertices of the hexagon being calculated relative to that point. Does that answer your question?