-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller_input.py
61 lines (47 loc) · 1.57 KB
/
controller_input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pygame
import time
import os
# Initialize Pygame and Joystick
pygame.init()
pygame.joystick.init()
# Check if a joystick is connected
if pygame.joystick.get_count() == 0:
print("No joystick detected.")
exit()
# Initialize the first joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
print(f"Controller name: {joystick.get_name()}")
print(f"Number of axes: {joystick.get_numaxes()}")
print(f"Number of buttons: {joystick.get_numbuttons()}")
print(f"Number of hats: {joystick.get_numhats()}")
def clear_screen():
"""Check the operating system and clear the termanal."""
os.system('cls' if os.name == 'nt' else 'clear')
# Main loop to capture all controller values
def get_joystick_inputs():
"""
Returns:
string: All of the values of the controller.
Display all of the values of an x-box controller.
"""
output = ""
pygame.event.pump() # Updates pygame events
# Get and print all axis values (analog sticks, triggers)
for i in range(joystick.get_numaxes()):
axis_value = joystick.get_axis(i)
output = f"{output}\n{axis_value}"
# Get and print all button values (A, B, X, Y, etc.)
for i in range(joystick.get_numbuttons()):
button_value = joystick.get_button(i)
output = f"{output}\n{button_value}"
# Get and print all hat (D-pad) values
for i in range(joystick.get_numhats()):
hat_value = joystick.get_hat(i)
output = f"{output}\n{hat_value}"
return output
while True:
print(get_joystick_inputs())
clear_screen()
# Quit Pygame
pygame.quit()