Game Makecode Arcade
Intermediate
60 mins
Teacher/Student led
+85 XP
What you need:
Chromebook/Laptop/PC or iPad/Tablet
Arcade computer

Donut Rush

In this lesson, you're going to learn how to create a fun, interactive game using MakeCode Arcade. The game is called 'Donut Rush', where the player has to collect as many donuts as possible within a given time limit. You'll be writing code to create the game sprites, handle events like sprite overlaps, and control the game logic.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Creating a new project

    Start by creating a new project in MakeCode Arcade. To do this, go to the MakeCode Arcade website and click on 'New Project'.

    2 - Setting up the Game

    In this step, we'll set up the game. We'll create a splash screen, initialize some variables, and create our player sprite. The splash screen is the first screen that appears when the game starts. It's a great place to display the game's title or any instructions for the player.

    The variables we're creating will be used to keep track of the game's state:

    • The 'level' variable will keep track of the current level.
    • The 'target' will store the number of donuts to be collected in the current level.
    • And the 'collected' will keep track of the number of donuts the player has collected so far.
    • The 'mySprite' variable will be used to store our player sprite, which is the character that the player will control in the game.

    Add the following code:

    let mySprite: Sprite = null
    let collected = 0
    let target = 0
    let level = 0
    game.splash("Collect the donuts!")
    level = 1
    target = 0
    collected = 0
    mySprite = sprites.create(img`
        . . . . . . 5 . 5 . . . . . . . 
        . . . . . f 5 5 5 f f . . . . . 
        . . . . f 1 5 2 5 1 6 f . . . . 
        . . . f 1 6 6 6 6 6 1 6 f . . . 
        . . . f 6 6 f f f f 6 1 f . . . 
        . . . f 6 f f d d f f 6 f . . . 
        . . f 6 f d f d d f d f 6 f . . 
        . . f 6 f d 3 d d 3 d f 6 f . . 
        . . f 6 6 f d d d d f 6 6 f . . 
        . f 6 6 f 3 f f f f 3 f 6 6 f . 
        . . f f d 3 5 3 3 5 3 d f f . . 
        . . f d d f 3 5 5 3 f d d f . . 
        . . . f f 3 3 3 3 3 3 f f . . . 
        . . . f 3 3 5 3 3 5 3 3 f . . . 
        . . . f f f f f f f f f f . . . 
        . . . . . f f . . f f . . . . . 
        `, SpriteKind.Player)
    controller.moveSprite(mySprite, 70, 70)
    

    3 - Creating the Start Level Function

    A function in programming is a reusable piece of code. It allows you to group several commands together, which can then be called upon whenever you need them. This helps to make your code more organized and efficient.

    In this step, we're creating a function called 'startLevel'. This function will set up each level of our game. Here's what it does:

    1. It sets the variable 'collected' to 0. This variable keeps track of how many donuts the player has collected.
    2. It randomly sets the background color of the game scene.
    3. It displays a message saying 'Level ' followed by the current level number.
    4. It sets the target number of donuts to collect for the level.
    5. It starts a countdown of 10 seconds for the level.

    Add the following code to create the 'startLevel' function:

    let mySprite: Sprite = null
    
    function startLevel () {
        collected = 0
        scene.setBackgroundColor(randint(3, 7))
        mySprite.sayText("Level " + level, 1000, false)
        target = 10 + level
        info.startCountdown(10)
    }
    

    4 - Create the Donuts

    In this step, we will create the donuts that the player has to collect. We will use a loop to create multiple donuts and place them randomly on the screen.

    The loop starts at 0 and runs until it reaches the target number of donuts. For each iteration of the loop, we create a new donut sprite and set its position to a random location on the screen.

    Add the loop code to your function:

    let mySprite: Sprite = null
    let donut: Sprite = null
    function startLevel () {
        collected = 0
        scene.setBackgroundColor(randint(3, 7))
        mySprite.sayText("Level " + level, 1000, false)
        target = 10 + level
        info.startCountdown(10)
        for (let index = 0; index <= (target - 1); index++) {
            donut = sprites.create(img`
                . . . . . . b b b b a a . . . . 
                . . . . b b d d d 3 3 3 a a . . 
                . . . b d d d 3 3 3 3 3 3 a a . 
                . . b d d 3 3 3 3 3 3 3 3 3 a . 
                . b 3 d 3 3 3 3 3 b 3 3 3 3 a b 
                . b 3 3 3 3 3 a a 3 3 3 3 3 a b 
                b 3 3 3 3 3 a a 3 3 3 3 d a 4 b 
                b 3 3 3 3 b a 3 3 3 3 3 d a 4 b 
                b 3 3 3 3 3 3 3 3 3 3 d a 4 4 e 
                a 3 3 3 3 3 3 3 3 3 d a 4 4 4 e 
                a 3 3 3 3 3 3 3 d d a 4 4 4 e . 
                a a 3 3 3 d d d a a 4 4 4 e e . 
                . e a a a a a a 4 4 4 4 e e . . 
                . . e e b b 4 4 4 4 b e e . . . 
                . . . e e e e e e e e . . . . . 
                . . . . . . . . . . . . . . . . 
                `, SpriteKind.Food)
            donut.setPosition(randint(20, 140), randint(20, 100))
        }
    }
    

    5 - Starting the Game

    Now, we need to call our 'startLevel' function to start the game. Add the following code to the end of 'on start':

    let mySprite: Sprite = null
    let collected = 0
    let target = 0
    let level = 0
    game.splash("Collect the donuts!")
    level = 1
    target = 0
    collected = 0
    mySprite = sprites.create(img`
        . . . . . . 5 . 5 . . . . . . . 
        . . . . . f 5 5 5 f f . . . . . 
        . . . . f 1 5 2 5 1 6 f . . . . 
        . . . f 1 6 6 6 6 6 1 6 f . . . 
        . . . f 6 6 f f f f 6 1 f . . . 
        . . . f 6 f f d d f f 6 f . . . 
        . . f 6 f d f d d f d f 6 f . . 
        . . f 6 f d 3 d d 3 d f 6 f . . 
        . . f 6 6 f d d d d f 6 6 f . . 
        . f 6 6 f 3 f f f f 3 f 6 6 f . 
        . . f f d 3 5 3 3 5 3 d f f . . 
        . . f d d f 3 5 5 3 f d d f . . 
        . . . f f 3 3 3 3 3 3 f f . . . 
        . . . f 3 3 5 3 3 5 3 3 f . . . 
        . . . f f f f f f f f f f . . . 
        . . . . . f f . . f f . . . . . 
        `, SpriteKind.Player)
    controller.moveSprite(mySprite, 70, 70)
    startLevel()
    
    function startLevel () {}
    

    To play the game, click the green 'Play' button in the simulator.


    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more