Created
June 17, 2024 13:06
-
-
Save twobob/89abcda154d81d8cdcfd992b62455190 to your computer and use it in GitHub Desktop.
pygame xbox360 controller minimum test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import sys | |
# Initialize pygame | |
pygame.init() | |
# Set up the display | |
width, height = 800, 600 | |
screen = pygame.display.set_mode((width, height)) | |
pygame.display.set_caption("Xbox 360 Controller Test") | |
# Set up colors | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
# Function to draw crosshair | |
def draw_crosshair(screen, x, y): | |
length = 20 | |
width = 3 | |
pygame.draw.line(screen, WHITE, (x - length, y), (x + length, y), width) | |
pygame.draw.line(screen, WHITE, (x, y - length), (x, y + length), width) | |
# Initialize the joystick | |
pygame.joystick.init() | |
if pygame.joystick.get_count() == 0: | |
print("No joystick connected.") | |
pygame.quit() | |
sys.exit() | |
joystick = pygame.joystick.Joystick(0) | |
joystick.init() | |
# Initial position of the crosshair | |
crosshair_x, crosshair_y = width // 2, height // 2 | |
# Movement speed | |
move_speed = 10 | |
# Main loop | |
running = True | |
while running: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
elif event.type == pygame.JOYBUTTONDOWN: | |
if joystick.get_button(0): # A button | |
print("A button pressed") | |
if joystick.get_button(1): # B button | |
print("B button pressed") | |
if joystick.get_button(2): # X button | |
print("X button pressed") | |
if joystick.get_button(3): # Y button | |
print("Y button pressed") | |
if joystick.get_button(4): # LB button | |
print("LB button pressed") | |
if joystick.get_button(5): # RB button | |
print("RB button pressed") | |
if joystick.get_button(6): # Back button | |
print("Back button pressed") | |
if joystick.get_button(7): # Start button | |
print("Start button pressed") | |
if joystick.get_button(8): # Left stick button | |
print("Left stick button pressed") | |
if joystick.get_button(9): # Right stick button | |
print("Right stick button pressed") | |
# Get the left stick axes | |
left_x = joystick.get_axis(0) | |
left_y = joystick.get_axis(1) | |
# Get the axis values for the right thumbstick | |
right_thumbstick_x = joystick.get_axis(2) | |
right_thumbstick_y = joystick.get_axis(3) | |
# Update crosshair position with left stick | |
crosshair_x += int(right_thumbstick_x * move_speed) | |
crosshair_y += int(right_thumbstick_y * move_speed) | |
# Update crosshair position with left stick | |
crosshair_x += int(left_x * move_speed) | |
crosshair_y += int(left_y * move_speed) | |
# Get the D-pad directions | |
dpad_x, dpad_y = joystick.get_hat(0) | |
# Update crosshair position with D-pad | |
crosshair_x += dpad_x * move_speed | |
crosshair_y += dpad_y * move_speed | |
# Get the analog triggers | |
trigger_left = joystick.get_axis(4) # Correct axis for left trigger | |
trigger_right = joystick.get_axis(5) # Correct axis for right trigger | |
# Print trigger values | |
if trigger_left > 0: | |
print(f"Left trigger pressed: {trigger_left}") | |
if trigger_right > 0: | |
print(f"Right trigger pressed: {trigger_right}") | |
# Keep the crosshair within screen bounds | |
crosshair_x = max(0, min(crosshair_x, width)) | |
crosshair_y = max(0, min(crosshair_y, height)) | |
# Clear the screen | |
screen.fill(BLACK) | |
# Draw the crosshair | |
draw_crosshair(screen, crosshair_x, crosshair_y) | |
# Update the display | |
pygame.display.flip() | |
# Cap the frame rate | |
pygame.time.Clock().tick(60) | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment