PAC-MAN is a classic arcade game released by Namco in 1980. The game features a yellow, circular character named Pac-Man who navigates a maze, eating small dots while avoiding four ghosts—Blinky, Pinky, Inky, and Clyde. The goal is to eat all the dots in the maze while avoiding being caught by the ghosts. Pac-Man can also eat large flashing dots called "power pellets," which temporarily make the ghosts vulnerable, allowing Pac-Man to eat them for extra points. The game continues with increasing difficulty as levels progress.
The main goal of this Arduino-based PAC-MAN game is to replicate the core mechanics of the original PAC-MAN arcade game while adapting it to work with an Arduino UNO, an LCD screen (I2C or graphical OLED), a joystick, and a buzzer. The player controls Pac-Man, navigating a maze, collecting dots, avoiding ghosts, and using power pellets to gain temporary advantages.
A. Navigate the Maze
The game consists of a predefined maze displayed on an LCD (16x2) or an OLED (128x64) screen.
Pac-Man moves within the maze using the joystick module.
The maze contains:
Walls (#
) that Pac-Man cannot pass through.
Dots (.
) or small pellets that Pac-Man must collect to score points.
Power Pellets (O
) that make ghosts vulnerable.
Ghosts (G
) that move randomly or chase Pac-Man.
B. Collect All Dots to Win
The player’s goal is to eat all the dots (.
) in the maze.
Once all dots are eaten, the game displays a "You Win!" message, and a buzzer plays a victory tune.
C. Avoid the Ghosts
Ghosts (G) move randomly or follow Pac-Man.
If Pac-Man collides with a ghost, the game ends with a Game Over message and sound.
Arduino UNO (Microcontroller)
The Arduino UNO acts as the brain of the PAC-MAN game. It processes inputs from the joystick, updates the game state, manages collisions, and controls the LCD screen and buzzer.
LCD I2C Screen
The display shows the PAC-MAN maze, characters (Pac-Man & ghosts), and score.
Joystick Module (Player Controls)
The joystick allows the player to control Pac-Man's movement in four directions: Up, Down, Left, and Right.
Buzzer (Sound Effects)
The buzzer generates sound effects for key game actions such as eating dots, power pellets, ghost interactions, and game over.
Jumper Wires
Jumper wires will be used to make connections between the components.
Breadboard:
A breadboard can be used to create a temporary circuit for testing and prototyping.
Connect VCC to 5V on Arduino.
Connect GND to GND on Arduino.
Connect X-axis to A0 and Y-axis to A1 on Arduino.
Connect the joystick button to a digital pin (e.g., D2).
Connect SDA to A4, SCL to A5, VCC to 5V, and GND to GND.
Connect the positive pin to a digital output pin (e.g., D3) on Arduino.
Connect the negative pin to GND.
This code initializes the maze, controls Pac-Man's movement, implements a score system, and plays buzzer sounds.
import this library : LiquidCrystal_I2C for I2C LCD screen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#include <Wire.h> #include <LiquidCrystal_I2C.h> // LCD Setup LiquidCrystal_I2C lcd(0x27, 16, 2); // Joystick Pins #define VRX A0 #define VRY A1 #define BUTTON 2 // Buzzer Pin #define BUZZER 8 // Maze dimensions const int ROWS = 2; const int COLS = 16; // Game variables char maze[ROWS][COLS] = { "P . . . . . G .", // Pac-Man starts at (0,0) "# . . . . . . ." }; int pacmanX = 0, pacmanY = 0; // Pac-Man position int score = 0; bool gameOver = false; // Function to display maze void displayMaze() { lcd.clear(); for (int i = 0; i < ROWS; i++) { lcd.setCursor(0, i); lcd.print(maze[i]); } } // Read joystick input void movePacman() { int xValue = analogRead(VRX); int yValue = analogRead(VRY); int newX = pacmanX, newY = pacmanY; if (xValue < 300) newY--; // Left else if (xValue > 700) newY++; // Right if (yValue < 300) newX--; // Up else if (yValue > 700) newX++; // Down // Ensure movement is within bounds if (newX >= 0 && newX < ROWS && newY >= 0 && newY < COLS && maze[newX][newY] != '#') { // Check for dot if (maze[newX][newY] == '.') { score += 10; tone(BUZZER, 1000, 100); // Play sound when eating dots } // Check for ghost else if (maze[newX][newY] == 'G') { gameOver = true; tone(BUZZER, 500, 500); lcd.clear(); lcd.setCursor(4, 0); lcd.print("GAME OVER!"); return; } // Update Pac-Man's position maze[pacmanX][pacmanY] = ' '; pacmanX = newX; pacmanY = newY; maze[pacmanX][pacmanY] = 'P'; displayMaze(); } } // Ghost movement (random) void moveGhost() { int ghostX = 0, ghostY = 7; // Ghost initial position int direction = random(4); // Random movement (0: Up, 1: Down, 2: Left, 3: Right) int newGhostX = ghostX, newGhostY = ghostY; switch (direction) { case 0: newGhostX--; break; // Up case 1: newGhostX++; break; // Down case 2: newGhostY--; break; // Left case 3: newGhostY++; break; // Right } // Ensure movement is within bounds if (newGhostX >= 0 && newGhostX < ROWS && newGhostY >= 0 && newGhostY < COLS && maze[newGhostX][newGhostY] != '#') { maze[ghostX][ghostY] = '.'; ghostX = newGhostX; ghostY = newGhostY; maze[ghostX][ghostY] = 'G'; } } // Display score void displayScore() { lcd.setCursor(0, 1); lcd.print("Score: "); lcd.print(score); } void setup() { lcd.begin(); pinMode(BUZZER, OUTPUT); pinMode(VRX, INPUT); pinMode(VRY, INPUT); pinMode(BUTTON, INPUT_PULLUP); displayMaze(); } void loop() { if (!gameOver) { movePacman(); moveGhost(); displayScore(); delay(500); } } |
How the Code Works
✔ Pac-Man Moves
Reads joystick values (VRX
, VRY
).
Updates Pac-Man’s position on the LCD.
✔ Dots (.
) Eating & Scoring
If Pac-Man moves to a dot position (.
), the score increases by 10.
A buzzer beep plays each time Pac-Man eats a dot.
✔ Ghost (G
) Movement
The ghost moves randomly within the maze.
If Pac-Man touches the ghost, Game Over message appears, and a "lose" sound plays.
✔ Game Over Condition
If Pac-Man collides with the ghost, the game stops, and "GAME OVER" is displayed.
Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info