Docs › game library

game.sprite()

Make a sprite: a player, a coin, an enemy, anything that moves. Pick a built-in picture, any emoji, or art you drew yourself in the Asset studio.

How to write it

game.sprite(skin, x, y, size=40, asset=False)
skin
What the sprite looks like: a built-in skin's number (0) or name ("chicken"), or any emoji ("🦖"). With asset=True, it is the id number of your own art.
x, y
Where the middle of the sprite goes. Leave them both out and it starts in the middle of the window.
size
Optional. How big it is, in pixels. Leave it out and you get 40.
asset
Optional. Set it to True to use a sprite you designed in the Asset studio. Then skin is that art's id number.

It gives back the sprite, so always save it in a variable: hero = game.sprite("chicken", 100, 200). You use that name to move it later.

The built-in skins

Use the number or the name, whichever you find easier. game.sprite(4) and game.sprite("coin") are the same coin.

  • 0 chicken, 1 dog, 2 bird, 3 egg, 4 coin, 5 basket
  • 6 shocked, 7 calm (two faces)
  • 8 turtle, 9 car, 10 mouse
  • 11 rocket, 12 asteroid, 13 laser
  • 14 snake, 15 pacman (it chomps all by itself), 16 ghost, 17 pacman2 (Pac-Man with his mouth shut), 18 kid

Anything that is not a skin name is drawn as text, so any emoji works: game.sprite("🍕", 200, 100). For words, like a score, use game.label() instead.

Your own art from the Asset studio works too. Each asset has an id number (shown like #2), and you pass that number with asset=True:

hero = game.sprite("chicken", 100, 200)    # built-in, by name
coin = game.sprite(4, 250, 100)            # built-in, by number
dino = game.sprite("🦖", 300, 200)          # any emoji
mine = game.sprite(2, 380, 200, asset=True) # your Asset studio art #2

A full example

Steer the chicken with the arrow keys to collect the spinning coin. The chicken turns to face the way it walks, and the coin disappears when you grab it:

import game

game.window(480, 360, background="#1d3557")

hero = game.sprite("chicken", 80, 180, size=56)
coin = game.sprite("coin", 380, 180, size=40)
dino = game.sprite("🦖", 380, 300, size=50)
note = game.label("Get the coin!", 240, 24, color="#ffffff")

while game.playing():
    if game.pressed("left"):
        hero.x = hero.x - 5
        hero.scale_x = -1         # mirror it to face left
    if game.pressed("right"):
        hero.x = hero.x + 5
        hero.scale_x = 1          # face right (the way the art is drawn)
    if game.pressed("up"):
        hero.y = hero.y - 5
    if game.pressed("down"):
        hero.y = hero.y + 5

    coin.angle = coin.angle + 6   # spin 6 degrees every frame

    if coin.visible and hero.touches(coin):
        coin.visible = False      # hide the coin
        note.content = "Got it!"
        hero.size = 80            # and grow a little
        dino.content = "😮"       # the dinosaur turns into a surprised face

    game.frame()

Changing a sprite

A sprite is like a box of settings. Change one, and the next game.frame() draws it the new way.

  • .x and .y: where it is. Change them to move it: hero.x = hero.x + 5.
  • .size: how big it is, in pixels.
  • .content: what it shows. Set it to another number, name or emoji to swap the picture: hero.content = "ghost".
  • .angle: turn it, in degrees. Add a little every frame to make it spin.
  • .scale_x and .scale_y: stretch it. 2 is twice as wide (or tall), and -1 mirrors it so it faces the other way.
  • .visible: False hides it, True shows it again. hero.hide() and hero.show() do the same.
  • .remove(): takes it off the screen for good, once it is collected or destroyed.
  • a.touches(b): True when two sprites overlap. See touches().

There are more, like .layer (what is drawn on top), .anchor and .animate(). The full reference lists them all.

Why isn't my sprite working?

  • It doesn't show up at all. Nothing is drawn until game.frame() runs, so check your game has a loop that ends with it. Also check x and y are inside the window: in a 480 by 360 window, y = 400 is below the bottom.
  • I see a purple square. That is a number with no skin, like game.sprite(25). The skins go from 0 to 18. Your own art also shows a faint square for a moment while it loads.
  • My Asset studio art shows the wrong thing. Without asset=True, the number is a built-in skin: game.sprite(2) is the bird, but game.sprite(2, asset=True) is your asset #2.
  • It won't move. You need to keep the sprite in a variable, then change name.x or name.y inside the loop.
  • A hidden sprite still gets touched. touches() does not care whether a sprite is visible, and that includes removed ones. That is why the example checks coin.visible and hero.touches(coin).
  • A word shows up invisible. Plain text as a skin, like game.sprite("Hello"), is drawn like an emoji and often comes out invisible. Use game.label("Hello", x, y) for words.