Docs › game library

game.pressed()

Read the keyboard. game.pressed("left") is True while the left arrow is held down, so you can steer, jump and shoot.

How to write it

game.pressed(key)
key
The key's name, as text in quotes. Capitals don't matter: "Left" and "left" are the same.

It gives back True while that key is held down, and False when it isn't. It never waits for a key, so the game keeps running.

Key names

  • Arrow keys: "left", "right", "up", "down"
  • Space bar: "space"
  • Letters: "a" to "z". "w", "a", "s", "d" make a second set of arrows for player two.
  • Numbers: "0" to "9", the number keys along the top
  • Others: "enter", "shift", "backspace", "escape"

While a game runs, the arrow keys and space bar control the game instead of scrolling the page.

A full example

Fly the rocket with the arrow keys, even diagonally. It can't leave the screen. Press space to fire: one laser per press, however long you hold it.

import game

game.window(480, 360, background="#0b1020")

ship = game.sprite("rocket", 240, 300, size=50)
ship.angle = -90                                     # turn it to point up
laser = game.sprite("laser", -100, -100, size=30)    # parked off screen
laser.angle = -90
info = game.label("Shots: 0", 240, 24, color="#ffffff")

speed = 5
shots = 0
space_was_down = False

while game.playing():
    # Held keys: move every frame the key is down.
    # Four separate ifs (not elif), so two arrows at once go diagonally.
    if game.pressed("left"):
        ship.x = ship.x - speed
    if game.pressed("right"):
        ship.x = ship.x + speed
    if game.pressed("up"):
        ship.y = ship.y - speed
    if game.pressed("down"):
        ship.y = ship.y + speed

    # Keep the ship on screen
    ship.x = max(25, min(455, ship.x))
    ship.y = max(25, min(335, ship.y))

    # One shot per press: only fire when space has JUST gone down
    space_down = game.pressed("space")
    if space_down and not space_was_down:
        laser.x = ship.x
        laser.y = ship.y - 30
        shots = shots + 1
        info.content = "Shots: " + str(shots)
    space_was_down = space_down

    laser.y = laser.y - 10            # the laser flies up

    game.frame()

Held down, or once per press?

game.pressed() is True for every frame the key is held. At 30 frames a second, holding a key for one second makes it True 30 times. That is perfect for walking and steering, but not for things that should happen once, like firing, jumping, or pausing.

For once per press, remember whether the key was down last frame, and only act when it has just gone down:

was_down = False                 # before the loop

while game.playing():
    down = game.pressed("space")
    if down and not was_down:    # it was up last frame, down now
        game.sound("jump")
    was_down = down              # remember for next frame
    game.frame()

The mouse has this built in: game.clicked() is True once per click, and on-screen game.button() buttons work the same way.

Diagonals and edges

  • Diagonals: use a separate if for each arrow. With elif, only the first key that matches counts, so up and right together just goes up.
  • Diagonals are a bit faster: moving 5 across and 5 up at once is about 7 pixels in a slanted line. In most games nobody notices. If yours is a race, that's a shortcut!
  • Keeping on screen: max(25, min(455, ship.x)) means "no smaller than 25, no bigger than 455". Use half the sprite's size as the gap, so it stops with its edge at the wall, not its middle.
  • Wrapping round: for a space game, send it to the other side instead: if ship.x > 480: ship.x = 0.

Why aren't my keys working?

  • It's only checked once. game.pressed() must be inside the while game.playing(): loop, so it is asked again every frame. Above the loop it is checked once, at the very start, when no key is down.
  • The key name is wrong. It is "space", not " ", and "left", not "arrowleft" or "leftarrow".
  • I'm typing in a text box. Keys typed into a game.textbox() or any other box on the page go to that box, not the game. Click the game window, then try again.
  • One press fires lots of times. That's the held-down behaviour. Use the "just went down" pattern above.
  • It doesn't work on a phone. Phones have no arrow keys. Use the mouse and touch calls, like game.clicked(), game.mouse_x(), or game.button(), to add on-screen controls.