pygame._sdl2.controller
Pygame module to work with controllers.
— initialize the controller module
— Uninitialize the controller module.
— Returns True if the controller module is initialized.
— Sets the current state of events related to controllers
— Gets the current state of events related to controllers
— Get the number of joysticks connected
— Check if the given joystick is supported by the game controller interface
— Get the name of the contoller
— Create a new Controller object.

该模块提供对DualShock 4或Xbox 360控制器等常见控制器类型的控制:它们有两个模拟杆、两个触发器、两个肩部按钮、一个dpad、侧面的4个按钮、中间的2个(或3个)按钮。

PYGAME使用Xbox控制器的命名约定(就像按钮的a、b、x、y一样),但它们总是指相同的按钮。例如 CONTROLLER_BUTTON_X 始终是右侧4个按钮中最左侧的按钮。

控制器可以生成以下事件:

CONTROLLERAXISMOTION, CONTROLLERBUTTONDOWN, CONTROLLERBUTTONUP,
CONTROLLERDEVICEREMAPPED, CONTROLLERDEVICEADDED, CONTROLLERDEVICEREMOVED

此外,如果使用SDL2.0.14或更高版本构建pyGame,还可以生成以下事件(要获取SDL的版本,请使用 pygame.version.SDL()tupled integers of the SDL library version ):

CONTROLLERTOUCHPADDOWN, CONTROLLERTOUCHPADMOTION, CONTROLLERTOUCHPADUP

可以通过以下方式启用/禁用这些事件 pygame._sdl2.controller.set_eventstate()Sets the current state of events related to controllers 请注意,控制器也可以生成操纵杆事件。此功能仅切换与控制器相关的事件。

备注

请参阅 pygame.joystickPygame module for interacting with joysticks, gamepads, and trackballs. 以获得更多功能但更高级的API。

New in pygame 2: 本模块需要SDL2。

pygame._sdl2.controller.init()
initialize the controller module
init() -> None

初始化控制器模块。

pygame._sdl2.controller.quit()
Uninitialize the controller module.
quit() -> None

取消对控制器模块的初始化。

pygame._sdl2.controller.get_init()
Returns True if the controller module is initialized.
get_init() -> bool

测试是否 pygame._sdl2.controller.init() 被召唤了。

pygame._sdl2.controller.set_eventstate()
Sets the current state of events related to controllers
set_eventstate(state) -> None

启用或禁用连接到控制器的事件。

备注

控制器仍然可以生成操纵杆事件,该功能不会切换这些事件。

Changed in pygame 2.0.2:: 将返回类型从int更改为None

pygame._sdl2.controller.get_eventstate()
Gets the current state of events related to controllers
get_eventstate() -> bool

返回与控制器相关的事件的当前状态,True表示将发布事件。

New in pygame 2.0.2.

pygame._sdl2.controller.get_count()
Get the number of joysticks connected
get_count() -> int

获取连接的操纵杆的数量。

pygame._sdl2.controller.is_controller()
Check if the given joystick is supported by the game controller interface
is_controller(index) -> bool

如果给定的索引可用于创建控制器对象,则返回True。

pygame._sdl2.controller.name_forindex()
Get the name of the contoller
name_forindex(index) -> name or None

返回控制器的名称,如果没有名称或索引无效,则返回None。

pygame._sdl2.controller.Controller
Create a new Controller object.
Controller(index) -> Controller

创建一个新的控制器对象。索引应为介于0和0之间的整数 pygame._sdl2.contoller.get_count() 。控制器也可以从 pygame.joystick.Joystick 使用 pygame._sdl2.controller.from_joystick 。控制器在创建时初始化。

— uninitialize the Controller
— check if the Controller is initialized
— Create a Controller from a pygame.joystick.Joystick object
— Check if the Controller has been opened and is currently connected.
— Returns a pygame.joystick.Joystick() object
— Get the current state of a joystick axis
— Get the current state of a button
— Get the mapping assigned to the controller
— Assign a mapping to the controller
— Start a rumbling effect
— Stop any rumble effect playing
quit()
uninitialize the Controller
quit() -> None

关闭控制器对象。在此之后,PYGAME事件队列将不再从设备接收事件。

可以安全地不止一次地调用它。

get_init()
check if the Controller is initialized
get_init() -> bool

如果控制器对象当前已初始化,则返回True。

static from_joystick()
Create a Controller from a pygame.joystick.Joystick object
from_joystick(joystick) -> Controller

从一个对象创建控制器对象 pygame.joystick.Joystick 对象

attached()
Check if the Controller has been opened and is currently connected.
attached() -> bool

如果已打开并连接了控制器对象,则返回True。

as_joystick()
Returns a pygame.joystick.Joystick() object
as_joystick() -> Joystick object

返回从此控制器的索引创建的pygame.joytick.Joytick()对象

get_axis()
Get the current state of a joystick axis
get_axis(axis) -> int

获取触发器或操纵杆轴的当前状态。AXIS参数必须是以下常量之一:

CONTROLLER_AXIS_LEFTX, CONTROLLER_AXIS_LEFTY,
CONTROLLER_AXIS_RIGHTX, CONTROLLER_AXIS_RIGHTY,
CONTROLLER_AXIS_TRIGGERLEFT, CONTROLLER_AXIS_TRIGGERRIGHT

操纵杆可以返回一个介于-32768和32767之间的值。但是,触发器只能返回介于0和32768之间的值。

get_button()
Get the current state of a button
get_button(button) -> bool

获取按钮的当前状态,True表示按钮被按下。按钮参数必须是以下常量之一:

CONTROLLER_BUTTON_A, CONTROLLER_BUTTON_B,
CONTROLLER_BUTTON_X, CONTROLLER_BUTTON_Y
CONTROLLER_BUTTON_DPAD_UP, CONTROLLER_BUTTON_DPAD_DOWN,
CONTROLLER_BUTTON_DPAD_LEFT, CONTROLLER_BUTTON_DPAD_RIGHT,
CONTROLLER_BUTTON_LEFTSHOULDER, CONTROLLER_BUTTON_RIGHTSHOULDER,
CONTROLLER_BUTTON_LEFTSTICK, CONTROLLER_BUTTON_RIGHTSTICK,
CONTROLLER_BUTTON_BACK, CONTROLLER_BUTTON_GUIDE,
CONTROLLER_BUTTON_START
get_mapping()
Get the mapping assigned to the controller
get_mapping() -> mapping

返回一个包含控制器映射的字典。有关更多信息,请参见 Controller.set_mapping()

Changed in pygame 2.0.2:: 返回类型从 str 至 dict

set_mapping()
Assign a mapping to the controller
set_mapping(mapping) -> int

重新绑定按钮、轴、触发器和DPad。映射应该是一个包含所有按钮、帽子和轴的词典。获取它的最简单方法是使用由 Controller.get_mapping() 。要编辑此映射,请为原始按钮赋值。字典的值必须是按以下方式表示的按钮、帽子或轴:

  • 对于按钮,使用:BX,其中X是按钮的索引。

  • 对于帽子使用:hX.Y,其中X是索引,Y是方向(向上:1,向右:2,向下:3,向左:4)。

  • 对于轴,使用:ax,其中x是轴的索引。

映射示例:

mapping = controller.get_mapping() # Get current mapping
mapping["a"] = "b3" # Remap button a to y
mapping["y"] = "b0" # Remap button y to a
controller.set_mapping(mapping) # Set the mapping

如果添加了新映射,该函数将返回1;如果更新了现有映射,该函数将返回0。

Changed in pygame 2.0.2:: 重命名自 add_mapping 至 set_mapping

Changed in pygame 2.0.2:: 参数类型从 str 至 dict

rumble()
Start a rumbling effect
rumble(low_frequency, high_frequency, duration) -> bool

在控制器上开始隆隆效果,指定强度从0到1。持续时间是效果的长度,以毫秒为单位。将持续时间设置为0将播放该效果,直到另一个覆盖它或 Controller.stop_rumble() 叫做。如果效果已经在播放,则它将被覆盖。

如果成功播放隆隆声,则返回True;如果控制器不支持,则返回False;或者 pygame.version.SDL()tupled integers of the SDL library version 低于2.0.9。

New in pygame 2.0.2.

stop_rumble()
Stop any rumble effect playing
stop_rumble() -> None

停止控制器上播放的任何隆隆效果。看见 Controller.rumble() 了解更多信息。

New in pygame 2.0.2.




Edit on GitHub