Python Forum

Full Version: pygame-manu : using controller buttons to move around menu
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using pygame-menu and I want to use buttons on my controller to navigate it. I can't figure it out at all. There's no example available online. Also, I'm using a USB NES controller which does not have it's own mapping. I am not sure what the issue is. I've tried many things but nothing seems to help. The menu can be navigated using the keyboard arrow buttons, but not the controller. The documentation is a little confusing on this.
here is code where I tried to make the controller work. It looked a lot different when I started, but I kept changing things and I could never get it to work. So don't use this as an example for your own code, it's likely way off. I also added a function to be run with the player higs the down button on the menu, but I can't get this working either. Help!


import pygame
import pygame_menu
from pygame_menu import themes
from pygame_menu.controls import Controller

pygame.init()
pygame.joystick.init()
pygame_menu.controls.KEY_BACK = Controller.back
pygame_menu.controls.KEY_APPLY = Controller.apply
pygame_menu.controls.KEY_MOVE_DOWN = Controller.joy_up
pygame_menu.controls.KEY_MOVE_UP = pygame_menu.controls.Controller.joy_up
def goDown():
    print('go Down')
    pygame_menu.controls.KEY_MOVE_DOWN = 1


while True:
    surface = pygame.display.set_mode((600, 400))

    def set_difficulty(value, difficulty):
        print('breakpoint!')
        # Do the job here !
        pass
    def start_the_game():
        menu._down
        print('breakpoint!')
        # Do the job here !
        pass
    # pygame_menu.controls.Controller.joy_axis_x_left()

    menu = pygame_menu.Menu('Welcome', 400, 300,
                        theme=pygame_menu.themes.THEME_BLUE)

    menu.add.text_input('Name :', default='John Doe')
    menu.add.selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=set_difficulty)
    menu.add.button('Play', start_the_game)
    menu.add.button('Go Down', goDown)
    menu.add.button('Quit', pygame_menu.events.EXIT)

    menu.mainloop(surface)
Not sure, but this post seems like it discusses hookup of USB controller:
https://stackoverflow.com/questions/6030...ith-python
Forget about the menu and focus on getting events from the controller. Can you do that? Can you see controller button presses?
import pygame


def pgprint(text):
    text = f"{text}::{pygame.joystick.get_count()}"
    surface = font.render(text, True, "black")
    x = (700 - surface.get_width()) // 2
    y = (100 - surface.get_height()) // 2
    screen.fill("white")
    screen.blit(surface, (x, y))
    pygame.display.update()


pygame.init()
screen = pygame.display.set_mode((700, 100))
font = pygame.font.Font(None, 16)

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

pygame.quit()