python - Pygame,操纵杆检测不起作用

标签 python pygame joystick

我正在尝试使用 Pygame 检测 USB 操纵杆。 每次运行此代码时,程序都会崩溃并出现错误: 第 10 行中的“操纵杆未初始化”。

import pygame as p

p.init()
stick = p.joystick.Joystick(0)
#get init returns always False
print("initialized:",bool(stick.get_init()))
#getting the name works as it should
print(stick.get_name())
#says always "pygame.error: Joystick not initialized"
print("axis_0",stick.get_axis(0))   
p.quit()

我做错了什么还是这是一个错误? (我在 Python 3.2 和 Windows 7 上使用最新的 pygame 版本。) 感谢您提供的每条信息

最佳答案

我自己刚刚开始使用 Pygame。我首先使用了 pygame.joystick.init() 。所以,我猜你会用 p.joystick.init() 替换 p.init()

来自 Pygame 文档:(http://www.pygame.org/docs/ref/joystick.html)

You can call the Joystick.get_name - get the Joystick system name and Joystick.get_id - get the Joystick ID functions without initializing the Joystick object.

我认为你还必须在其中添加一行内容:

stick.init()

然后初始化摇杆并从中接收事件。

关于python - Pygame,操纵杆检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11382335/

相关文章:

python - 如何杀死多进程中的所有 Pool worker?

python - 如何使用 matplotlib 绘制 3D 数组(如像素图像序列)

python - 在 if-else 语句中使用 in range(..)

ios - 寻找仅用于直接上/下左/右方向的操纵杆

sdl - 为什么 SDL_NumJoysticks() 从不更新?

delphi - Delphi有 'joystick'控件吗?

python - 尽管有 uid,Django 后保存信号被调用两次

python - 如何更改pygame图标?

python - 在pygame中从缓冲区播放声音

python - 为正确版本的 Python 安装 python 模块。 Ubuntu