#7 Added ghost home and pivoting walls. /JL
This commit is contained in:
@@ -19,4 +19,6 @@ class PillType(Enum):
|
|||||||
class Colors(Enum):
|
class Colors(Enum):
|
||||||
Yellow = (255, 255, 0)
|
Yellow = (255, 255, 0)
|
||||||
Black = (0, 0, 0)
|
Black = (0, 0, 0)
|
||||||
Blue = (0, 0, 255)
|
Blue = (0, 0, 255)
|
||||||
|
Cyan = (0, 255, 255)
|
||||||
|
Magenta = (255, 0, 255)
|
70
labyrinth.py
70
labyrinth.py
@@ -1,5 +1,56 @@
|
|||||||
from actors.enums import Colors
|
from actors.enums import Colors
|
||||||
import pygame
|
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, 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, (r.left, r.top + t, t, r.height -t))
|
||||||
|
pygame.draw.rect(self.screen, Colors.Magenta, (r.right - t, r.top + t, t, r.height -t))
|
||||||
|
pygame.draw.rect(self.screen, Colors.Magenta, (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, (r.left, r.top, gap_x1 - r.left, t))
|
||||||
|
pygame.draw.rect(self.screen, Colors.Magenta, (gap_x2, r.top, r.right - gap_x2, t))
|
||||||
|
|
||||||
class Labyrinth:
|
class Labyrinth:
|
||||||
def __init__(self, screen, width, height, wall_thickness=20):
|
def __init__(self, screen, width, height, wall_thickness=20):
|
||||||
@@ -7,6 +58,14 @@ class Labyrinth:
|
|||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.wall_thickness = wall_thickness
|
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):
|
def draw(self):
|
||||||
w = self.wall_thickness
|
w = self.wall_thickness
|
||||||
@@ -21,4 +80,13 @@ class Labyrinth:
|
|||||||
pygame.draw.rect(self.screen, Colors.Blue, (0, 0, w, mid_y -50))
|
pygame.draw.rect(self.screen, Colors.Blue, (0, 0, w, mid_y -50))
|
||||||
pygame.draw.rect(self.screen, Colors.Blue, (0, mid_y + 50, w, self.height - (mid_y + 50)))
|
pygame.draw.rect(self.screen, Colors.Blue, (0, mid_y + 50, w, self.height - (mid_y + 50)))
|
||||||
pygame.draw.rect(self.screen, Colors.Blue, (self.width - w, 0, w, mid_y - 50))
|
pygame.draw.rect(self.screen, Colors.Blue, (self.width - w, 0, w, mid_y - 50))
|
||||||
pygame.draw.rect(self.screen, Colors.Blue, (self.width - w, mid_y + 50, w, self.height - (mid_y + 50)))
|
pygame.draw.rect(self.screen, Colors.Blue, (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)
|
5
pman.py
5
pman.py
@@ -3,7 +3,7 @@ from actors.enums import Colors, PlayerDirection
|
|||||||
from actors.pacman import ActorPacman
|
from actors.pacman import ActorPacman
|
||||||
from labyrinth import Labyrinth
|
from labyrinth import Labyrinth
|
||||||
|
|
||||||
__version__ = "0.1.1"
|
__version__ = "0.1.2"
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
pygame.init()
|
pygame.init()
|
||||||
@@ -43,7 +43,8 @@ def main() -> None:
|
|||||||
player.direction = PlayerDirection.DirectionUp
|
player.direction = PlayerDirection.DirectionUp
|
||||||
elif hat_y == -1:
|
elif hat_y == -1:
|
||||||
player.direction = PlayerDirection.DirectionDown
|
player.direction = PlayerDirection.DirectionDown
|
||||||
|
|
||||||
|
labyrinth.draw()
|
||||||
player.animate()
|
player.animate()
|
||||||
player.draw()
|
player.draw()
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
Reference in New Issue
Block a user