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.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run, with making the microStudio project 15 min · Investigate 10 min · Modify 15 min · Make 15 min
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.
| What you will see | A question to ask | The fix |
|---|---|---|
| Nothing happens when they press Run, or the code turns red. | Is the project set to Python? | In the project's Settings, set the language to Python, then Run again. |
| cannot access local variable 'x' where it is not associated with a value. | Which function changes x, and does it say global x? | Add global x as the first line of that function. |
| The dot is drawn but never moves. | Is x changed in update(), or only in init()? | Add x += 1 inside update(). |
| IndentationError, or code that is half outside a function. | Did the whole file get replaced, or was the code pasted under microStudio's own example? | Select all in the file main, delete it, then paste. |
One possible solution for each task. Other correct programs are fine.
def init():
global y
y = -100
def update():
global y
y += 1
if y > 100:
y = -100
def draw():
screen.clear("rgb(10,10,40)")
screen.fillRound(0, y, 10, 10, "rgb(255,210,0)")
screen.drawText("y = " + str(y), 0, 80, 10, "#FFF")
def init():
global x, dx
x = 0
dx = 1
def update():
global x, dx
x += dx
if x > 100 or x < -100:
dx = -dx
def draw():
screen.clear("rgb(10,10,40)")
screen.fillRound(x, 0, 10, 10, "rgb(255,210,0)")
def init():
global t
t = 0
def update():
global t
t += 1
def draw():
screen.clear("rgb(10,10,40)")
screen.fillRound(t % 200 - 100, 40, 12, 12, "rgb(255,210,0)")
screen.fillRound(100 - t % 200, -40, 12, 12, "rgb(255,100,120)")
screen.drawText("Frames: " + str(t), 0, 80, 10, "#FFF")
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.
Why does draw() start with screen.clear(...)?
draw() runs 60 times a second; without clearing, every old picture of the dot would stay, making a smear.
Next lesson: the arrow keys move a paddle. The same update-and-draw loop runs every engine, from MakeCode Arcade to Unity and Godot.