// ===================== PACMAN for ESP32 + ILI9341 (Wokwi) =====================
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// ---- Pins ----
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define JOY_X 35 // HORZ
#define JOY_Y 34 // VERT
#define JOY_SW 32 // SEL (stick press)
// Flip these if a direction feels reversed in the simulator
#define INVERT_X true
#define INVERT_Y true
#define DEADZONE 900
#define GAME_SPEED 8 // 1 = slowest ... 10 = fastest
#define PAC_MS (250 - GAME_SPEED * 20)
#define GHOST_MS (PAC_MS * 5 / 4)
#define SCARED_MS (PAC_MS * 9 / 5)
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
// ---- Colors (RGB565) ----
#define C_BLACK 0x0000
#define C_BLUE 0x001F
#define C_NAVY 0x0008
#define C_YELLOW 0xFFE0
#define C_WHITE 0xFFFF
#define C_RED 0xF800
#define C_PINK 0xFBB5
#define C_CYAN 0x07FF
#define C_ORANGE 0xFD20
// ---- Layout ----
const int COLS = 21, ROWS = 21, T = 11, OX = 4, OY = 30;
// ---- Maze: # wall, . dot, o power pellet, - ghost door ----
const char* LEVEL[ROWS] = {
"#####################",
"#o........#........o#",
"#.###.###.#.###.###.#",
"#...................#",
"#.###.#.#####.#.###.#",
"#.....#...#...#.....#",
"#####.###.#.###.#####",
" #.#.......#.# ",
"#####.#.##-##.#.#####",
" ...# #... ",
"#####.#.#####.#.#####",
" #.#.......#.# ",
"#####.#.#####.#.#####",
"#.....#...#...#.....#",
"#.###.###.#.###.###.#",
"#o..#...........#..o#",
"#.###.#.#####.#.###.#",
"#.....#...#...#.....#",
"#.###.###.#.###.###.#",
"#o.................o#",
"#####################"
};
char maze[ROWS][COLS + 1];
// ---- Directions: 0=UP 1=DOWN 2=LEFT 3=RIGHT ----
const int DR[4] = { -1, 1, 0, 0 };
const int DC[4] = { 0, 0, -1, 1 };
// ---- Game state ----
enum State { TITLE, PLAYING, DEAD_OVER, WON };
State state;
int score, lives, dotsLeft;
int pr, pc, pdir, wantDir;
bool mouth;
unsigned long lastPac, lastGhost, scaredUntil, stateSince;
struct Ghost {
int r, c, dir;
uint16_t color;
int aggr; // % chance to chase Pacman instead of random
bool inHouse, scared;
unsigned long releaseAt;
};
Ghost g[3];
const uint16_t GCOL[3] = { C_RED, C_PINK, C_CYAN };
const int GAGGR[3] = { 85, 60, 40 };
// ---- Helpers ----
inline int px(int c) { return OX + c * T; }
inline int py(int r) { return OY + r * T; }
bool canGo(int r, int c, int d, bool allowDoor) {
int nr = r + DR[d];
int nc = (c + DC[d] + COLS) % COLS; // wraps for the tunnel
if (nr < 0 || nr >= ROWS) return false;
char ch = maze[nr][nc];
if (ch == '#') return false;
if (ch == '-' && !allowDoor) return false;
return true;
}
void stepPos(int &r, int &c, int d) {
r += DR[d];
c = (c + DC[d] + COLS) % COLS;
}
// ---- Drawing ----
void drawTile(int r, int c) {
int x = px(c), y = py(r);
char ch = maze[r][c];
tft.fillRect(x, y, T, T, C_BLACK);
if (ch == '#') {
tft.fillRect(x, y, T, T, C_BLUE);
tft.fillRect(x + 2, y + 2, T - 4, T - 4, C_NAVY);
} else if (ch == '.') {
tft.fillCircle(x + T / 2, y + T / 2, 1, C_WHITE);
} else if (ch == 'o') {
tft.fillCircle(x + T / 2, y + T / 2, 3, C_WHITE);
} else if (ch == '-') {
tft.fillRect(x, y + T / 2 - 1, T, 2, C_PINK);
}
}
void drawMaze() {
for (int r = 0; r < ROWS; r++)
for (int c = 0; c < COLS; c++) drawTile(r, c);
}
void drawPac() {
int cx = px(pc) + T / 2, cy = py(pr) + T / 2;
tft.fillCircle(cx, cy, 5, C_YELLOW);
if (mouth) {
switch (pdir) {
case 0: tft.fillTriangle(cx, cy, cx - 5, cy - 6, cx + 5, cy - 6, C_BLACK); break;
case 1: tft.fillTriangle(cx, cy, cx - 5, cy + 6, cx + 5, cy + 6, C_BLACK); break;
case 2: tft.fillTriangle(cx, cy, cx - 6, cy - 5, cx - 6, cy + 5, C_BLACK); break;
default: tft.fillTriangle(cx, cy, cx + 6, cy - 5, cx + 6, cy + 5, C_BLACK); break;
}
}
}
void drawGhost(Ghost &G) {
int cx = px(G.c) + T / 2, cy = py(G.r) + T / 2;
uint16_t col = G.color;
if (G.scared) {
bool flash = (scaredUntil - millis() < 2000) && ((millis() / 200) % 2);
col = flash ? C_WHITE : C_BLUE;
}
tft.fillCircle(cx, cy - 1, 5, col);
tft.fillRect(cx - 5, cy - 1, 11, 6, col);
// wavy bottom
tft.drawPixel(cx - 3, cy + 5, C_BLACK);
tft.drawPixel(cx, cy + 5, C_BLACK);
tft.drawPixel(cx + 3, cy + 5, C_BLACK);
// eyes
tft.fillRect(cx - 3, cy - 3, 2, 3, C_WHITE);
tft.fillRect(cx + 1, cy - 3, 2, 3, C_WHITE);
if (!G.scared) {
tft.drawPixel(cx - 2 + DC[G.dir] , cy - 2 + DR[G.dir], C_BLUE);
tft.drawPixel(cx + 2 + DC[G.dir] , cy - 2 + DR[G.dir], C_BLUE);
}
}
void drawEntities() {
for (int i = 0; i < 3; i++) { drawTile(g[i].r, g[i].c); }
drawTile(pr, pc);
for (int i = 0; i < 3; i++) drawGhost(g[i]);
drawPac();
}
void drawHUD() {
tft.setTextSize(2);
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(4, 6);
tft.print("SCORE ");
tft.print(score);
tft.print(" ");
// lives
tft.fillRect(0, OY + ROWS * T + 6, 240, 20, C_BLACK);
for (int i = 0; i < lives; i++)
tft.fillCircle(14 + i * 16, OY + ROWS * T + 16, 5, C_YELLOW);
}
void banner(const char* msg, uint16_t col) {
tft.fillRect(20, 140, 200, 50, C_BLACK);
tft.drawRect(20, 140, 200, 50, col);
tft.setTextSize(2);
tft.setTextColor(col, C_BLACK);
int w = strlen(msg) * 12;
tft.setCursor(120 - w / 2, 148);
tft.print(msg);
tft.setTextSize(1);
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(44, 172);
tft.print("Move stick to continue");
}
// ---- Game logic ----
void resetPositions() {
pr = 15; pc = 10; pdir = 3; wantDir = 3; mouth = false;
for (int i = 0; i < 3; i++) {
g[i].r = 9; g[i].c = 9 + i; g[i].dir = 0;
g[i].color = GCOL[i]; g[i].aggr = GAGGR[i];
g[i].inHouse = true; g[i].scared = false;
g[i].releaseAt = millis() + 1500UL + i * 3000UL;
}
scaredUntil = 0;
}
void startGame() {
dotsLeft = 0;
for (int r = 0; r < ROWS; r++) {
strcpy(maze[r], LEVEL[r]);
for (int c = 0; c < COLS; c++)
if (maze[r][c] == '.' || maze[r][c] == 'o') dotsLeft++;
}
score = 0; lives = 3;
state = PLAYING;
tft.fillScreen(C_BLACK);
drawMaze();
resetPositions();
drawHUD();
drawEntities();
lastPac = lastGhost = millis();
}
void loseLife() {
lives--;
drawHUD();
if (lives <= 0) {
state = DEAD_OVER;
stateSince = millis();
banner("GAME OVER", C_RED);
return;
}
delay(800);
for (int i = 0; i < 3; i++) drawTile(g[i].r, g[i].c);
drawTile(pr, pc);
resetPositions();
drawEntities();
lastPac = lastGhost = millis();
}
void checkCollisions() {
for (int i = 0; i < 3; i++) {
if (g[i].r == pr && g[i].c == pc) {
if (g[i].scared) {
score += 200;
drawTile(g[i].r, g[i].c);
g[i].r = 9; g[i].c = 10; g[i].dir = 0;
g[i].inHouse = true; g[i].scared = false;
g[i].releaseAt = millis() + 3000;
drawHUD();
} else {
loseLife();
return;
}
}
}
}
void movePac() {
if (canGo(pr, pc, wantDir, false)) pdir = wantDir;
if (canGo(pr, pc, pdir, false)) {
drawTile(pr, pc);
stepPos(pr, pc, pdir);
char &ch = maze[pr][pc];
if (ch == '.') { score += 10; dotsLeft--; ch = ' '; drawHUD(); }
else if (ch == 'o') {
score += 50; dotsLeft--; ch = ' ';
scaredUntil = millis() + 6000;
for (int i = 0; i < 3; i++) if (!g[i].inHouse) g[i].scared = true;
drawHUD();
}
}
mouth = !mouth;
}
void moveGhost(Ghost &G) {
unsigned long now = millis();
int oldR = G.r, oldC = G.c;
if (G.inHouse) {
if (now < G.releaseAt) return;
if (G.c < 10) G.dir = 3;
else if (G.c > 10) G.dir = 2;
else G.dir = 0;
stepPos(G.r, G.c, G.dir);
if (G.r == 7) G.inHouse = false;
drawTile(oldR, oldC);
return;
}
int options[4], n = 0;
for (int d = 0; d < 4; d++) {
if (d == (G.dir ^ 1)) continue; // no instant reversal
if (canGo(G.r, G.c, d, false)) options[n++] = d;
}
if (n == 0) { // dead end -> reverse
int d = G.dir ^ 1;
if (canGo(G.r, G.c, d, false)) options[n++] = d;
}
if (n == 0) return;
int pick = options[random(n)];
bool chase = !G.scared && (random(100) < G.aggr);
if (chase) {
int best = 9999;
for (int i = 0; i < n; i++) {
int d = options[i];
int nr = G.r + DR[d], nc = G.c + DC[d];
int dist = abs(nr - pr) + abs(nc - pc);
if (dist < best) { best = dist; pick = d; }
}
}
G.dir = pick;
stepPos(G.r, G.c, G.dir);
drawTile(oldR, oldC);
}
void readInput() {
int x = analogRead(JOY_X) - 2048;
int y = analogRead(JOY_Y) - 2048;
if (INVERT_X) x = -x;
if (INVERT_Y) y = -y;
if (abs(x) < DEADZONE && abs(y) < DEADZONE) return; // stick centered
if (abs(x) > abs(y)) wantDir = (x > 0) ? 3 : 2; // right / left
else wantDir = (y > 0) ? 1 : 0; // down / up
}
bool anyButton() {
int x = analogRead(JOY_X) - 2048;
int y = analogRead(JOY_Y) - 2048;
return !digitalRead(JOY_SW) || abs(x) > DEADZONE || abs(y) > DEADZONE;
}
// ---- Title screen ----
int titleX;
bool titleGoingRight, titleArmed;
unsigned long lastTitleAnim, lastBlink;
bool blinkOn;
const int TITLE_Y = 185;
void drawBigPac(int cx, int cy, bool right, bool open) {
tft.fillCircle(cx, cy, 9, C_YELLOW);
if (open) {
if (right) tft.fillTriangle(cx, cy, cx + 10, cy - 9, cx + 10, cy + 9, C_BLACK);
else tft.fillTriangle(cx, cy, cx - 10, cy - 9, cx - 10, cy + 9, C_BLACK);
}
}
void drawBigGhost(int cx, int cy, uint16_t col, bool eyesRight, bool scaredLook) {
tft.fillCircle(cx, cy - 1, 9, col);
tft.fillRect(cx - 9, cy - 1, 19, 10, col);
for (int i = -6; i <= 6; i += 6) tft.fillTriangle(cx + i - 3, cy + 9, cx + i + 3, cy + 9, cx + i, cy + 5, C_BLACK);
if (scaredLook) {
tft.fillRect(cx - 5, cy - 4, 3, 3, C_WHITE);
tft.fillRect(cx + 2, cy - 4, 3, 3, C_WHITE);
} else {
int o = eyesRight ? 1 : -1;
tft.fillCircle(cx - 4, cy - 3, 3, C_WHITE);
tft.fillCircle(cx + 4, cy - 3, 3, C_WHITE);
tft.fillCircle(cx - 4 + o * 2, cy - 3, 1, C_BLUE);
tft.fillCircle(cx + 4 + o * 2, cy - 3, 1, C_BLUE);
}
}
void showTitle() {
state = TITLE;
stateSince = millis();
titleArmed = false;
titleX = -80;
titleGoingRight = true;
lastTitleAnim = lastBlink = millis();
blinkOn = true;
tft.fillScreen(C_BLACK);
// Logo
tft.setTextSize(5);
tft.setTextColor(C_YELLOW, C_BLACK);
tft.setCursor(30, 50);
tft.print("PACMAN");
tft.drawFastHLine(30, 94, 180, C_BLUE);
tft.drawFastHLine(30, 97, 180, C_BLUE);
tft.setTextSize(2);
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(42, 110);
tft.print("ESP32 EDITION");
// Point values legend
tft.fillCircle(60, 250, 1, C_WHITE);
tft.setTextSize(1);
tft.setCursor(72, 247); tft.print("10 PTS");
tft.fillCircle(130, 250, 3, C_WHITE);
tft.setCursor(140, 247); tft.print("50 PTS");
tft.fillCircle(60, 268, 4, C_BLUE);
tft.setCursor(72, 265); tft.print("GHOST 200 PTS");
tft.setTextColor(C_ORANGE, C_BLACK);
tft.setCursor(66, 302);
tft.print("JOYSTICK TO STEER");
}
void titleLoop() {
unsigned long now = millis();
// wait until stick is released once, then any input starts the game
if (!anyButton()) titleArmed = true;
if (titleArmed && now - stateSince > 400 && anyButton()) { startGame(); return; }
// blinking prompt
if (now - lastBlink >= 500) {
lastBlink = now;
blinkOn = !blinkOn;
tft.setTextSize(2);
tft.setTextColor(blinkOn ? C_WHITE : C_BLACK, C_BLACK);
tft.setCursor(24, 215);
tft.print("PUSH TO START");
}
// chase animation
if (now - lastTitleAnim >= 50) {
lastTitleAnim = now;
bool open = (titleX / 8) % 2 == 0;
if (titleGoingRight) titleX += 5; else titleX -= 5;
if (titleGoingRight && titleX > 340) titleGoingRight = false;
if (!titleGoingRight && titleX < -90) titleGoingRight = true;
tft.fillRect(0, TITLE_Y - 14, 240, 28, C_BLACK);
if (titleGoingRight) {
for (int i = 0; i < 3; i++)
drawBigGhost(titleX - 34 - i * 24, TITLE_Y, GCOL[i], true, false);
drawBigPac(titleX, TITLE_Y, true, open);
} else {
for (int i = 0; i < 3; i++)
drawBigGhost(titleX + 34 + i * 24, TITLE_Y, C_BLUE, false, true);
drawBigPac(titleX, TITLE_Y, false, open);
}
}
}
// ---- Arduino ----
void setup() {
pinMode(JOY_SW, INPUT_PULLUP);
randomSeed(analogRead(36));
tft.begin();
tft.setRotation(0); // portrait 240x320
showTitle();
}
void loop() {
unsigned long now = millis();
if (state == TITLE) { titleLoop(); return; }
if (state != PLAYING) { // game over / win -> back to title
if (now - stateSince > 800 && anyButton()) showTitle();
return;
}
readInput();
if (scaredUntil && now > scaredUntil) {
scaredUntil = 0;
for (int i = 0; i < 3; i++) g[i].scared = false;
}
if (now - lastPac >= PAC_MS) {
lastPac = now;
movePac();
checkCollisions();
if (state == PLAYING) drawEntities();
}
unsigned long ghostMs = scaredUntil ? 280 : 190;
if (state == PLAYING && now - lastGhost >= ghostMs) {
lastGhost = now;
for (int i = 0; i < 3; i++) moveGhost(g[i]);
checkCollisions();
if (state == PLAYING) drawEntities();
}
if (state == PLAYING && dotsLeft <= 0) {
state = WON;
stateSince = millis();
banner("YOU WIN!", C_YELLOW);
}
}