Python Forum
[PyGame] pygame-manu : using controller buttons to move around menu
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] pygame-manu : using controller buttons to move around menu
#1
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)
Reply
#2
Not sure, but this post seems like it discusses hookup of USB controller:
https://stackoverflow.com/questions/6030...ith-python
Reply
#3
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()
Reply
#4
The thread is a troubleshooting discussion around using a gamepad/controller in Python applications (likely with libraries such as pygame or similar input handlers). The main issue being addressed is that the controller is either not being detected properly or is not producing input events inside the Python program even though it works at the system level. The typical guidance in such cases revolves around checking correct joystick initialization, ensuring the event loop is continuously polling input events, verifying that the system recognizes the device (e.g., via input devices/USB), and confirming that the correct driver or backend is being used. In short, it is about fixing inconsistent gamepad input detection in Python-based projects rather than a hardware fault.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] my pygame car wont move Erio85 1 2,422 Apr-24-2023, 04:52 PM
Last Post: Erio85
  How to make an image move in relation to another in PYGAME CompleteNewb 1 3,463 Nov-10-2021, 03:38 PM
Last Post: metulburr
  Getting a ship to move in pygame djwilson0495 2 6,195 Dec-09-2020, 11:03 AM
Last Post: djwilson0495
  Pygame - Images As Buttons vman44 3 18,010 Mar-20-2020, 08:13 PM
Last Post: vman44
  [PyGame] Problems w/ controller Zman350x 5 8,063 Oct-29-2019, 10:31 PM
Last Post: Zman350x
  [pygame] Equiping inventory slots with invisible buttons SheeppOSU 6 7,275 Apr-26-2019, 08:45 PM
Last Post: SheeppOSU
  Problems with loading buttons (pygame) SheeppOSU 2 4,630 Apr-12-2019, 08:04 PM
Last Post: SheeppOSU
  Buttons in PyGame mzmingle 4 17,622 Oct-09-2018, 05:19 PM
Last Post: Mekire
  Menus, buttons and other widgets for Pygame Olleus 4 14,936 Apr-17-2017, 11:08 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020