为什么只有 1 个对象正在使用 pygame 绘制?

问题描述 投票:0回答:0

所以我的目标是能够以径向方式绘制六边形,这意味着首先是中心,然后从顶部开始顺时针移动,每边再画 6 个。然后绘制下一层等自己。每个六边形都有坐标、颜色和其他参数。然后我有一个 HexGrid 类,它接收六边形列表或数字并自动执行所有数学运算。所有类都在文件夹类中。然后我有一个 main.py 用于程序的其余部分以更大的六边形形状绘制六边形网格(如 Catan 游戏板)。那么我在下面的代码中做错了什么:

这是我的六边形课程:

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)

这是我的 HexGrid 类:

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)

最后是我的 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()
python pygame pycharm hexagonal-tiles
© www.soinside.com 2019 - 2024. All rights reserved.