112 lines
3.8 KiB
Python
112 lines
3.8 KiB
Python
import pygame
|
|
from actors.enums import Colors, PlayerDirection
|
|
from actors.pacman import ActorPacman
|
|
from labyrinth import Labyrinth
|
|
from actors.ghosts import Blinky, Pinky, Inky, Clyde # adjust import path as needed
|
|
|
|
__version__ = "0.2.1"
|
|
|
|
|
|
def spawn_ghosts(center_position):
|
|
"""
|
|
Spawns up to 4 ghosts at the center of the maze ("ghost house").
|
|
|
|
Args:
|
|
center_position (tuple): (x, y) coordinates for spawn point.
|
|
|
|
Returns:
|
|
pygame.sprite.Group: A group containing all ghost instances.
|
|
"""
|
|
offset = 20 # spread out a little bit inside ghost home
|
|
|
|
blinky = Blinky((center_position[0] - offset, center_position[1] - offset))
|
|
pinky = Pinky((center_position[0] + offset, center_position[1] - offset))
|
|
inky = Inky((center_position[0] - offset, center_position[1] + offset))
|
|
clyde = Clyde((center_position[0] + offset, center_position[1] + offset))
|
|
|
|
return pygame.sprite.Group(blinky, pinky, inky, clyde)
|
|
|
|
def place_pills(maze, spacing=16):
|
|
pills = pygame.sprite.Group()
|
|
height = len(maze)
|
|
width = len(maze[0])
|
|
|
|
# Normal pills
|
|
for y in range(height):
|
|
for x in range(width):
|
|
if maze[y][x] == 0: # assuming 0 is path
|
|
if x % spacing == 0 and y % spacing == 0:
|
|
pills.add(NormalPill(x * spacing + spacing // 2, y * spacing + spacing // 2))
|
|
|
|
# Power pills in 4 corners
|
|
corners = [
|
|
(1, 1),
|
|
(1, width - 2),
|
|
(height - 2, 1),
|
|
(height - 2, width - 2)
|
|
]
|
|
for cy, cx in corners:
|
|
pills.add(PowerPill(cx * spacing + spacing // 2, cy * spacing + spacing // 2))
|
|
|
|
return pills
|
|
|
|
def main() -> None:
|
|
pygame.init()
|
|
screen_width, screen_height = 800, 800
|
|
screen = pygame.display.set_mode((screen_width, screen_height))
|
|
pygame.display.set_caption("Pac-Man " + __version__)
|
|
|
|
labyrinth = Labyrinth(screen, width=screen_width, height=screen_height)
|
|
player = ActorPacman(screen, center=(200, 200))
|
|
ghost_home_center = (maze_width // 2, maze_height // 2)
|
|
ghosts = spawn_ghosts(ghost_home_center)
|
|
clock = pygame.time.Clock()
|
|
|
|
running = True
|
|
while running:
|
|
screen.fill(Colors.Black)
|
|
|
|
for event in pygame.event.get():
|
|
match event.type:
|
|
case pygame.QUIT:
|
|
running = False
|
|
case pygame.KEYDOWN:
|
|
match event.key:
|
|
case pygame.K_RIGHT:
|
|
player.direction = PlayerDirection.DirectionRight
|
|
case pygame.K_LEFT:
|
|
player.direction = PlayerDirection.DirectionLeft
|
|
case pygame.K_UP:
|
|
player.direction = PlayerDirection.DirectionUp
|
|
case pygame.K_DOWN:
|
|
player.direction = PlayerDirection.DirectionDown
|
|
case pygame.JOYHATMOTION:
|
|
hat_x, hat_y =event.value
|
|
if hat_x == 1:
|
|
player.direction = PlayerDirection.DirectionRight
|
|
elif hat_x == -1:
|
|
player.direction = PlayerDirection.DirectionLeft
|
|
elif hat_y == 1:
|
|
player.direction = PlayerDirection.DirectionUp
|
|
elif hat_y == -1:
|
|
player.direction = PlayerDirection.DirectionDown
|
|
|
|
labyrinth.draw()
|
|
player.animate()
|
|
player.draw()
|
|
ghost_mode_controller.update()
|
|
current_mode = ghost_mode_controller.mode
|
|
|
|
for ghost in ghosts:
|
|
ghost.set_mode(current_mode)
|
|
ghost.update(maze, pacman)
|
|
|
|
ghosts.draw(screen)
|
|
pygame.display.flip()
|
|
clock.tick(60)
|
|
|
|
pygame.quit()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|