Game Microbit Inventor Project
Easy
40 mins
145 points
What you need:
  • Computer/laptop
  • Microbit
  • USB Cable

Microbit Magic 8 Ball

In this project we're going to create a Magic 8 Ball with our Microbit. When we shake the Microbit we can ask it a question and it should give us one of many responses to our question.

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 {
    }
})

Join our club 😃

To view the remaining 3 steps and access hundreds of other coding projects please login or create an account.

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