From 1f554e0ce03f29c5a57bc9bacb251f0af0bea693 Mon Sep 17 00:00:00 2001 From: Jan Lerking Date: Sun, 20 Apr 2025 13:44:52 +0200 Subject: [PATCH] #28 Working on motion repeat functionality. /JL --- .gitignore | 1 + pman.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 0dbf2f2..5df2b0f 100644 --- a/.gitignore +++ b/.gitignore @@ -168,3 +168,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +highscore.txt diff --git a/pman.py b/pman.py index cf80d7f..3c53f08 100644 --- a/pman.py +++ b/pman.py @@ -7,8 +7,16 @@ from actors.ghost_mode_controller import GhostModeController from hud import HUD from maze import Maze -__version__ = "0.3.3" +__version__ = "0.3.4" +# Constants +HAT_REPEAT_DELAY = 300 # milliseconds before first repeat +HAT_REPEAT_INTERVAL = 100 # milliseconds between repeats + +# State +hat_direction = (0, 0) +hat_timer = 0 +hat_first_press = True def spawn_ghosts(center_position): """ @@ -29,9 +37,31 @@ def spawn_ghosts(center_position): return pygame.sprite.Group(blinky, pinky, inky, clyde) -def main() -> None: - pygame.init() +def handle_hat_repeat(): + global hat_timer, hat_first_press + now = pygame.time.get_ticks() + if hat_direction != (0, 0): + if hat_first_press: + if now - hat_timer >= HAT_REPEAT_DELAY: + hat_timer = now + hat_first_press = False + post_hat_repeat_event() + else: + if now - hat_timer >= HAT_REPEAT_INTERVAL: + hat_timer = now + post_hat_repeat_event() + +def post_hat_repeat_event(): + pygame.event.post(pygame.event.Event(pygame.USEREVENT, { + "type_name": "JOYHATREPEAT", + "value": hat_direction + })) +def main() -> None: + global hat_direction, hat_timer, hat_first_press + + pygame.init() + pygame.key.set_repeat(200) joystick_count = pygame.joystick.get_count() joysticks = {} @@ -53,7 +83,7 @@ def main() -> None: running = True while running: screen.fill(Colors.Black.value) - + pygame.event.pump() for event in pygame.event.get(): match event.type: case pygame.QUIT: @@ -81,7 +111,12 @@ def main() -> None: player.direction = PlayerDirection.DirectionDown player.move_down() case pygame.JOYHATMOTION: - hat_x, hat_y = event.value + hat_x, hat_y = hat_direction = event.value + hat_timer = pygame.time.get_ticks() + hat_first_press = True + + if event.type == pygame.USEREVENT and event.dict.get("type_name") == "JOYHATREPEAT": + print(f"Repeated hat: {event.dict['value']}") if hat_x == 1: if maze.is_wall(player.location[0] + 1, player.location[1]): break @@ -112,8 +147,15 @@ def main() -> None: case pygame.JOYDEVICEREMOVED: del joysticks[event.instance_id] print(f"Joystick {event.instance_id} disconnected") + + match maze.collect_dot(player.location[0], player.location[1]): + case "dot": + hud.add_points(10) + case "power": + hud.add_points(50) # In your main game loop: + handle_hat_repeat() hud.draw(screen) maze.draw(screen) player.update() @@ -129,6 +171,9 @@ def main() -> None: pygame.display.flip() clock.tick(60) + if joysticks: + joysticks[joy.get_instance_id()].controllers[0].close() + pygame.quit() if __name__ == "__main__":