BBC micro:bit – Whack-a-Mole

BBC-Microbit-Whack-a-MoleWhack-a-mole is a popular arcade game invented in 1976 by Aaron Fechter of Creative Engineering, Inc.. The adaptation of this game to the mirco:bit has been fully designed and implemented by one of our students during a computer science lesson!

The aim of the game is to use a mallet to strike a mole within a certain time. Moles randomly appear for a short time above a hole.

As we will be using the micro:bit, our game will consists of two holes and two moles. The player will strike a mole by pressing the A or the B button on the micro:bit depending on whether the mole appears on the left or the right hand side of the LED screen.

Look at the picture below. The ! represents a mole on the left hand side, The player has less than 1 second to whack this mole by pressing the A button.

BBC-Microbit-Whack-a-Mole-Left

The code


You will need to use the micro:bit website to create the code using the JavaScript Block Editor.

micro-bit-logo

whack-a-mole-BBC-microbit-javascript-code

Code Review


Can you identify which section of code is used to:

  • Randomly display a mole to either the left or the right of the screen?
  • Check, if the left mole is displayed, that the user has pressed button A and if not ends the game?
  • Check, if the right mole is displayed, that the user has pressed button B and if not ends the game?
  • Allow a maximum of 1 second for the user to whack a mole? If the use exceeds this delay, the game should end.

Extension Task #1:


buttons-a-and-b

Tweak this code so that both moles can appear at the same time. In this case the player needs to press both buttons A and B simultaneously to strike both moles otherwise they lose.

BBC-Microbit-Whack-a-Mole-2

When the user strike both moles simultaneously they should get a 5-point bonus added to their score.

score-5-points

Extension Task #2:


Tweak your code so that the time given to the user to “whack a mole” decreases when the score reaches 10 points, 20 points, 30 points etc.

To start with the delay should be set to 1000ms, it should then decrease to 900ms, then 800ms, 700ms, etc…

Tip: Use a variable to store the value of the delay. Initialise this variable to 1000ms.

Did you like this challenge?

Click on a star to rate it!

Average rating 3 / 5. Vote count: 19

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

As you found this challenge interesting...

Follow us on social media!

Tagged with: