I've been using pygame as a means of creating a game, and I would like to incorporate controller gameplay into the game. I have been using a new Xbox One controller on pygame and have been able to detect the controller in pygame and such, however none of the pygame.get_event functions ever work and always return their default values even when I move the controller's joysticks around and press the buttons as well.
I have tried pygame.event.pump() to no avail. Here is my code:
xboxController = pygame.joystick.Joystick(0)
xboxController.init()
print(xboxController.get_name())
print("Axes", (xboxController.get_numaxes()))
print("Balls", xboxController.get_numballs())
print("Buttons", xboxController.get_numbuttons())
print("Hats", xboxController.get_numhats())
if xboxController.get_init() == True: print("Initialized properly")
while not found:
trial += 1
print(trial)
pygame.event.pump()
for a in range(0, xboxController.get_numaxes()):
if round(xboxController.get_axis(a)*100) != 0:
print(xboxController.get_axis(a))
for b in range(0, xboxController.get_numballs()):
if xboxController.get_ball(b) != 0:
print(xboxController.get_ball(b))
for c in range(0, xboxController.get_numbuttons()):
if xboxController.get_button(c) != False:
print(xboxController.get_axis(button(c)))
for d in range(0, xboxController.get_numhats()):
if xboxController.get_hat(d) != (0, 0):
print(xboxController.get_hat(d))
The first part where I print out all of the properties of the controller works fine, it just never seems to know when the controller's joysticks are being moved.
I am also making a game with an Xbox controller, and experienced a very similar problem. My problem was fixed simply by using
pygame.event.get()
instead ofpygame.event.pump()
. You don't even need to do anything with the results, just getting the events seems to make pygame update the joystick positions.