Arduino Project

Catch Game

Course Introduction

This project is a simple catching game using an OLED display, joystick, and Arduino R4 UNO. Move the basket to catch falling apples and avoid spiky bombs.

Catching apples increases your score, while bombs reduce it. Press the joystick button to start or restart the 30-second game.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 WIFI

1

2

USB Type-C cable

1

×

3

Breadboard

1

4

Wires

Several

5

Joystick Module

1

6

OLED Display Module

1

Wiring

3.png__PID:ea1afa66-4b54-4a23-b585-a8edea98d488

Common Connections:

OLED Display Module
SDA:
Connect to A4 on the Arduino.
SCK: Connect to A5 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

Joystick Module
VRY:
Connect to A1 on the Arduino.
VRX: Connect to A0 on the Arduino.
SW: Connect to 2 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

Writing the Code

Note
You can copy this code into the Arduino IDE.
To install the library, use the Arduino Library Manager and search for Adafruit SSD1306 and Adafruit GFX and install it.
Don’t forget to select the board(Arduino UNO R4 Minima) and the correct port before clicking the Upload button.


#include 
#include 
#include 

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET     -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int JOY_X = A0;
const int JOY_BTN = 2;

struct Object {
  int x, y;
  bool isCoin;  // true = coin, false = bomb
  bool active;
};

Object falling;
int playerX = SCREEN_WIDTH / 2;
const int basketWidth = 16;
int score = 0;
unsigned long startTime;
const int gameDuration = 30000; // 30s
bool inGame = false;

void setup() {
  pinMode(JOY_BTN, INPUT_PULLUP);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  showTitle();
}

void loop() {
  if (!inGame) {
    if (digitalRead(JOY_BTN) == LOW) {
      delay(300);
      startGame();
    }
    return;
  }

  unsigned long now = millis();
  if (now - startTime >= gameDuration) {
    inGame = false;
    showGameOver();
    return;
  }

  updateJoystick();
  updateObject();
  checkCollision();
  drawGame();
  delay(50);
}

void showTitle() {
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(15, 20);
  display.println("Catching");
  display.setTextSize(1);
  display.setCursor(20, 50);
  display.println("Press to Start");
  display.display();
}

void startGame() {
  inGame = true;
  score = 0;
  startTime = millis();
  playerX = SCREEN_WIDTH / 2;
  spawnObject();
}

void showGameOver() {
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(10, 20);
  display.println("Game Over");
  display.setTextSize(1);
  display.setCursor(20, 50);
  display.print("Score: ");
  display.println(score);
  display.display();
}

void updateJoystick() {
  int x = analogRead(JOY_X);
  if (x < 400) playerX -= 4;
  if (x > 600) playerX += 4;
  playerX = constrain(playerX, 0, SCREEN_WIDTH - basketWidth);
}

void spawnObject() {
  falling.active = true;
  falling.y = 0;
  falling.x = random(0, SCREEN_WIDTH - 8);
  falling.isCoin = random(0, 10) < 7;  // 70% coin, 30% bomb
}

void updateObject() {
  if (!falling.active) return;
  falling.y += 4;
  if (falling.y > SCREEN_HEIGHT) {
    falling.active = false;
    spawnObject();
  }
}

void checkCollision() {
  if (!falling.active) return;

  if (falling.y + 6 >= 56 && falling.y + 6 <= 64) {
    if (falling.x + 6 >= playerX && falling.x <= playerX + basketWidth) {
      if (falling.isCoin) score += 1;
      else score -= 1;
      falling.active = false;
      spawnObject();
    }
  }
}

void drawGame() {
  display.clearDisplay();

  // Draw basket
  display.fillRect(playerX, 56, basketWidth, 6, SSD1306_WHITE);

  // Draw falling object
  if (falling.active) {
    if (falling.isCoin) {
      // Apple: big solid circle + stem + leaf
      display.fillCircle(falling.x + 6, falling.y + 6, 6, SSD1306_WHITE);          // Apple body
      display.drawLine(falling.x + 6, falling.y + 1, falling.x + 6, falling.y - 2, SSD1306_WHITE); // Stem
      display.drawPixel(falling.x + 5, falling.y - 3, SSD1306_WHITE);             // Leaf left
      display.drawPixel(falling.x + 7, falling.y - 3, SSD1306_WHITE);             // Leaf right
    } else {
      // Spiky bomb / mine: solid center + 4 spikes
      display.fillCircle(falling.x + 6, falling.y + 6, 5, SSD1306_WHITE);         // Body
      display.drawLine(falling.x + 6, falling.y + 1, falling.x + 6, falling.y - 2, SSD1306_WHITE); // Top spike
      display.drawLine(falling.x + 6, falling.y + 11, falling.x + 6, falling.y + 14, SSD1306_WHITE); // Bottom spike
      display.drawLine(falling.x + 1, falling.y + 6, falling.x - 2, falling.y + 6, SSD1306_WHITE); // Left spike
      display.drawLine(falling.x + 11, falling.y + 6, falling.x + 14, falling.y + 6, SSD1306_WHITE); // Right spike
    }
  }

  // Draw score
  display.setCursor(0, 0);
  display.setTextSize(1);
  display.print("Score: ");
  display.print(score);

  // Draw time left
  display.setCursor(80, 0);
  int remaining = (gameDuration - (millis() - startTime)) / 1000;
  display.print("Time: ");
  display.print(remaining);

  display.display();
}