Maze Game
ProjectThe Maze game has been upgraded to provide a more engaging and challenging gaming experience. The game now includes four different levels, each featuring a uniquely designed map with progressively increasing difficulty. The main objective is to roll a ball from the starting point to the exit while staying within the specified time limit.
As players successfully complete a level, the next, more challenging level is unlocked. The game continues until all four levels are conquered or the player fails.
To enhance engagement, a scoring system tracks player performance based on completion time.
Players earn:
- 2 points for completing a level in under one minute.
- 1 point for completing a level in over one minute.
If a player fails a level by colliding with walls, no points are awarded for that attempt. This system adds an element of strategy, rewarding both speed and precision.
Libraries & Tools
- Hardware: STM32F4 Nucleo-F401RE, LCD 128064b, MPU6050.
- Software: STM32CubeIDE
- Programming Languages: Embedded C
Structure
The interaction structure between microcontroller and sensors
Progrmaming Modularization
Overview of Maze game
- ball.h / ball.c
This file contains several functions for creating and manipulating a ball on the LCD screen. For instance, the LCD_draw_dot()
function lights up or turns off a pixel. It also includes a function to create a ball of a specified size and a function to clear all the positions of the ball. Additionally, there are eight functions to control the ball’s movement direction.
- connect.h / connect.c
This file connects the ball-related functions with other parts of the program. The main function, tilting()
, analyzes the movement of the ball and is called within the app_loop()
function. Another function initializes the ball’s position when it is stationary, and the accelerometer is not detecting movement.
- delay.h / delay.c
This file implements a delay function. Since the HAL_DELAY
function only provides millisecond delays, a microsecond delay is necessary to initialize the LCD device. The delay_us()
function is created to provide this finer resolution of delay.
- gameLogic.h / gameLogic.c
This file contains the functions that manage the overall game logic. When the game starts, and the ball begins to rotate, it interacts with walls or reaches the goal area. Based on these interactions, the score is calculated according to the time taken. The file also handles the scene transitions, such as changing from one map to another or displaying the score. These conditions are set up in advance and then called when necessary.
- lcd.h / lcd.c
This file contains several functions for activating and controlling the LCD device. It handles the necessary pin settings and specific registers required for LCD operation. For example, to write data to the LCD and display it on the screen, certain pins must be configured to specific values.
- maps.h / maps.c
This file is responsible for creating the game maps. The walls and exits of the maps are predefined with fixed coordinates. Functions in this file display the walls and exits on the LCD screen. The main goal is to generate the maps as intended and store the coordinates of the walls and exits in arrays.
- mpu6050.h / mpu6050.c
This file is crucial for using the MPU6050 accelerometer to detect the rotation of the device and control the tilting of the ball. It analyzes the accelerometer’s data, calculates the tilting angle along the three axes, and displays the measured values on the laptop to determine the ball’s direction.
- procedure.h / procedure.c
This file manages the game’s start and overall execution, checking conditions such as goal and wall interactions. It oversees the entire game process, determining how the game should proceed and what setup is required.
- score.h / score.c
This file manages the player’s score throughout the game. Regardless of the outcome, the score reflects the player’s effort. It contains arrays for score signs and functions to create various score displays.
Algorithmic description
When the MPU6050 sensor is tilted, the acceleration due to gravity (1 g) acts vertically downward. By measuring the sensor’s accelerometer data along the x, y, and z axes, the roll (rotation around the x-axis) and pitch (rotation around the y-axis) angles can be calculated by using arctangent function.
Acc_x = arctan( x / √(y² + z²) )
Acc_y = arctan( y / √(x² + z²) )