Every game engine runs your game in a loop: init once, then update and draw about 60 times a second. Make a ball move across the screen in microStudio.
Every game you have played redraws the whole screen about 60 times a second. Between pictures, the game moves everything a tiny bit.
microStudio is a free game engine that runs in the browser. You write Python there, and it runs your game. These lessons show each game working on this page; you make your own copy on microstudio.dev.
A microStudio game is three functions, and you never call them yourself: the engine does.
init() runs once, when the game starts: set things up here.update() runs 60 times a second: move things, check keys, keep score.draw() runs after each update: clear the screen and draw everything where it is now.The middle of the screen is (0, 0). y goes up to 100 at the top and down to -100 at the bottom; x goes left and right from the middle.
Make your prediction first. Help opens once you've locked it in.
def init():
global x
x = 0
def update():
global x
x += 1
def draw():
screen.clear("rgb(10,10,40)")
screen.fillRound(x, 0, 10, 10, "rgb(255,210,0)")
Nobody calls update() in this code. Who does, and how often?
Watch the number at the top: that is x, going up by 1 sixty times a second. When it passes 100, update() sends the dot back to -100.
Now make your own copy. Open microstudio.dev in a new tab and click Create, then make a new project. Choose Python as its language (in the new project box, or later in the project's Settings). Open its Code tab, replace everything in the file main with the code above (use its Copy button), then press Run ▶. Keep this tab open for every lesson.
def init():
global x
x = -100
def update():
global x
x += 1
if x > 100:
x = -100
def draw():
screen.clear("rgb(10,10,40)")
screen.fillRound(x, 0, 10, 10, "rgb(255,210,0)")
screen.drawText("x = " + str(x), 0, 80, 10, "#FFF")
def init():
global x
x = -100
def update():
global x
x += 1
if x > 100:
x = -100
def draw():
screen.clear("rgb(10,10,40)")
screen.fillRound(x, 0, 10, 10, "rgb(255,210,0)")
screen.drawText("x = " + str(x), 0, 80, 10, "#FFF")
The engine decides, not you.
Count the frames.
The same game, one line missing.
Match each function with when the engine runs it.
This stops with an error: cannot access local variable 'x'. Click the line where it goes wrong.
def init():
global x
x = -100
def update():
global x
x += 1
if x > 100:
x = -100
def draw():
screen.clear("rgb(10,10,40)")
screen.fillRound(x, 0, 10, 10, "rgb(255,210,0)")
screen.drawText("x = " + str(x), 0, 80, 10, "#FFF")
In your microStudio project, make the dot move up the screen instead, and start again at the bottom.
Use y instead of x: the dot is drawn at (x, 0) now. Draw it at (0, y).
Start y at -100, add 1 to it in update(), and when y > 100, set it back to -100.
Make the dot bounce off each edge instead of starting again. The game on this page shows a bouncing dot.
Keep a speed in a second variable, dx = 1, and add it: x += dx.
When x > 100 or x < -100, turn the speed round: dx = -dx. Both x and dx need to be in the global line.
Start from this empty game in microStudio and make at least two things move in different ways: one faster than the other, one going the other way, one changing size... The game on this page is one idea. screen.fillRect(x, y, width, height, colour) draws a box, screen.fillRound(...) a dot, and screen.drawText(text, x, y, size, colour) writes.
def init():
pass
def update():
pass
def draw():
screen.clear("rgb(10,10,40)")
Every game engine works this way: Unity, Godot, Pico-8 and MakeCode Arcade all call an update and a draw function every frame. Next: move something with the arrow keys.
Students meet microStudio's game loop (init once, then update and draw 60 times a second), copy a moving-dot game into their own microStudio project, fix a missing global, then make a dot move up and bounce, and make two things move their own way. About 55 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. Students write their games on microstudio.dev, not on our site: before the lesson, open it on a student device at school, make a project, choose Python and press Run (the site's games here are only for playing). Decide on accounts: a guest can start at once with no email, but a guest's projects are deleted after about 15 days unless an account is made (a nickname and password; email is optional). For a short unit guests are fine if students keep a copy of their code; for longer work, make accounts with nicknames that are not real names. The lesson's Python is ordinary Python run by Brython: global is needed in update() for any variable it changes, exactly as in the turtle course's events lesson. microStudio's screen is 200 tall with (0, 0) in the middle and y up; its width depends on the window.
Getting into microStudio and the Python setting in the first 15 minutes, then the forgotten global. Have the project made before the lesson if time is short.
Stretch: Make the dot change colour as it moves, or grow and shrink with a size variable.
Support: Give the bounce as a sentence first ("a speed that turns round at the edges"), then the dx = -dx line.
Next lesson: the arrow keys move a paddle. The same update-and-draw loop runs every engine, from MakeCode Arcade to Unity and Godot.