Python Forum
Hex coords and using the tile - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Hex coords and using the tile (/thread-32849.html)



Hex coords and using the tile - jauger - Mar-10-2021

I am trying to learn python through making a game. In my game, I used a script that built the set up for Settlers of Catan. My board is slightly larger and I can move a game piece around the board to different hexes.

Now, in each hex I am looking to have different options based where the game piece is and the hex type (color for now but later art). At the moment, I just want to print the hex type (called environment, like lava and basalt) is when I press a key.

This is how I am drawing each hex and board.


class Board:

hexes = []

def __init__(self, size):

self.cX = screen.get_rect().centerx

self.cY = screen.get_rect().centery



self.hexHeight = 0.866 * size

~ ~ ~

# Second Ring

self.tiles = [LAVA, BASALT]*3

random.shuffle(self.tiles)


# Second Ring of hexes

self.hexes.append(Hex((self.cX, self.cY - (2*self.hexHeight)), size, self.tiles[0]))

self.hexes.append(Hex((self.cX - (1.5*size), self.cY - self.hexHeight), size, self.tiles[1]))

self.hexes.append(Hex((self.cX - (1.5*size), self.cY + self.hexHeight), size, self.tiles[2]))

self.hexes.append(Hex((self.cX, self.cY + (2*self.hexHeight)), size, self.tiles[3]))

self.hexes.append(Hex((self.cX + (1.5*size), self.cY + self.hexHeight), size, self.tiles[4]))

self.hexes.append(Hex((self.cX + (1.5*size), self.cY - self.hexHeight), size, self.tiles[5]))

~ ~ ~

Then, later when I press number key 5...and this is where I am stuck. I want to say something like...


elif event.key == K_KP5:
if HEX == LAVA:
print("LAVA")


I'd be happy to share the entire script in a pm for those interested.


RE: Hex coords and using the tile - michael1789 - Mar-10-2021

Are you using something like pygame for your input/graphics?


RE: Hex coords and using the tile - jauger - Mar-10-2021

(Mar-10-2021, 03:42 PM)michael1789 Wrote: Are you using something like pygame for your input/graphics?

Yes! I should have said that...


RE: Hex coords and using the tile - michael1789 - Mar-10-2021

Instead of a list, I'd use a dictionary for the game board.
game_board = {}

#loop over how you like adding hexes.
game_board.update({x,y : Hex(x,y)})
Then you can refer to them my location game_board[x,y].

As far as keyboard stuff, where are you at it?