Added gamepad movement. /JL

This commit is contained in:
2025-04-19 19:54:40 +02:00
parent 8480b6ced7
commit 08824ecc06
13 changed files with 898 additions and 1 deletions

17
pman.py
View File

@@ -1,4 +1,5 @@
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
@@ -30,6 +31,11 @@ def spawn_ghosts(center_position):
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__)
@@ -67,7 +73,6 @@ def main() -> None:
player.direction = PlayerDirection.DirectionDown
player.move_down()
case pygame.JOYHATMOTION:
print("Hat has been pressed.")
hat_x, hat_y = event.value
if hat_x == 1:
player.direction = PlayerDirection.DirectionRight
@@ -81,6 +86,16 @@ def main() -> None:
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)