Files
PyGame-Pacman/labyrinth.py
2025-04-17 21:40:49 +02:00

92 lines
3.8 KiB
Python

from actors.enums import Colors
import pygame
import math
class PivotWall:
def __init__(self, screen, center, length=40, thickness=8, angle=0):
self.screen = screen
self.center = center
self.length = length
self.thickness = thickness
self.angle = angle
def draw(self):
x, y = self.center
angle_rad = math.radians(self.angle)
dx = math.cos(angle_rad) * self.length / 2
dy = math.sin(angle_rad) * self.length / 2
points = [
(x - dx - dy * self.thickness / self.length, y - dy + dx * self.thickness / self.length),
(x - dx + dy * self.thickness / self.length, y - dy - dx * self.thickness / self.length),
(x + dx + dy * self.thickness / self.length, y + dy - dx * self.thickness / self.length),
(x + dx - dy * self.thickness / self.length, y + dy + dx * self.thickness / self.length),
]
pygame.draw.polygon(self.screen, Colors.Cyan.value, points)
def rotate(self, delta_angle):
self.angle = (self.angle + delta_angle) % 360
class GhostHome:
def __init__(self, screen, center, width=100, height=60, wall_thickness=8):
self.screen = screen
self.center = center
self.width = width
self.height = height
self.wall_thickness = wall_thickness
cx, cy = self.center
self.rect = pygame.Rect(cx - width // 2, cy - height // 2, width, height)
def draw(self):
r = self.rect
t = self.wall_thickness
pygame.draw.rect(self.screen, Colors.Magenta.value, (r.left, r.top + t, t, r.height -t))
pygame.draw.rect(self.screen, Colors.Magenta.value, (r.right - t, r.top + t, t, r.height -t))
pygame.draw.rect(self.screen, Colors.Magenta.value, (r.left, r.bottom - t, r.width, t))
gap = 40
gap_x1 = r.centerx - gap // 2
gap_x2 = r.centerx + gap // 2
pygame.draw.rect(self.screen, Colors.Magenta.value, (r.left, r.top, gap_x1 - r.left, t))
pygame.draw.rect(self.screen, Colors.Magenta.value, (gap_x2, r.top, r.right - gap_x2, t))
class Labyrinth:
def __init__(self, screen, width, height, wall_thickness=20):
self.screen = screen
self.width = width
self.height = height
self.wall_thickness = wall_thickness
offset = 50
self.pivot_walls = [
PivotWall(self.screen, (offset, offset)),
PivotWall(self.screen, (width - offset, offset)),
PivotWall(self.screen, (offset - height, offset)),
PivotWall(self.screen, (width - offset, height - offset))
]
self.ghost_home = GhostHome(self.screen, center=(width // 2, height // 2))
def draw(self):
w = self.wall_thickness
mid_x = self.width // 2
mid_y = self.height // 2
pygame.draw.rect(self.screen, Colors.Blue.value, (0, 0, mid_x - 50, w))
pygame.draw.rect(self.screen, Colors.Blue.value, (mid_x + 50, 0, self.width - (mid_x + 50), w))
pygame.draw.rect(self.screen, Colors.Blue.value, (0, self.height - w, mid_x - 50, w))
pygame.draw.rect(self.screen, Colors.Blue.value, (mid_x + 50, self.height - w, self.width - (mid_x + 50), w))
pygame.draw.rect(self.screen, Colors.Blue.value, (0, 0, w, mid_y -50))
pygame.draw.rect(self.screen, Colors.Blue.value, (0, mid_y + 50, w, self.height - (mid_y + 50)))
pygame.draw.rect(self.screen, Colors.Blue.value, (self.width - w, 0, w, mid_y - 50))
pygame.draw.rect(self.screen, Colors.Blue.value, (self.width - w, mid_y + 50, w, self.height - (mid_y + 50)))
for pivot in self.pivot_walls:
pivot.draw()
self.ghost_home.draw()
def rotate_all_pivots(self, angle):
for pivot in self.pivot_walls:
pivot.rotate(angle)