-
Notifications
You must be signed in to change notification settings - Fork 358
How to use controller documentation/tutorials issue(#1475) #1590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Cleptomania
merged 98 commits into
pythonarcade:development
from
JDLiang100:development
Mar 19, 2023
Merged
Changes from all commits
Commits
Show all changes
98 commits
Select commit
Hold shift + click to select a range
491d937
background
JDLiang100 47dfe2b
Added a zombie character
JDLiang100 694b005
object moves
JDLiang100 cf7c02a
deleted the .py file and set up the .rst file
JDLiang100 ab61c8c
trial
JDLiang100 033dd77
Update joystick_controller.rst
JDLiang100 8851483
Update joystick_controller.rst
JDLiang100 4d88f11
Update joystick_controller.rst
JDLiang100 c4c7f52
Update joystick_controller.rst
JDLiang100 85b5203
Update joystick_controller.rst
JDLiang100 0a4cb8f
Update joystick_controller.rst
JDLiang100 a3d592f
Update joystick_controller.rst
JDLiang100 f7b2b96
Update joystick_controller.rst
JDLiang100 2dd834e
Update joystick_controller.rst
JDLiang100 bd693b8
Update joystick_controller.rst
JDLiang100 501cf55
Update joystick_controller.rst
JDLiang100 5aaeb0b
Update joystick_controller.rst
JDLiang100 a2422d8
Update joystick_controller.rst
JDLiang100 a5d2297
Update joystick_controller.rst
JDLiang100 d501ac2
Update joystick_controller.rst
JDLiang100 d56ed9f
Update joystick_controller.rst
JDLiang100 e1cf18e
Update joystick_controller.rst
JDLiang100 cea4253
Update joystick_controller.rst
JDLiang100 9e33610
Update joystick_controller.rst
JDLiang100 5ebd12f
Update joystick_controller.rst
JDLiang100 5df0b72
Update joystick_controller.rst
JDLiang100 c849727
Update joystick_controller.rst
JDLiang100 cb875ea
Update joystick_controller.rst
JDLiang100 03bba81
Update joystick_controller.rst
JDLiang100 db9def6
Update joystick_controller.rst
JDLiang100 da68826
Update joystick_controller.rst
JDLiang100 0123aa0
Update joystick_controller.rst
JDLiang100 3b109ce
Update joystick_controller.rst
JDLiang100 f510701
Update joystick_controller.rst
JDLiang100 129bb33
Update joystick_controller.rst
JDLiang100 556ece9
Update joystick_controller.rst
JDLiang100 422aee7
Added inforamtiona about how (-1, 0, 1) workds
4558051
Expalined (-1, 0, 1) for controller joystick
eb68dd1
Explained values for joystick
Aurelioghs c9511fb
Update joystick_controller.rst
Aurelioghs ef2c7cb
Update joystick_controller.rst
michellejtan 676b6fc
Update joystick_controller.rst
Aurelioghs 6eafa07
Update joystick_controller.rst
Aurelioghs 067f4c7
Update joystick_controller.rst
michellejtan 05254cb
Update joystick_controller.rst
michellejtan 45b193a
Update joystick_controller.rst
michellejtan 41db7b3
Update joystick_controller.rst
JDLiang100 718ab0b
Update joystick_controller.rst
JDLiang100 d86a9c9
Update joystick_controller.rst
JDLiang100 2085bfd
Update joystick_controller.rst
JDLiang100 e4a5a09
Update joystick_controller.rst
JDLiang100 0e9c7ca
Update joystick_controller.rst
JDLiang100 4fc3678
Update joystick_controller.rst
JDLiang100 6cca99e
Update joystick_controller.rst
JDLiang100 36c641f
Update joystick_controller.rst
JDLiang100 39d9764
Update joystick_controller.rst
JDLiang100 f8bc932
Update joystick_controller.rst
JDLiang100 1ee7522
Update joystick_controller.rst
JDLiang100 1672f49
Update joystick_controller.rst
JDLiang100 ab1220b
Update joystick_controller.rst
JDLiang100 38cbae2
Update joystick_controller.rst
JDLiang100 e17dc4b
Update joystick_controller.rst
JDLiang100 321eee8
Update joystick_controller.rst
JDLiang100 7991834
Update joystick_controller.rst
JDLiang100 d53a2ba
Update joystick_controller.rst
JDLiang100 0db2f0f
Update joystick_controller.rst
JDLiang100 97ae054
Update joystick_controller.rst
JDLiang100 98d260d
Update joystick_controller.rst
JDLiang100 1192450
Update joystick_controller.rst
JDLiang100 5aba197
Update joystick_controller.rst
JDLiang100 5274421
Update joystick_controller.rst
JDLiang100 fca5b44
Update joystick_controller.rst
JDLiang100 072ccfb
Update joystick_controller.rst
JDLiang100 651d0e6
Update joystick_controller.rst
JDLiang100 4cdef54
Update joystick_controller.rst
JDLiang100 2836169
Update joystick_controller.rst
JDLiang100 90b7f2e
Update joystick_controller.rst
JDLiang100 e1e2f64
Update joystick_controller.rst
JDLiang100 7891791
Update joystick_controller.rst
JDLiang100 bf00687
Update joystick_controller.rst
JDLiang100 07213d1
Update joystick_controller.rst
JDLiang100 8a1c167
Update joystick_controller.rst
JDLiang100 2b2e379
Update joystick_controller.rst
JDLiang100 9e40cf8
Update joystick_controller.rst
JDLiang100 20e865e
Update joystick_controller.rst
JDLiang100 097a24b
Update joystick_controller.rst
JDLiang100 261f743
Update joystick_controller.rst
michellejtan 19e0dfa
Update joystick_controller.rst
michellejtan b1f11d8
Update joystick_controller.rst
Aurelioghs 21d2bb9
deleted temprunner
JDLiang100 86c2e9c
Update joystick_controller.rst
JDLiang100 ff0a276
Rename joystick_controller.rst to controller.rst
JDLiang100 5948e21
Update controller.rst
JDLiang100 86c7b9c
changed name of the controller picture
JDLiang100 68f3887
Update controller.rst
JDLiang100 ba56343
Update controller.rst
JDLiang100 6468e3d
Update controller.rst
JDLiang100 74e9424
Update controller.rst
JDLiang100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.