Game Microbit
Beginner
60 mins
Teacher/Student led
145 points
What you need:
Chromebook/Laptop/PC
Microbit

Microbit Magic 8 Ball

Embark on a fun journey to create your own digital Magic 8 Ball using Microbit. You'll start by creating a new project, then program your Microbit to display the number 8. You'll learn to detect a shake gesture, create a variable for random numbers, and assign different messages to each number. Finally, download your code, and enjoy your personalised Magic 8 Ball!
Learning Goals Learning Outcomes Teacher Notes

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 - Create a new Microbit project

    Go to the makecode.microbit.org website and create a new project.

    2 - Show the number 8

    When the Microbit is not being shaken we should show the number 8 (just like on a real magic 8 ball!).

    basic.forever(function () {
        basic.showNumber(8)
    })

    3 - Detect a shake

    Add the following block of code to detect when the Microbit is shaken.

    input.onGesture(Gesture.Shake, function () {
    	
    })


    4 - Create a variable called 'Random Number'

    You're going to create a new variable called Random Number and set it to a random number (obviously!) between 0 and 4 when we shake the Microbit.

    let Random_Number = 0
    input.onGesture(Gesture.Shake, function () {
        Random_Number = randint(0, 4)
    })

    Each random number corresponds to a particular response from the magic 8 ball. This means we can have five responses if our variable is between 0 and 4.

    Notice how we begin at 0 (not 1). In computer programming, we almost always start counting from 0.

    5 - Check the value of the variable

    Now we need to check what number is stored in our variable after the Microbit was shaken (remember, it can only be one number at a time between 0 and 4).

    let Random_Number = 0
    input.onGesture(Gesture.Shake, function () {
        Random_Number = randint(0, 4)
        if (Random_Number == 0) {
        } else if (Random_Number == 1) {
        }
    })

    In the code above, I've given you the first two condition blocks. Can you add the rest in yourself for the other numbers?

    see the code

    let Random_Number = 0
    input.onGesture(Gesture.Shake, function () {
        Random_Number = randint(0, 4)
        if (Random_Number == 0) {
        } else if (Random_Number == 1) {
        } else if (Random_Number == 2) {
        } else if (Random_Number == 3) {
        } else {
        }
    })

    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