Input

Pyxen supports keyboard, touch, mouse, and gamepad input. All input is read through the input module and updated once per frame, before update() runs.

Keyboard

Access any key by name on input.keyboard:

if input.keyboard.right.down:
    player.x += 2

if input.keyboard.space.pressed:
    jump()

Every key exposes the same properties:

PropertyMeaning
downHeld down this frame
pressedJust pressed this frame (true for one frame)
releasedJust released this frame (true for one frame)
valueAnalog value (0.0 or 1.0 for keys)

Common key names: left, right, up, down, space, enter, escape, az, 09.

Touch

On iPad, touch input comes through input.touches — a list of active touch points:

for touch in input.touches:
    if touch.started:
        spawn_particle(touch.x, touch.y)

Each touch has:

PropertyMeaning
x, yPosition in game coordinates
dx, dyMovement since last frame
pressureTouch pressure (if available)
downCurrently touching
startedTouch began this frame
endedTouch ended this frame

Mouse

Mouse input works on Mac and iPad with a connected mouse or trackpad:

if input.mouse.buttons.left.pressed:
    shoot(input.mouse.x, input.mouse.y)

The mouse exposes:

Gamepads

Pyxen supports up to 4 gamepads. Access them through input.gamepads:

pad = input.gamepads[0]
if pad.connected:
    player.x += pad.left_stick.x * 2
    if pad.buttons.a.pressed:
        jump()

Each gamepad has:

Designing for multiple inputs

A Pyxen game can run on iPad with touch, on iPad with a keyboard, or on Mac with mouse and keyboard. A good approach is to check multiple input sources:

def update():
    dx = 0
    if input.keyboard.right.down:
        dx = 2
    elif input.keyboard.left.down:
        dx = -2

    pad = input.gamepads[0]
    if pad.connected:
        dx = pad.left_stick.x * 2

    player.x += dx

For the full API, see the input reference.