树莓派运行Pygame摇杆代码报错Invalid joystick device number求助
Hey there, let's work through this joystick issue together—since your code worked fine months ago and hasn't changed, the problem is almost certainly rooted in hardware connection, system-level recognition, or minor environment shifts. Here's a step-by-step breakdown to fix it:
1. Verify Hardware Connections First
- Double-check your joystick's USB cable (or wireless pairing if it's a Bluetooth controller). Sometimes cables loosen over time, or wireless controllers drop their connection unexpectedly.
- Try plugging the joystick into a different USB port on your Pi—some ports might have power delivery issues that prevent the controller from being detected properly.
- If it's a battery-powered wireless controller, make sure the batteries are charged or fresh. Dead batteries are an easy oversight!
2. Check if the System Recognizes Your Joystick
Before blaming Pygame, confirm your Raspberry Pi's OS can see the controller at all:
- Run the command
lsusbin the terminal. Look for an entry matching your joystick (e.g., "Microsoft Xbox 360 Controller" or "Logitech Gamepad F310"). If it's not listed, your OS isn't detecting it—focus on fixing hardware or driver issues here. - If
lsusbshows the device, runjstest /dev/input/js0. If this throws an error like "No such file or directory", the system hasn't created the input device file for your joystick, which means there's a driver or permissions problem to resolve.
3. Adjust Your Pygame Code to Validate Device Count
Your current code assumes a joystick is present at index 0, but let's add a check to confirm Pygame can see any controllers first. Modify your code like this:
import pygame pygame.init() # Get the number of detected joysticks first joystick_count = pygame.joystick.get_count() print(f"Detected {joystick_count} connected joysticks") if joystick_count > 0: j = pygame.joystick.Joystick(0) j.init() print(f"Successfully initialized joystick: {j.get_name()}") else: print("No joysticks detected by Pygame!")
If this prints "0 joysticks", Pygame isn't picking up the controller—head back to system-level checks.
4. Check for Pygame or System Updates
Even if your code didn't change, updates to Pygame or your Raspberry Pi OS could break compatibility:
- Check your current Pygame version with
pip show pygame. If it's different from when your code worked, try rolling back to the older version withpip install pygame==[your-old-version-number](replace the bracket text with the version you used before). - If you've updated your Pi's OS recently, try rebooting the system—sometimes kernel updates require a restart to load new driver modules properly.
5. Fix Permissions for Input Devices
Occasionally, the permissions for the joystick input file get messed up, preventing Pygame from accessing it:
- Try running your script with
sudofirst (e.g.,sudo python3 your-script.py). If this works, the issue is definitely permissions. - To fix it permanently, add your user to the
inputgroup with this command:
Log out and back in (or reboot) for the changes to take effect.sudo usermod -aG input $USER
Start with the hardware checks first—they're the most common culprits when a previously working setup stops functioning. Let me know if any of these steps get you back up and running!
内容的提问来源于stack exchange,提问作者Ctpelnar1988

