diff --git a/actors/enums.py b/actors/enums.py index 4b86c41..b04cf9d 100644 --- a/actors/enums.py +++ b/actors/enums.py @@ -1,5 +1,9 @@ from enum import Enum, auto +class PillType(Enum): + NORMAL = 1 + POWER = 2 + class PlayerDirection(Enum): DirectionRight = 0 DirectionLeft = 180 diff --git a/actors/ghost.py b/actors/ghost.py index 3c45261..6949464 100644 --- a/actors/ghost.py +++ b/actors/ghost.py @@ -1,5 +1,5 @@ -from enums import GhostColor, GhostMode, GhostBehavior -from behaviors import path_toward # required if you want a fallback +from .enums import GhostColor, GhostMode, GhostBehavior +from .behaviors import path_toward # required if you want a fallback class Ghost(pygame.sprite.Sprite): def __init__(self, name, color_enum, behavior_enum, position, speed): diff --git a/actors/ghost_mode_controller.py b/actors/ghost_mode_controller.py index 634d232..4732b7c 100644 --- a/actors/ghost_mode_controller.py +++ b/actors/ghost_mode_controller.py @@ -1,5 +1,5 @@ import time -from enums import GhostMode +from .enums import GhostMode class GhostModeController: def __init__(self): diff --git a/actors/pill.py b/actors/pill.py index a26edcc..2329ba0 100644 --- a/actors/pill.py +++ b/actors/pill.py @@ -1,5 +1,44 @@ -from .enums import PillType +import pygame +from enums import PillType -class Pill: - def __init__(self, typ: PillType): - self.pilltype = typ \ No newline at end of file +class Pill(pygame.sprite.Sprite): + def __init__(self, x, y, pill_type: PillType): + super().__init__() + self.x = x + self.y = y + self.pill_type = pill_type + self.eaten = False + self.radius = 2 if pill_type == PillType.NORMAL else 6 + self.color = (255, 255, 255) + self.image = pygame.Surface((self.radius * 2, self.radius * 2), pygame.SRCALPHA) + pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius) + self.rect = self.image.get_rect(center=(x, y)) + + def check_eaten(self, pacman_rect, game_state): + if not self.eaten and self.rect.colliderect(pacman_rect): + self.eaten = True + self.kill() + return self.on_eaten(game_state) + return 0 + + def on_eaten(self, game_state): + """Override this in subclasses to apply effects when eaten""" + return 0 + +class NormalPill(Pill): + def __init__(self, x, y): + super().__init__(x, y, PillType.NORMAL) + + def on_eaten(self, game_state): + game_state["score"] += 100 + return 100 + +class PowerPill(Pill): + def __init__(self, x, y): + super().__init__(x, y, PillType.POWER) + + def on_eaten(self, game_state): + game_state["score"] += 500 + game_state["ghost_mode"] = "frightened" + game_state["frightened_timer"] = pygame.time.get_ticks() # Optional + return 500 \ No newline at end of file diff --git a/pman.py b/pman.py index c8e2cfb..72c90fc 100644 --- a/pman.py +++ b/pman.py @@ -3,8 +3,9 @@ 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 +from scoreboard import Scoreboard -__version__ = "0.2.0" +__version__ = "0.2.1" def spawn_ghosts(center_position): @@ -26,12 +27,37 @@ def spawn_ghosts(center_position): 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__) + scoreboard = Scoreboard() labyrinth = Labyrinth(screen, width=screen_width, height=screen_height) player = ActorPacman(screen, center=(200, 200)) ghost_home_center = (maze_width // 2, maze_height // 2) @@ -78,6 +104,7 @@ def main() -> None: ghost.update(maze, pacman) ghosts.draw(screen) + scoreboard.draw(screen) pygame.display.flip() clock.tick(60) diff --git a/scoreboard.py b/scoreboard.py new file mode 100644 index 0000000..560ed6a --- /dev/null +++ b/scoreboard.py @@ -0,0 +1,18 @@ +import pygame + +class Scoreboard: + def __init__(self, font_size=24): + self.score = 0 + self.font = pygame.font.Font(None, font_size) + self.color = (255, 255, 255) + self.position = (10, 10) + + def add_points(self, points): + self.score += points + + def reset(self): + self.score = 0 + + def draw(self, screen): + score_text = self.font.render(f"Score: {self.score}", True, self.color) + screen.blit(score_text, self.position) \ No newline at end of file