pythonpygamexbox

How do I get xbox controls outside event loop in pygame?


I use python3 and pygame and I have player class, in which I have my controls of the player defined as in the function:

    def get_input(self):
        #xbox example code, which doesnt work:
        buttons=pygame.joystick.Joystick.get_button()
        if buttons(button=0):
            self.jump()
        #more code for xbox controller. 
        #keyboard, this works perfectly:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
            self.direction.x = 1
        elif keys[pygame.K_LEFT]:
            self.direction.x = -1
        elif keys[pygame.K_d]:
            self.direction.x = 1
        elif keys[pygame.K_a]:
            self.direction.x = -1
        else:
            self.direction.x = 0
        if (keys[pygame.K_LEFT] and keys[pygame.K_RIGHT]):
                self.direction.x = 0
        if (keys[pygame.K_SPACE] and self.on_ground):
            self.jump()

Question how do I add XBOX controls as in example above for the keys? (I wanna use axis for left-right movement and one button for jumping) This code for reason I don't understand gives me following error:

File [...], line 37, in get_input
    buttons=pygame.joystick.Joystick.get_button()
AttributeError: 'builtin_function_or_method' object has no attribute 'get_button'

What's going on here?


Solution

  • You have to create an instance object of a pygame.joystick.Joystick.

    Minimal example:

    import pygame
    
    pygame.init()
    window = pygame.display.set_mode((300, 300))
    clock = pygame.time.Clock()
    
    rect = pygame.Rect(0, 0, 20, 20)
    rect.center = window.get_rect().center
    vel = 5
    color = (255, 0, 0)
    
    joystick = None
    if pygame.joystick.get_count() > 0:
        joystick = pygame.joystick.Joystick(0)
        joystick.init()
        print("joystick initialized")
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                print(pygame.key.name(event.key))
    
        if joystick:
            button = joystick.get_button(0)
            rect.x += round(joystick.get_axis(0) * vel)
            rect.y += round(joystick.get_axis(1) * vel)
            if joystick.get_button(0):
                color = (0, 255, 0)
            elif joystick.get_button(1):
                color = (255, 0, 0)
            elif joystick.get_button(2):
                color = (0, 0, 255)
            elif joystick.get_button(3):
                color = (255, 255, 0)
    
        keys = pygame.key.get_pressed()
        rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
        rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
            
        rect.centerx = rect.centerx % window.get_width()
        rect.centery = rect.centery % window.get_height()
    
        window.fill(0)
        pygame.draw.rect(window, color, rect)
        pygame.display.flip()
    
    pygame.quit()
    exit()