pygame-input
pygame-input will simplify your input handling with pygame
by providing a simple way to assign callbacks to given key press
or joystick events.
Look how easy it is to use:
import pygame
from pygame_input import Inputs, Button, JoyButton
inputs = Inputs()
inputs["fire"] = Button(pygame.K_SPACE, JoyButton(1))
inputs["fire"].on_press_repeated(player.fire, delay=0.1)
This will call your player.fire() function every 0.1 seconds while
any button, whether it is the space bar or the button one on your
joystick is pressed.
Features
- Joystick
- Boolean input values (ie. is key pressed ?) and scalar input values (ie. how much is the stick on the left ?)
- Register callbacks on:
- press,
- release,
- double-press
- or all the time
- Every function and class has a detailed docstring
What pygame-input is not (yet ?):
- handling key modifiers
- handling mouse input
- recognising mouse gestures
- doing gamepad configuration (ie. you need to know which id is the "A" button)
Though the first two will probably be implemented quite soon, depending on when I need them.
Installation
Install pygame-input by running::
pip install pygame-input
Alternativelly you can just copy pygame_input.py into your
game folder as the whole code is just one file. Feel free to
modify it as much as you need.
Usage
Defining the inputs
The first step to be able to to register callbacks
is to create an Inputs object, which is bacically a
python's dict with a trigger method. We will
come back on trigger later. So:
from pygame_input import *
inputs = Inputs()
Then you can add all the input types in dictionary.
There are two input types: Button and Axis.
A Button represents anything that is either
pressed or released, like a key on your keyboard or
a button of your joystick.
An Axis is for anything that takes a value between -1 and 1,
for instance, the position of a joystick, but a pair of
keys can also be seen as an axis: if we consider the pair of
left and right arrows, the axis would have value -1 if the left key
is pressed, 1 if it is the right and 0 if none or both are pressed.
This is very convenient for moving a player.
Enough talking, let's add a Button to our inputs.
inputs["fire"] = Button(pygame.K_SPACE, pygame.K_RETURN)
The Button takes every posible way to press it as parameters.
Here we want to trigger the fire input when we press either
the space bar or return. Any pygame key code is a valid argument
but if we also want joystick support
inputs["fire"] = Button(pygame.K_SPACE, pygame.K_RETURN, JoyButton(1))
It would also trigger if the button 1 on joystick 0 is pressed.
Note that you need to initialise the joysticks
yourself. If you want to fire only when the button 1 on the 3rd joystick
is pressed, you would pass JoyButton(1, 3) instead.
Other parameters for buttons can be:
QuitEvent()that matches thepygame.QUITevent.JoyAxisTrigger(axis, threshold=0.5, above=True, joy_id=0)that matches when an axis has a value abovethreshold(or below ifabove=False)
We will also add an Axis so our player can move horizontally.
import pygame
from pygame_input import Axis
inputs["hmove"] = Axis(
(pygame.K_LEFT, pygame.K_a),
(pygame.K_RIGHT, pygame.K_d),
JoyAxis(1),
)
What did you do here ? The two first arguments of Axis
are the negative and positive keys. Negative keys corresponds to a value
of -1 (think: left/down) and positive to a value of +1 (think: right/up).
It accepts a single key code or a list of key codes.
After that, can follow any number of JoyAxis that correspond
to an axis on the joystick. The signature of JoyAxis is more complex
but quite explicit :
@dataclass(frozen=True)
class JoyAxis:
axis: int
reversed: bool = False
"""Whether the positive and negative should be reversed."""
threshold: float = 0.2
"""Any value of smaller magnitude will be considered as zero."""
sensibility: float = 1.0
"""Multiply the value by this amount. Useful if a joystick doesn't go all the way to +/-1"""
joy_id: int = 0
"""The id used to initialise the joystick."""
If you don't know about dataclasses, don't worry and consider that
the attributes defined here are the parameters of JoyAxis(). Though
feel free to check what dataclasses are,
because they're nice :)
Adding callbacks
Now that you know everything about defining the inputs, we can add callbacks to them. Those two steps are different, because they can happen at different places in the code. For instance inputs definition can be in the main in on a settings screen. You may also want to add callbacks during the player creation and also register a callback on the camera when the player moves.
Enough theory. There are 5 ways to add a callback f to a Button:
always_call(f):on_press(f)on_release(f)on_double_press(f)on_press_repeated(f, delay)
Their names are self explanatory. on_press_repeated
accepts a delay argument: the callback will be called every delay seconds.
Each callback is always called with one argument:
the Button or Axis that triggered it.
This way you can access the axis value or the button's press_time.
For an Axis there only always_call(f) is available, since the others
don't make sense. Example:
inputs["fire"].on_press_repeated(player.fire, delay=0.5)
inputs["hmove"].allways_call(player.horizontal_move)
Triggering callbacks
To trigger the callbacks the only thing that is needed is
to call inputs.trigger with a list of the events
that happened since last frame.
while True:
# Event handling
inputs.trigger(pygame.events.get())
# Game logic
...
# Draw everything
...
pygame.display.update()
Note: if you also need to process the events in a different way,
you need to convert pygame.events.get() to a list first and use the list:
events = list(pygame.events.get())
inputs.trigger(events)
for event in events:
if event.type == ...:
...
For more, see the examples.
Contribute
- Issue Tracker: https://gitlab.com/ddorn/pygame-input/issues
- Source Code: https://gitlab.com/ddorn/pygame-input
Support
If you are having issues, please let me know. You can open an issue or send me a mail, my email address is on my gitlab profile.
License
The project is licensed under the MIT license.
Metadata
Release files for pygame-input 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygame-input-1.0.0.tar.gz | 8.6 kB | Without provenance | Details for pygame-input-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygame_input-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for pygame_input-1.0.0-py3-none-any.whl |
Total release size: 16.8 kB
Release files / pygame-input-1.0.0.tar.gz
| Download URL | pygame-input-1.0.0.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cccfc4d1662fcd797c883d06e0ad92b3948fc3492750a7ab8da70d2154f7962c
|
|
BLAKE2b-256 checksum How to use checksums |
aecee4a8d964f8bdee78ea52efefe48fcb2950cc754b2db3caddf280c675676d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.5 CPython/3.8.2 Linux/5.5.9-arch1-2
|
Release files / pygame_input-1.0.0-py3-none-any.whl
| Download URL | pygame_input-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c4fd882f2d2fd929283d4cb7f92cba987118a59275feb144c47f183d49dd710
|
|
BLAKE2b-256 checksum How to use checksums |
70392478889a492592c028a18293d9786b88d26629f52145a913a7dffca96dbd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.5 CPython/3.8.2 Linux/5.5.9-arch1-2
|
Security for pygame-input 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.