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 ("🦖"). Withasset=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
Trueto use a sprite you designed in the Asset studio. Thenskinis 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.
0chicken,1dog,2bird,3egg,4coin,5basket6shocked,7calm (two faces)8turtle,9car,10mouse11rocket,12asteroid,13laser14snake,15pacman (it chomps all by itself),16ghost,17pacman2 (Pac-Man with his mouth shut),18kid
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.
.xand.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_xand.scale_y: stretch it.2is twice as wide (or tall), and-1mirrors it so it faces the other way..visible:Falsehides it,Trueshows it again.hero.hide()andhero.show()do the same..remove(): takes it off the screen for good, once it is collected or destroyed.a.touches(b):Truewhen 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 checkxandyare inside the window: in a 480 by 360 window,y = 400is below the bottom. - I see a purple square. That is a number with no skin, like
game.sprite(25). The skins go from0to18. 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, butgame.sprite(2, asset=True)is your asset #2. - It won't move. You need to keep the sprite in a variable, then change
name.xorname.yinside 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 checkscoin.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. Usegame.label("Hello", x, y)for words.