I have the following code which used to work but now does nothing, i have no idea what is causing the problem. I have researched and found the following post and they worked one time but no longer work. How do i use Xbox 360 controller with pygame?
import pygame
from pygame.locals import *
pygame.init()
joystick = pygame.joystick.Joystick(0)
while True:
pygame.event.get(): # get the events (update the joystick)
a_pressed = joystick.get_button(0)
b_pressed = joystick.get_button(1)
x_pressed = joystick.get_button(2)
y_pressed = joystick.get_button(3)
lb_pressed = joystick.get_button(4)
rb_pressed = joystick.get_button(5)
back_button_pressed = joystick.get_button(6)
start_button_pressed = joystick.get_button(7)
guide_button_pressed = joystick.get_button(8)
ls_pressed = joystick.get_button(9)
rs_pressed = joystick.get_button(10)
lsx = joystick.get_axis(0)
lsy = joystick.get_axis(1)
rsx = joystick.get_axis(3)
rsy = joystick.get_axis(4)
lt = joystick.get_axis(2)
rt = joystick.get_axis(5)
dpad = joystick.get_hat(0)
if a_pressed:
print("a")
if b_pressed:
print("b")
if x_pressed:
print("x")
if y_pressed:
print("y")
if lb_pressed:
print("lb")
if rb_pressed:
print("rb")
if back_button_pressed:
print("back")
if start_button_pressed:
print("start")
if ls_pressed:
print("ls")
if rs_pressed:
print("rs")
if guide_button_pressed:
print("guide")
if lsx >= 0.2:
print("lsright")
if lsx <= -0.2:
print("lsleft")
if rsx >= 0.2:
print("rsright")
if rsx <= -0.2:
print("rsleft")
if lsy >= 0.2:
print("lsdown")
if lsy <= -0.2:
print("lsup")
if rsy >= 0.2:
print("rsdown")
if rsy <= -0.2:
print("rsup")
if dpad[0] == 1:
print("dpadright")
if dpad[0] == -1:
print("dpadleft")
if dpad[1] == 1:
print("dpadup")
if dpad[1] == -1:
print("dpaddown")
This code used to produce an output when any of the specified buttons were pressed. It no longer works.