UNIHIKER K10 Lesson 2: Design a Whack-a-Mole Arcade Game with Graphical Coding
Ready to smash moles with a tilt of your board? In this UNIHIKER K10 lesson, you’ll code a tactile Whack-a-Mole arcade game using its built-in accelerometer and graphical coding software Mind+. Design variables to randomize mole appearances, master logical operators for hit detection, and program real-time feedback: green LEDs + victory chimes for hits, red lights + buzzer misses. This CSTA/DigComp-aligned STEM lab transforms physics (tilt angles!) into computational logic—no extra hardware, just pure coding fun.

Objectives
Create a game scene on the UNIHIKER K10 and use the accelerometer to determine if you can hit the mole.
If we hit the mole, then we light up the green light and play the F4 tone. If we do not hit the mole, then we light up the red light and play the A3 tone.

Knowledge Goals:
1.Understand the concept of variables in the program.
2.Master the use of the UNIHIKER K10 onboard accelerometer.
3.Learn how to set up light and sound effects on the UNIHIKER K10.
4.Master the use of logical operators.
Materials List:
Hardware:

Basic Mind+ Software Usage
1. Open Mind+ and switch to offline mode.

2. Load UNIHIKER K10
Based on the previous steps, then click on "Extensions" find the "UNIHIKER K10" module under the "Board" and click to add it. After clicking "Back" you can find the UNIHIKER K10 in the "Command Area" and complete the loading of UNIHIKER K10.

3. Connect UNIHIKER K10
First of all, you need to use a USB cable to connect the UNIHIKER K10 to the computer.

Then, after clicking Connect Device, click COM7-UNIHIKER K10 to connect.

Note: The device name of different UNIHIKER K10 may vary, but all end with K10.
In Windows 10/11, the UNIHIKER K10 is driver-free. However, for Windows 7, manual driver installation is required: https://www.unihiker.com/wiki/K10/faq/#high-frequency-problem
Hands-on Practice
Next, let's get hands-on and complete today's course tasks together. We will start by creating the "Whack-a-Mole" game scene and gradually learn how to use the onboard accelerometer on the UNIHIKER K10 to implement the game functionality.
Task 1: Create the Game Scene
In this part, we will create the initial game scene and make the moles randomly appear from four holes.
Task 2: Determine if the Mole is Hit
In this part, we will mainly focus on using the onboard accelerometer to determine whether the mole has been hit.
Task 3: Game Feedback Effect Setup
In this part, we will mainly set up the feedback effects for whether the mole has been hit.
Task 1: Create the Game Scene

1. Hardware Setup
Ensure that the UNIHIKER K10 is connected to the computer using a USB cable.
2. Software Preparation
Make sure that Mind+ is opened and the UNIHIKER board has been successfully loaded. Once confirmed, you can proceed to write the project program.

3. Write the Program
STEP 1: Use the “cache local image” command to set up the initial game scene. The relevant settings are as follows.

STEP 2: Set the mole to randomly appear at one of the four holes.
In this game, the mole will randomly pop up from one of the four holes, with each scene corresponding to an image. To make the mole appear randomly from a hole every two seconds, we will use the “pick random () to ()” command, and use a variable called "position" to store the generated random number. The relevant program settings are as follows.

Create a new variable called "position" and assign the random number to the variable as follows:

Note: For more information about “variables”, refer to the "Knowledge Hub."
STEP 3: Set the mole image to appear at the corresponding position.
Earlier, we stored the random number for the current game round in the "position" variable. Now, we need to use the “if () then ()”, “if () then () else” command to set the corresponding mole image based on the different random numbers. The relevant program code is as follows.

To keep the main program clear, we encapsulate the code for each function into a "function". In the main program, we only need to call the corresponding function to achieve the desired functionality. The function definition operation is as follows.

The complete code for the game scene setup module is shown in the following image:

4. Run the Program
STEP 1: Check and ensure that the UNIHIKER K10 is connected to Mind+

STEP 2: Click the "Upload" button at the top right of the interface.

STEP 3: Observe the display effect on the UNIHIKER K10 screen (refer to the image below).

Task 2: Determine if the Mole is Hit
In this game, we detect the UNIHIKER K10's tilt posture using the onboard accelerometer. If the mole appears at the front, the mole is considered "hit" only when the UNIHIKER K10 detects a "tilt forward" posture.
1. Write the Program
STEP 1: Determine whether the mole is hit.
First, use the “is Forward tilt ?” command to determine the tilt state of the UNIHIKER K10. Then, use the “If () Then ()”, “If () Then () else” command to check whether the tilt direction matches the mole's position. If they match, assign the variable "hit" the value of "1," indicating the mole has been hit. Note that we must initially set "hit" to "0" to ensure that it changes to "1" only when the mole is successfully hit.

Note: For more information about the accelerometer, refer to the "Knowledge Hub."
To check whether the tilt direction matches the mole's position using the “if () then ()”, “if () then () else” command, we need to use "logical and operations." Only when both conditions are met is the mole considered hit. The tilt directions of the UNIHIKER K10 and the specific program operations are shown below.


Note: For more details about logical and operations, refer to the "Knowledge Hub."
STEP 2: Set the game time.
To require players to complete the "Whack-a-Mole" task within a given time of 2 seconds, we need to set a variable "time" to record the allowed time. That is, calculate the elapsed time by subtracting the initial system runtime from the current system runtime. If this time exceeds 2 seconds, the game is considered failed, and the mole is not hit. The relevant program is as follows:

The complete program code for determining if Mole is Hit module is as follows:

2. Run the Program
STEP 1: Connect the K10.
STEP 2: Click the "Upload" button in the top-right corner of the interface.
STEP 3: Play the game and observe the effect on the screen when the tilt direction matches the mole's position (as shown in the image below).

Task 3: Game Feedback Effect Setup
In this task, we need to set up different lighting and sound effects for the variable "hit," based on the previous judgment.
1. Write the Program
STEP 1: Set up the lighting and sound effects.
For determining whether the mole is hit, we set two different feedback effects. If the mole is hit, we light up a green light and “Middle F/F4 for 3/4 beat”. If the mole is not hit, we light up a red light and “Low A/A3 for 1 beat”.

STEP 2: Determine Execution
After the previous judgment, if the mole is hit, the variable "hit" will be set to 1; if not hit, the variable "hit" will be 0. We will use the “if () then ()” command to perform different actions based on the value.

Write the program code for both scenarios into two functions: "Hit Effect" and "Miss Effect". The complete program code is shown below:

2. Run the Program
Click the "Upload" button and try out your custom "Whack-a-Mole" game!
Knowledge Hub
1. Variables
To make it easier to understand, we can think of a variable as a box. Assigning a value to a variable is like putting something into that box. Variables can be assigned new values repeatedly, meaning the contents of the box can be taken out and replaced with something else.

For example, this program repeatedly assigns a value to the "flag" variable. The first assignment places the number 0 into the empty box of "flag," so the value of flag is 0. In the second assignment, the number 0 is taken out, and the number 1 is placed in, so the value of flag becomes 1.

Note: When creating a new variable, it need be named, and the naming must follow specific rules:
(1)The name can consist of numbers, letters, and underscores.
(2)The name cannot use Python's reserved keywords, which are words that already have special meanings in Python, such as True, False, def, if, elif, else, import, etc.
(3)The name cannot contain special characters (`~!@#$%^&*()+?:../;'[]).
(4)It is best to choose a name that reflects its meaning.
2. Logical Operations
Logical operations are used to perform calculations on logical values, which only have two possible outcomes: true or false. There are three logical operators: AND, OR, and NOT, with the corresponding program modules shown below:

Logical Operator AND
The result is true only when both operands are true; otherwise, it is false. For example, when Y = 180, and Y > 150 and Y < 200, the entire logical expression evaluates to true. The "AND" operator is similar to the "and" in daily language.

Logical Operator OR
The result is true if at least one of the operands is true. If both operands are false, the result is false. The logical operator "OR" is similar to the "or" in everyday language.
Logical Operator NOT
This operator works on a single operand. If the operand is true, the result is false. If the operand is false, the result is true.
3. UNIHIKER K10 Built-in Accelerometer
The UNIHIKER K10 has a built-in accelerometer that can detect the posture of the UNIHIKER K10 and read values from the onboard accelerometer (in the X, Y, Z directions or intensity).
The following is an introduction to the commands related to the accelerometer:


Want to explore more sensor-driven projects? The UNIHIKER K10’s accelerometer is just the beginning—imagine coding tilt-controlled maze games, gesture-activated music instruments, or even earthquake simulators.
[Get UNIHIKER K10 here]
Want to keep learning? Continue your learning journey with our other lessons.
Lesson1: Build a Light-Responsive Virtual Pet Dog
Lesson3: Build Portable Walkman-Style Music Player (KS3/CSTA/DigComp)
