← Teacher guide

Functions

Students name a job (making an enemy) as a function, call it from several places, and see in Python that every event block is a function the game calls.

Arcade advanced skills · Many sprites · about 40 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

Timing

Predict and Run 7 min · Investigate 10 min · Modify 8 min · Mix it up 3 min · Make 9 min · Snow globe 3 min

Before class

A function is a named job: some blocks with a name, made once in Functions (under Advanced in the toolbox). Making it does not run it. It runs when a call block calls it, once per call. One copy serves every call, so a change to the function changes every place that calls it. The Python button at the top of the editor shows the same program as text: every event block (on game update every, on overlap, on A pressed) becomes a def, handed to the game, which calls it. MakeCode adds a global bug line inside the function: it means "use the bug variable from outside". Parameters (inputs) are next lesson.

Running the lesson

  1. Predict: some students expect nothing, because the function is not an event. Each call runs it. Count the call blocks together.
  2. Investigate: Ava's empty timer is this lesson's misconception. Then switch the editor to Python together before the match.
  3. Modify: the score goes inside the function, so every call scores, wherever it is called from.
  4. Mix it up: the for element loop, from last lesson.
  5. Make: makeCoin is called four times in the code: three in on start, one in the overlap.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Making a function does nothing until it is called. When nothing appears, ask which block calls the function.

When students get stuck

What you will seeA question to askThe fix
No bugs appear.Which block calls your function?Add a call block (from Functions) inside the timer or on start.
The call block is missing from the toolbox.Have you made the function yet?Make the function first; its call block then appears under Functions.
Only one coin ever comes back.Where is the call to makeCoin in the overlap?Call it after destroy, inside the Player–Food overlap.

Answers to the checks

  1. Predict: on start calls spawnBug three times. What do you see?
    Three bugs flying right. Each call runs the function's blocks once, and each run picks its own random height.
  2. Ava's bugs: Ava made the spawnBug function and an empty timer (below). She runs the game and no bugs ever appear. Why?
    Nothing calls spawnBug: making a function does not run it
    Making a function only names the job. Its blocks run when a call block runs them. Ava's timer is empty, so the job is never done.
  3. How many bugs?: The game runs for 10 seconds. The timer calls spawnBug every 1000 ms. How many times do the blocks inside spawnBug run?
    10
    Once per call, and the timer calls it once a second: about 10 times in 10 seconds.
  4. The same game in Python: This is the game in Python (press Python at the top of the editor). Match each Python line with what it does.
    def spawn_bug(): → D. makes the function spawn_bug; spawn_bug() → A. calls it: runs its blocks once; def on_update_interval(): → B. makes a function from the blocks inside on game update every; game.on_update_interval(1000, on_update_interval) → C. hands that function to the game to call every second
  5. Mix it up: There are 3 bugs on the screen. You press B. How many bugs are destroyed?
    3
    array of sprites of kind Enemy lists all 3, and the loop destroys each one.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › A point per bug

The program as blocks
Show as code
let bug: Sprite = null
function spawnBug () {
    bug = sprites.create(img`
        . . 7 7 . .
        . 7 7 7 7 .
        7 7 f 7 f 7
        . 7 7 7 7 .
        `, SpriteKind.Enemy)
    bug.setPosition(0, randint(10, 110))
    bug.vx = 40
    info.changeScoreBy(1)
}
info.setLife(3)
let player = sprites.create(img`
    . 4 4 .
    4 4 4 4
    . 8 8 .
    . 8 8 .
    `, SpriteKind.Player)
player.setPosition(140, 60)
controller.moveSprite(player)
player.setStayInScreen(true)
game.onUpdateInterval(1000, function () {
    spawnBug()
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    otherSprite.destroy()
    info.changeLifeBy(-1)
})

Modify › An extra bug

The program as blocks
Show as code
let bug: Sprite = null
function spawnBug () {
    bug = sprites.create(img`
        . . 7 7 . .
        . 7 7 7 7 .
        7 7 f 7 f 7
        . 7 7 7 7 .
        `, SpriteKind.Enemy)
    bug.setPosition(0, randint(10, 110))
    bug.vx = 40
    info.changeScoreBy(1)
}
info.setLife(3)
let player = sprites.create(img`
    . 4 4 .
    4 4 4 4
    . 8 8 .
    . 8 8 .
    `, SpriteKind.Player)
player.setPosition(140, 60)
controller.moveSprite(player)
player.setStayInScreen(true)
game.onUpdateInterval(1000, function () {
    spawnBug()
})
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
    spawnBug()
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
    otherSprite.destroy()
    info.changeLifeBy(-1)
})

Make

The program as blocks
Show as code
let coin: Sprite = null
function makeCoin () {
    coin = sprites.create(img`
        . 5 5 .
        5 5 4 5
        5 4 5 5
        . 5 5 .
        `, SpriteKind.Food)
    coin.setPosition(randint(10, 150), randint(10, 110))
}
scene.setBackgroundColor(7)
let player = sprites.create(img`
    . 4 4 .
    4 4 4 4
    . 8 8 .
    . 8 8 .
    `, SpriteKind.Player)
controller.moveSprite(player)
player.setStayInScreen(true)
makeCoin()
makeCoin()
makeCoin()
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, function (sprite, otherSprite) {
    otherSprite.destroy()
    info.changeScoreBy(1)
    makeCoin()
})

Change a snow globe

Open task: any program that does what the step asks.

Stretch and support

Stretch: Make a second function, makeSpike, that adds an Enemy at a random place, and call it every 3 seconds.

Support: Give students the makeCoin function already made, and ask them to add the four calls.

Exit ticket

You make a function, but never call it. What happens when the game runs?

Nothing from that function: its blocks only run when something calls it.

What each step is for

Where it leads

Next lesson: a parameter, so one function can make a slow bug and a fast bug. Skillmap: the Game Maker Guide (arcade.makecode.com/skillmaps) builds bigger games from named jobs like these.