BBC micro:bit – Magic 8 Ball

For this project we are going to code our micro:bit to act as a magic 8 ball:

The user will think of a question such as:

• Will it be snowing tomorrow?
• Will I be a rock star one day?
• Will my program work?

Then they will shake the micro-bit. The micro-bit will than randomly pick one of the following four answers:

• No way!
• Probably!
• Unlikely!
• Definitely!

Have a go at completing the task by yourself and if you get stuck or don’t know how to get started check our solution below.

Access BBC micro:bit website:

Solution

Tweak the code to add more possible answers such as:

• I don’t think so
• Maybe
• No doubt about it
• Not sure
[pms-restrict display_to=”not_logged_in” message=” “]

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

Solution...

You are viewing this solution as part of your full membership subscription!

Solution
You can copy/import the following JavaScript code in the BBC micro:bit code editor and convert it to blocks if needed.

Javascript Code

```input.onGesture(Gesture.Shake, function () {
answer = randint(0, 7)
if (answer == 0) {
basic.showString("No way!")
} else if (answer == 1) {
basic.showString("Probably!")
} else if (answer == 2) {
basic.showString("Unlikely!")
} else if (answer == 3) {
basic.showString("Definitely!")
} else if (answer == 4) {
basic.showString("I don't think so!")
} else if (answer == 5) {
basic.showString("Maybe!")
} else if (answer == 6) {
basic.showString("No doubt about it!")
} else if (answer == 7) {
basic.showString("Not sure!")
}
})
let answer = 0
basic.showString("Ask a question and shake me!")```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 3.2 / 5. Vote count: 41

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: