Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
491d937
background
JDLiang100 Feb 14, 2023
47dfe2b
Added a zombie character
JDLiang100 Feb 14, 2023
694b005
object moves
JDLiang100 Feb 14, 2023
cf7c02a
deleted the .py file and set up the .rst file
JDLiang100 Feb 18, 2023
ab61c8c
trial
JDLiang100 Feb 18, 2023
033dd77
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
8851483
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
4d88f11
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
c4c7f52
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
85b5203
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
0a4cb8f
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
a3d592f
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
f7b2b96
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
2dd834e
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
bd693b8
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
501cf55
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
5aaeb0b
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
a2422d8
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
a5d2297
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
d501ac2
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
d56ed9f
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
e1cf18e
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
cea4253
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
9e33610
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
5ebd12f
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
5df0b72
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
c849727
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
cb875ea
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
03bba81
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
db9def6
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
da68826
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
0123aa0
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
3b109ce
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
f510701
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
129bb33
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
556ece9
Update joystick_controller.rst
JDLiang100 Feb 18, 2023
422aee7
Added inforamtiona about how (-1, 0, 1) workds
Feb 18, 2023
4558051
Expalined (-1, 0, 1) for controller joystick
Feb 18, 2023
eb68dd1
Explained values for joystick
Aurelioghs Feb 18, 2023
c9511fb
Update joystick_controller.rst
Aurelioghs Feb 19, 2023
ef2c7cb
Update joystick_controller.rst
michellejtan Feb 22, 2023
676b6fc
Update joystick_controller.rst
Aurelioghs Feb 22, 2023
6eafa07
Update joystick_controller.rst
Aurelioghs Feb 22, 2023
067f4c7
Update joystick_controller.rst
michellejtan Feb 23, 2023
05254cb
Update joystick_controller.rst
michellejtan Feb 23, 2023
45b193a
Update joystick_controller.rst
michellejtan Feb 23, 2023
41db7b3
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
718ab0b
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
d86a9c9
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
2085bfd
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
e4a5a09
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
0e9c7ca
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
4fc3678
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
6cca99e
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
36c641f
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
39d9764
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
f8bc932
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
1ee7522
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
1672f49
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
ab1220b
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
38cbae2
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
e17dc4b
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
321eee8
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
7991834
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
d53a2ba
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
0db2f0f
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
97ae054
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
98d260d
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
1192450
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
5aba197
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
5274421
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
fca5b44
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
072ccfb
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
651d0e6
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
4cdef54
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
2836169
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
90b7f2e
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
e1e2f64
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
7891791
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
bf00687
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
07213d1
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
8a1c167
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
2b2e379
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
9e40cf8
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
20e865e
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
097a24b
Update joystick_controller.rst
JDLiang100 Feb 25, 2023
261f743
Update joystick_controller.rst
michellejtan Feb 25, 2023
19e0dfa
Update joystick_controller.rst
michellejtan Feb 25, 2023
b1f11d8
Update joystick_controller.rst
Aurelioghs Feb 26, 2023
21d2bb9
deleted temprunner
JDLiang100 Mar 1, 2023
86c2e9c
Update joystick_controller.rst
JDLiang100 Mar 1, 2023
ff0a276
Rename joystick_controller.rst to controller.rst
JDLiang100 Mar 4, 2023
5948e21
Update controller.rst
JDLiang100 Mar 4, 2023
86c7b9c
changed name of the controller picture
JDLiang100 Mar 4, 2023
68f3887
Update controller.rst
JDLiang100 Mar 4, 2023
ba56343
Update controller.rst
JDLiang100 Mar 4, 2023
6468e3d
Update controller.rst
JDLiang100 Mar 4, 2023
74e9424
Update controller.rst
JDLiang100 Mar 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added doc/example_code/how_to_examples/controller.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
186 changes: 186 additions & 0 deletions doc/example_code/how_to_examples/controller.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
.. _example-code:

How To Get Connected Controllers
================================
We can get a list of connected controllers by using :code:`arcade.get_controllers()`.

.. code-block:: python

controllers = arcade.get_controllers()
if controllers:
self.controller = controllers[0]
self.controller.open()
else:
print("There are no controllers.")
self.controller = None

To simplify hot-plugging of Controllers, `the ControllerManager` class is available.
To use a ControllerManager, first create an instance:

.. code-block:: python

manager = arcade.input.ControllerManager()

To handle controller connections, attach handlers to the following methods:

.. code-block:: python

@manager.event
def on_connect(controller):
print(f"Connected: {controller}")

@manager.event
def on_disconnect(controller):
print(f"Disconnected: {controller}")

Controllers Values
==================
The controllers values can be obtained by using :code:`self.controller.x` and :code:`self.controller.y`. This can be used to update the current positon of an object.

.. code-block:: python

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.lefty

How To Use Buttons
===================

Controllers 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:

.. code-block:: python

@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 (-1.0 to 1.0) Values On Controller
======================================
* 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.

.. list-table:: controller
:widths: 25 25 50
:header-rows: 1

* - Left & Right
- Centered
- Up & Down
* - (-1, 1)
- 0
- (-1, 1)


The controller Movements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. image:: controller.png
:width: 400px
:align: center
:alt: Screenshot of controller movements


Deadzone
========
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

.. code-block:: console

import arcade

add the following line at the top of the code to define a constant :code:`DEAD_ZONE` :

.. code-block:: console

DEAD_ZONE = 0.02


and adding the following code to the :code:`update`:

.. code-block:: python

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_SPEED


Different Types Of Controllers
==============================
Some types of controllers:

1. Joystick hats: the directional pads on game controller. It allows you to move in eight directions (up, down, left, right and the diagonals).

2. Xbox controller: This is a popular gamepad used with Xbox consoles and Windows PCs. It has two thumbsticks, a directional pad, and various buttons.

3. PlayStation controller: This is the gamepad used with PlayStation consoles. It has two thumbsticks, a directional pad, and various buttons.

4. 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.