problems with pygame controller support

Do not use the JOYAXISMOTION event. The event does not occur continuously, it only occurs once when the axis changes.
Use pygame.joystick.Joystick.get_axis to get the current position of the axis and move the player depending on the axis:

def main():
    # [...]

    while run:
        # [...]
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        if joysticks:
            joystick = joysticks[0]
            axis_x, axis_y = (joystick.get_axis(0), joystick.get_axis(1))
            if abs(axis_x) > 0.1:
                player.x += round(7 * axis_x)
            if abs(axis_y) > 0.1:
                player.y += round(7 * axis_y)

        # [...]

Minimal example:

import pygame
from pygame.locals import *

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
x, y = window.get_rect().center

if pygame.joystick.get_count() > 0:
    joystick = pygame.joystick.Joystick(0)
    joystick.init()

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    axis_x, axis_y = (joystick.get_axis(0), joystick.get_axis(1))
    if abs(axis_x) > 0.1:
        x = (x + round(7 * axis_x)) % window.get_width()
    if abs(axis_y) > 0.1:
        y = (y + round(7 * axis_y)) % window.get_height()   

    window.fill(0)
    pygame.draw.circle(window, (255, 0, 0), (x, y), 10)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

Leave a Comment