Lois
Published © CERN-OHL2

Arduino Pac-Man Game: Eat beans, avoid walls

Navigate Pac-Man on an OLED display to eat beans and avoid walls in the maze.

BeginnerProtip2 hours78

Things used in this project

Story

Read more

Schematics

Pac-Man Game

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.

Code

Arduino Pac-Man Game: Eat beans, avoid walls

Arduino
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.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define PACMAN_SIZE 6

int pacmanX = 2;
int pacmanY = 2;
int beanX;
int beanY;

const int gridWidth = 16;
const int gridHeight = 8;

const int grid[gridHeight][gridWidth] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
};

int direction = 0; // 0: Stay, 1: Up, 2: Down, 3: Left, 4: Right
int score = 0;

void setup() {
  Serial.begin(9600);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(F("Pacman Game"));
  display.display();
  delay(2000);
  display.clearDisplay();

  pinMode(2, INPUT_PULLUP); // Up
  pinMode(3, INPUT_PULLUP); // Down
  pinMode(4, INPUT_PULLUP); // Left
  pinMode(5, INPUT_PULLUP); // Right

  resetBean();
}

void resetBean() {
  do {
    beanX = random(1, gridWidth - 1);
    beanY = random(1, gridHeight - 1);
  } while (grid[beanY][beanX] == 1); // 确保豆子不会生成在墙上
}

int readButton() {
  if (digitalRead(2) == LOW) return 1; // Up
  if (digitalRead(3) == LOW) return 2; // Down
  if (digitalRead(4) == LOW) return 3; // Left
  if (digitalRead(5) == LOW) return 4; // Right
  return 0; // Stay
}

void loop() {
  int newDirection = readButton();

  if (newDirection != 0) {
    direction = newDirection;

    int newX = pacmanX;
    int newY = pacmanY;

    switch (direction) {
      case 1: newY--; break; // Up
      case 2: newY++; break; // Down
      case 3: newX--; break; // Left
      case 4: newX++; break; // Right
    }

    if (grid[newY][newX] == 0) {
      pacmanX = newX;
      pacmanY = newY;
    }

    if (pacmanX == beanX && pacmanY == beanY) {
      score += 10;
      resetBean();
    }
  }

  display.clearDisplay();

  for (int i = 0; i < gridHeight; i++) {
    for (int j = 0; j < gridWidth; j++) {
      if (grid[i][j] == 1) {
        int drawX = j * PACMAN_SIZE;
        int drawY = (gridHeight - 1 - i) * PACMAN_SIZE; // 反转Y坐标
        display.drawRect(drawX, drawY, PACMAN_SIZE, PACMAN_SIZE, SSD1306_WHITE);
      }
    }
  }

  // 绘制吃豆人
  int pacmanDrawX = pacmanX * PACMAN_SIZE;
  int pacmanDrawY = (gridHeight - 1 - pacmanY) * PACMAN_SIZE; // 反转Y坐标
  display.fillRect(pacmanDrawX, pacmanDrawY, PACMAN_SIZE, PACMAN_SIZE, SSD1306_WHITE);

  // 绘制豆子
  int beanDrawX = beanX * PACMAN_SIZE + PACMAN_SIZE / 2;
  int beanDrawY = (gridHeight - 1 - beanY) * PACMAN_SIZE + PACMAN_SIZE / 2; // 反转Y坐标
  display.fillCircle(beanDrawX, beanDrawY, PACMAN_SIZE / 2, SSD1306_WHITE);

  display.setCursor(0, SCREEN_HEIGHT - 8);
  display.print(F("Score: "));
  display.print(score);
  display.display();

  delay(100);
}

Credits

Lois
13 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.