← Games with microStudio

A game loop

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.

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.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

game.py
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?

What happens when the game runs?

RunRun it and compare the result with your prediction.

Run

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.

game.py
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")
Play itClick the game to play. Make your own on microstudio.dev.

InvestigateTrace how each line of code connects to what you see.

Investigate

game.py
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.

init()
update()
draw()

The game has run for 2 seconds. About how far has the dot moved?

This stops with an error: cannot access local variable 'x'. Click the line where it goes wrong.

ModifyChange the code on purpose and check the result.

Modify

game.py
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.

Need a hint?

Use y instead of x: the dot is drawn at (x, 0) now. Draw it at (0, y).

Another hint

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.

Need a hint?

Keep a speed in a second variable, dx = 1, and add it: x += dx.

Another hint

When x > 100 or x < -100, turn the speed round: dx = -dx. Both x and dx need to be in the global line.

Play itClick the game to play. Make your own on microstudio.dev.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Something that moves

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.

game.py
def init():
    pass

def update():
    pass

def draw():
    screen.clear("rgb(10,10,40)")
Play itClick the game to play. Make your own on microstudio.dev.

Info

Where this leads

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.

In the turtle course, events waited for a click or a key. What does a game loop do instead?

For teachers

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.

Students will be able to

Before class

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.

Running the lesson

  1. Predict: act it out: one student says update every time you clap, another moves one step.
  2. Run: get every project made and running before Investigate; pair anyone stuck on the sign-in.
  3. Investigate: the bug is the forgotten global from the turtle events lesson. Ask who remembers it.
  4. Modify: bouncing needs a second variable for the direction; let students try before the second hint.
  5. Make: walk round and ask each pair which variable moves which thing.

The hard part (on purpose)

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.

When students get stuck

Stretch and support

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.

Where it leads

Next lesson: the arrow keys move a paddle. The same update-and-draw loop runs every engine, from MakeCode Arcade to Unity and Godot.

What each step is for

Now you can
Next lesson2. Keys and movementCheck the arrow keys in update() and move a paddle, keep it on the screen, then make a player that moves four ways and speeds up.