Success! I’m using a modified version of the Python library from RedBlogGames’ excellent page on hexagons (seriously, it’s worth a read if you’ve ever wondered about hexagons and programming) to do a bunch of the annoying math with hexagons, and I went back and read the page to figure out why my hexes weren’t aligning properly. One of the comments was talking about a similar problem, leading me to see that the author had already thought of it and included fixes for it.
I had a floating point error in my code. Part of the code calculates the distance from the center of the hex to the corner. Initially, I was using Python’s integer division when calculating this, but it rounds down, even though the value I was rounding was ~36.95. Using the round() function got me 37, which was the result expected, and results in proper alignment of the hexes. This is because the ideal hex doesn’t have perfectly matching integer boundaries, so I’m actually deforming the high slightly from ideal to match the display’s pixels.
I was hoping this would also fix the mouse offset I was having, but I’m still seeing an ~21 pixel vertical offset upwards. I’m going to try to shelve this issue and move on with actually getting more game stuff working, including starting to implement game systems and mechanics.
After fixing the pixel alignment issue, I found the bug causing the buildings to not draw properly. I realized that since transparency was working elsewhere that my issue probably wasn’t with transparency. I had accidentally been drawing the buildings in the terrain layer, instead of the terrain, and then drawing buildings on top of them. Perhaps I should have set a different background colour other than black to help catch the issue.
I also had another minor bug where I was adding to my rendering batch, but had the actually rendering being done inside the loop. I wondered why it took several seconds to render the scene before I realized, but it was an easy fix.
I also worked on refactoring the terrain rendering code. I want to be able to render chunks and also not have building stored directly on the hex tile, rather have a building both be associated with a tile and a dictionary of building attached to the whole terrain map. I also switched to “rectangular” chunks because they’re about the same aspect ratio as the viewport is.
I haven’t gotten more than one chunk added and drawn yet because I got sidetracked adding scrolling so I could see my different chunks. Scrolling itself works, but thouse mouse is offset from where it should be.
Continued in Part 2.