| orphan: |
|---|
We can get a list of connected controllers by using arcade.get_controllers().
controllers = arcade.get_controllers()
if controllers:
self.controller = controllers[0]
self.controller.open()
else:
print("There are no controllers.")
self.controller = NoneTo simplify hot-plugging of Controllers, the ControllerManager class is available. To use a ControllerManager, first create an instance:
manager = arcade.input.ControllerManager()To handle controller connections, attach handlers to the following methods:
@manager.event
def on_connect(controller):
print(f"Connected: {controller}")
@manager.event
def on_disconnect(controller):
print(f"Disconnected: {controller}")The controllers values can be obtained by using self.controller.x and self.controller.y. This can be used to update the current positon of an object.
def update(self, delta_time):
# Update the position according to the game controller
if self.controller:
print(self.controller.leftx, self.controller.lefty)
self.object.change_leftx = self.controller.leftx
self.object.change_lefty = -self.controller.leftyControllers have a strictly defined set of inputs. These inputs are the following:
| Button/Action | Notes |
|---|---|
| A | The “south” face button |
| B | The “east” face button |
| X | The “west” face button |
| Y | The “north” face button |
| LeftShoulder | |
| RightShoulder | |
| Start | Called “options” on some controllers |
| Back | Called “select” or “share” on some controllers |
| Guide | Usually in the center, with a company logo |
| LeftStick | Pressing in on the left analog stick |
| RightStick | Pressing in on the right analog stick |
| DPLeft | |
| DPRight | |
| DPUp | |
| DPDown |
An event is dispatched when any of the values of the buttons are changed. These events can be handle like this:
@controller.event
def on_button_press(controller, button_name):
if button_name == 'a':
# start firing
elif button_name == 'b':
# do something else
@controller.event
def on_button_press(controller, button_name):
if button_name == 'a':
# start firing
elif button_name == 'b':
# do something else- The values will be between -1 and +1, with 0 being a centered joystick.
- The x-axis numbers will be negative if the stick goes left, positive for right.
- The y-axis numbers will be opposite of what you might expect. Negative for up, positive for down.
| Left & Right | Centered | Up & Down |
|---|---|---|
| (-1, 1) | 0 | (-1, 1) |
A centered controller might have a value not at 0, but at 0.0001 or some small number. This will make for a small “drift” on a person’s character. We often counteract this by having a “dead zone” where if the number is below a certain value, we just assume it is zero to eliminate the drift.
How we take care of the dead zone:
After
import arcadeadd the following line at the top of the code to define a constant DEAD_ZONE :
DEAD_ZONE = 0.02and adding the following code to the update:
def update(self, delta_time):
# Update the position according to the game controller
if self.controller:
# Set a "dead zone" to prevent drive from a centered controller
if abs(self.controller.x) < DEAD_ZONE:
self.object.change_x = 0
else:
self.object.change_x = self.controller.x * MOVEMENT_SPEED
# Set a "dead zone" to prevent drive from a centered controller
if abs(self.controller.y) < DEAD_ZONE:
self.object.change_y = 0
else:
self.object.change_y = -self.controller.y * MOVEMENT_SPEEDSome types of controllers:
- Joystick hats: the directional pads on game controller. It allows you to move in eight directions (up, down, left, right and the diagonals).
- Xbox controller: This is a popular gamepad used with Xbox consoles and Windows PCs. It has two thumbsticks, a directional pad, and various buttons.
- PlayStation controller: This is the gamepad used with PlayStation consoles. It has two thumbsticks, a directional pad, and various buttons.
- Nintendo Switch controller: This is the gamepad used with Nintendo Switch consoles. It has two detachable Joy-Con controllers, each with a joystick, buttons, and motion sensors.
