Files
PyGame-Pacman/pman.py
2025-04-19 20:01:44 +02:00

120 lines
4.5 KiB
Python

import pygame
import pygameController as PC
from actors.enums import Colors, PlayerDirection
from actors.pacman import ActorPacman
from actors.ghost import Blinky, Pinky, Inky, Clyde # adjust import path as needed
from actors.ghost_mode_controller import GhostModeController
from hud import HUD
from maze import Maze
__version__ = "0.3.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 main() -> None:
pygame.init()
joystick_count = pygame.joystick.get_count()
joysticks = {}
screen_width, screen_height = (32*29), (32*30)+32
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pac-Man " + __version__)
cherry_img = pygame.image.load("assets/cherry.png").convert_alpha()
hud = HUD(screen_width, screen_height, cherry_image=cherry_img)
maze = Maze("maze/pacman_maze.txt")
player = ActorPacman(screen, center=maze.pacman_start, start_location=maze.pacman_location, maze=maze)
ghost_mode_controller = GhostModeController()
ghost_home_center = (screen_width // 2, (screen_height) // 2)
ghosts = spawn_ghosts(ghost_home_center)
clock = pygame.time.Clock()
running = True
while running:
screen.fill(Colors.Black.value)
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
player.move_right()
case pygame.K_LEFT:
player.direction = PlayerDirection.DirectionLeft
player.move_left()
case pygame.K_UP:
player.direction = PlayerDirection.DirectionUp
player.move_up()
case pygame.K_DOWN:
player.direction = PlayerDirection.DirectionDown
player.move_down()
case pygame.JOYHATMOTION:
hat_x, hat_y = event.value
if hat_x == 1:
player.direction = PlayerDirection.DirectionRight
player.move_right()
elif hat_x == -1:
player.direction = PlayerDirection.DirectionLeft
player.move_left()
elif hat_y == 1:
player.direction = PlayerDirection.DirectionUp
player.move_up()
elif hat_y == -1:
player.direction = PlayerDirection.DirectionDown
player.move_down()
# Handle hotplugging
case pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
joy = pygame.joystick.Joystick(event.device_index)
joysticks[joy.get_instance_id()] = PC.controller.Controllers(joy)
case pygame.JOYDEVICEREMOVED:
del joysticks[event.instance_id]
print(f"Joystick {event.instance_id} disconnected")
# In your main game loop:
hud.draw(screen)
maze.draw(screen)
player.update()
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()