Based on the Arduino Nano and SSD1306 OLED display, the Push Box Game includes the following main components:
Online Simulation DisplayExperience project online:
https://www.pcbx.com/community-detail/e94df5f813474829a3b18aa213351689
DescriptionArduino Nano:
- Acts as the game's control unit, processing user inputs and game logic.
SSD1306 OLED Display:
- Used to display the game interface, including the player, boxes, target positions, and boundaries.
Buttons:
- Four buttons are used to control the player's up, down, left, and right movements.
Connection Method:
- The SSD1306 OLED display is connected to the Arduino Nano via the I2C interface.
- The four buttons are connected to the digital pins of the Arduino Nano, and each button requires a pull-up resistor to ensure stable signal reading.
Program Introduction:
The main functions and features of the program are as follows:
Initialization (setup):
- Sets up serial communication.
- Initializes the OLED display and displays the game title.
- Initializes button pins as input pull-up mode.
- Calls the
initGame()
function to set the initial state of the game.
Game Loop (loop):
- The main game loop, which continuously reads button states, updates game status, and redraws the game map as long as the game is ongoing (
gameRunning
istrue
).
Initialize Game (initGame):
- Sets the initial position of the player.
- Randomly generates the positions of the boxes and targets, ensuring they are at least 10 pixels (i.e., two block units) away from the boundaries.
Draw Game Map (drawMap):
- Clears the display and redraws the game map, including boundaries, player, boxes, and target positions.
Read Buttons (readButtons):
- Detects button states, and if a button is pressed, calls the
movePlayer()
function to move the player.
Move Player (movePlayer):
- Updates the player's position based on button inputs.
- If the player moves to the position of a box, attempts to push the box.
- Checks if the game is won.
Check Win Condition (checkWinCondition):
- If all boxes are at the target positions, displays the win message and restarts the game.
Update Game (updateGame):
- Additional game logic, such as scoring or level changes, can be added here.
Precautions:
- Ensure all connections are correct, especially the I2C connection and the pull-up resistors for the buttons.
- Test each component before actual deployment to ensure they work properly.
- The program uses random numbers to initialize the positions of the boxes and targets, which may result in some game configurations being unsolvable. You may need to add additional logic to ensure the generated layout is solvable.
This circuit plan and program provide a basic framework for the Push Box Game, which you can expand and optimize according to your needs.
Join the PCBX community to create your first project and get a $5 coupon.
https://www.pcbx.com/community?mtm_campaign=E&mtm_kwd=hack
Register now to get your first Free PCB&PCBA coupon
Comments
Please log in or sign up to comment.