Example Code for micro:bit-Whack a Mole

Last revision 2025/12/31

Math Game Application: Whack a Mole.

Hardware Preparation

  • Math & Automatic expansion board for micro:bit, Quantity: 1
  • micro:bit, Quantity: 1
  • Vibration Motor, Quantity: 1
  • Buzzer, Quantity: 1

Software Preparation

Makecode graphical programming, Mind+ platform.
Makecode Library Address.
About how to install a software library?

Wiring Diagram

Vibration Motor and Buzzer are integrated on the board.

Other Preparation Work

Install the Makecode library from https://github.com/DFRobot/pxt-Math-Automatic.
About how to install a software library?

Sample Code

Whack a Mole
Whack a Mole Example Code Download

Result

Touch the DFlogo key, and RGB LEDs flash twice, then the game begins. The RGB LEDs on the keyboard will light up randomly in random color. When the LED turns on, players have to touch the corresponding key within preset time, by which to simulate the process of whack a mole. If users hit the right mole, the viration motor vibrates and the buzzer makes sound effect. Once players missed the moles for 10 times continuously, game over. Every time you hit a mole, you get a point. Your final score will be displayed on the micro:bit LED panel. Touch the DFlogo key again to restart the game.

Was this article helpful?

TOP