Support both joysticks on controller.

This commit is contained in:
JustinOros
2025-07-14 13:01:55 -07:00
parent edaeb045a6
commit 528bc029b3
+38 -14
View File
@@ -18,6 +18,7 @@ PETAL_SIZE = (30, 30)
GAME_OVER_DELAY = 3000 # ms GAME_OVER_DELAY = 3000 # ms
WASABI_CHANCE = 0.1 WASABI_CHANCE = 0.1
# Helper Functions # Helper Functions
def quit_game(): def quit_game():
pygame.quit() pygame.quit()
@@ -185,11 +186,12 @@ class Game:
self.chomp_sound = pygame.mixer.Sound("audio_chomp.mp3") self.chomp_sound = pygame.mixer.Sound("audio_chomp.mp3")
self.meow_sound = pygame.mixer.Sound("audio_meow.mp3") self.meow_sound = pygame.mixer.Sound("audio_meow.mp3")
# Joystick # Joysticks: support multiple joysticks if connected
self.joystick = None self.joysticks = []
if pygame.joystick.get_count() > 0: for i in range(pygame.joystick.get_count()):
self.joystick = pygame.joystick.Joystick(0) joy = pygame.joystick.Joystick(i)
self.joystick.init() joy.init()
self.joysticks.append(joy)
# Game entities # Game entities
self.cat = Cat(self.cat_right_base, (WIDTH // 2, HEIGHT), 14) self.cat = Cat(self.cat_right_base, (WIDTH // 2, HEIGHT), 14)
@@ -292,6 +294,8 @@ class Game:
pygame.mixer.music.unpause() pygame.mixer.music.unpause()
def run(self): def run(self):
deadzone = 0.2
while True: while True:
dt = self.clock.tick(60) dt = self.clock.tick(60)
now = pygame.time.get_ticks() now = pygame.time.get_ticks()
@@ -313,14 +317,15 @@ class Game:
quit_game() quit_game()
elif self.game_over and not self.paused: elif self.game_over and not self.paused:
self.reset_game() self.reset_game()
if self.joystick and event.type == pygame.JOYBUTTONDOWN: if event.type == pygame.JOYBUTTONDOWN:
if event.button == 7 or event.button == 6: # Find which joystick pressed button
if event.button in [7, 6]: # Start buttons
if not self.game_over: if not self.game_over:
if self.paused: if self.paused:
self.unpause() self.unpause()
else: else:
self.pause(by_controller=True) self.pause(by_controller=True)
elif self.paused and event.button == 2: elif self.paused and event.button == 2: # X button to quit while paused
quit_game() quit_game()
elif self.game_over and not self.paused: elif self.game_over and not self.paused:
self.reset_game() self.reset_game()
@@ -367,7 +372,7 @@ class Game:
self.petals.remove(petal) self.petals.remove(petal)
if not self.game_over: if not self.game_over:
# Handle input # Handle input from keyboard first
if keys[pygame.K_a] or keys[pygame.K_LEFT]: if keys[pygame.K_a] or keys[pygame.K_LEFT]:
move = -1 move = -1
self.cat.facing = "left" self.cat.facing = "left"
@@ -375,11 +380,30 @@ class Game:
move = 1 move = 1
self.cat.facing = "right" self.cat.facing = "right"
if self.joystick: # Handle input from joysticks
axis = self.joystick.get_axis(0) for joy in self.joysticks:
if abs(axis) > 0.2: # Left stick horizontal axis (usually axis 0)
move = int(axis / abs(axis)) # normalize to -1 or 1 axis0 = joy.get_axis(0)
self.cat.facing = "left" if axis < 0 else "right" # Right stick horizontal axis (usually axis 2)
axis2 = joy.get_axis(2)
# Safely check for hats
if joy.get_numhats() > 0:
hat_x, _ = joy.get_hat(0)
else:
hat_x = 0
# Only override move if no keyboard input (move==0)
if move == 0:
if abs(axis0) > deadzone:
move = int(axis0 / abs(axis0))
self.cat.facing = "left" if axis0 < 0 else "right"
elif abs(axis2) > deadzone:
move = int(axis2 / abs(axis2))
self.cat.facing = "left" if axis2 < 0 else "right"
elif hat_x != 0:
move = hat_x
self.cat.facing = "left" if hat_x < 0 else "right"
self.cat.update_image() self.cat.update_image()
self.cat.move(move) self.cat.move(move)