This is a simple Pac-Man game running on the Arduino platform, using an OLED display to show the game interface. The goal of the game is to control Pac-Man to move through the maze, eat all the beans, while avoiding hitting the walls.
Initialization and Setup:
- Defined the width and height of the OLED display, as well as a macro OLED_RESET for resetting the OLED.
- Created an Adafruit_SSD1306 object display to control the OLED display.
- Defined the size of Pac-Man PACMAN_SIZE, and the initial positions of Pac-Man and the bean, pacmanX, pacmanY, beanX, beanY.
- Defined a two-dimensional array grid to represent the layout of the maze, where 1 represents walls and 0 represents paths where beans can be eaten.
- Initialized the direction of Pac-Man direction and the score score.
setup() Function:
- Initializes serial communication.
- Starts the OLED display and shows the game title, then clears the display.
- Sets the pins used to control Pac-Man's movement to input pull-up mode.
resetBean() Function:
- Randomly generates the position of the bean, ensuring it is not generated on a wall.
readButton() Function:
- Reads the button state and returns the new direction of Pac-Man.
loop() Function:
- This is the main loop of Arduino, where the game logic is continuously executed.
- Reads button input and updates the direction of Pac-Man.
- Updates Pac-Man's position based on the new direction, but only if the new position is not a wall.
- If Pac-Man encounters a bean, increases the score and regenerates the position of the bean.
- Clears the OLED display and redraws the maze, Pac-Man, and the bean.
- Displays the current score.
- There is a brief delay after each loop to control the speed of the game.
Drawing Logic:
- Uses display.drawRect() to draw walls.
- Uses display.fillRect() to draw Pac-Man.
- Uses display.fillCircle() to draw the bean.
- Displays the score.
Game Loop:
- The game will continue to loop until the user stops the program or the Arduino device is powered off.
Comments
Please log in or sign up to comment.