1

So my goal is to be able to draw hexagons in a radial way, meaning first is center then 6 more on each side starting from top moving clockwise. Then drawing next layer and so own. Each Hexagon has a coordinate, a colour and other parameters. Then I have a HexGrid class that takes in a list of Hexagons or a number and automatically does all the math stuff. All classes are in folder Classes. Then I have a main.py for the rest of the program to draw the Hex grid in a shape of a bigger Hexagon (Like Catan game board). So what am I doing wrong in the code below:

This is my Hexagon class:

import pygame
import math

class Hexagon:
    def __init__(self, x, y, angle, color, size):
        self.x = x
        self.y = y
        self.angle = angle
        self.color = color
        self.size = size

    def draw(self, surface):
        points = []
        for i in range(6):
            angle = math.radians(self.angle + i * 60)
            x = self.x + self.size * math.cos(angle)
            y = self.y + self.size * math.sin(angle)
            points.append((x, y))
        pygame.draw.polygon(surface, self.color, points)

This is my HexGrid class:

import pygame
import math
from Classes.Hexagon import Hexagon

class HexGrid:
    def __init__(self, hexagons):
        self.hexagons = hexagons

    def arrange_hexagons(self):
        num_hexagons = len(self.hexagons)
        if num_hexagons == 0:
            return

        # Find the central hexagon
        center_hexagon = self.hexagons[0]
        center_x = center_hexagon.x
        center_y = center_hexagon.y

        # Calculate the offset for arranging hexagons
        x_offset = center_x
        y_offset = center_y

        # Arrange the hexagons based on axial coordinates
        for i, hexagon in enumerate(self.hexagons):
            q = hexagon.x - center_x
            r = hexagon.y - center_y
            hexagon.x = x_offset + (3 / 2 * hexagon.size) * q
            hexagon.y = y_offset + (math.sqrt(3) / 2 * hexagon.size) * (q + 2 * r)

    def draw(self, surface):
        for hexagon in self.hexagons:
            hexagon.draw(surface)

And finally my main.py:

import pygame
import math
from Classes.Hexagon import Hexagon
from Classes.HexGrid import HexGrid

# Set up Pygame
pygame.init()

# Window dimensions
width = 800
height = 600

# Create the window
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Hexagonal Grid")

# Fill the background
background_color = (255, 255, 255)
screen.fill(background_color)
h1 = Hexagon(width // 2 - 100, height // 2, 60, (0, 0, 255), 50)
# Create hexagon instances
hexagons = [
    Hexagon(width // 2, height // 2, 0, (255, 0, 0), 50),
    Hexagon(width // 2 + 100, height // 2 + 100, 30, (0, 255, 0), 50),
    Hexagon(width // 2 - 100, height // 2, 60, (0, 0, 255), 50),
]

# Create hexagonal grid
hex_grid = HexGrid(hexagons)

# Arrange the hexagons in the grid
hex_grid.arrange_hexagons()

# Main loop
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Clear the screen
    screen.fill(background_color)

    # Draw the hexagonal grid
    [i.draw(screen) for i in hex_grid.hexagons]
    # Update the display
    pygame.display.flip()

    clock.tick(60)

# Quit Pygame
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • It's just a problem with the math. Two of your hexagons are out of bounds. `print(hexagon.x, hexagon.y)` to debug your problem (in the `for`-loop in `arrange_hexagons`) – Rabbid76 May 16 '23 at 17:04
  • What's the solution? – Red Mermaid May 16 '23 at 17:05
  • Fix the math. If you followed the advice to print the intermediate values, you'd see it. For the second hexagon, `q` and `r` are 100. So, you're computing `100 + (3 / 2 * 50) * 100` That is `100 + 7500` or `7600`, which is way out of range. – Tim Roberts May 16 '23 at 17:14

0 Answers0