Skip to content

Instantly share code, notes, and snippets.

@R3tr0BoiDX
Created October 15, 2023 22:07
Show Gist options
  • Save R3tr0BoiDX/ffba502f15e84ac93512d81e5e06b4da to your computer and use it in GitHub Desktop.
Save R3tr0BoiDX/ffba502f15e84ac93512d81e5e06b4da to your computer and use it in GitHub Desktop.
Pygame Controller Layout
import pygame
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
while True:
for event in pygame.event.get():
if event.type == pygame.JOYHATMOTION:
hat = joystick.get_hat(0)
if hat == (0, 1):
print("D-pad up")
elif hat == (0, -1):
print("D-pad down")
elif hat == (-1, 0):
print("D-pad left")
elif hat == (1, 0):
print("D-pad right")
elif event.type == pygame.JOYAXISMOTION:
axis = event.axis
value = joystick.get_axis(axis)
if axis == 0:
if value < -0.5:
print("Left stick left")
elif value > 0.5:
print("Left stick right")
elif axis == 1:
if value < -0.5:
print("Left stick up")
elif value > 0.5:
print("Left stick down")
elif axis == 2:
if value < -0.5:
print("Left trigger")
elif axis == 3:
if value < -0.5:
print("Right trigger")
elif axis == 4:
if value < -0.5:
print("Right stick left")
elif value > 0.5:
print("Right stick right")
elif axis == 5:
if value < -0.5:
print("Right stick up")
elif value > 0.5:
print("Right stick down")
elif event.type == pygame.JOYBUTTONDOWN:
button = event.button
if button == 0:
print("A button")
elif button == 1:
print("B button")
elif button == 2:
print("X button")
elif button == 3:
print("Y button")
elif button == 4:
print("Left bumper")
elif button == 5:
print("Right bumper")
elif button == 6:
print("Select button")
elif button == 7:
print("Start button")
elif button == 8:
print("Guide button")
elif button == 9:
print("Left stick button")
elif button == 10:
print("Right stick button")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment