Arnov Sharma
Published © MIT

XIAO Hopper

Our DIY Version of Flappy Bird featuring the XIAO ESP32 C6 and Custom 16x8 matrix as Display.

BeginnerFull instructions provided2 hours260
XIAO Hopper

Things used in this project

Hardware components

Seeed Studio XIAO ESP32 C6
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Seeed Studio Fusion PCB/PCBA
Seeed Studio Fusion PCB/PCBA

Story

Read more

Schematics

SCH

Code

code

C/C++
#include <Adafruit_NeoMatrix.h>

#define MATRIX_PIN 0
#define MATRIX_WIDTH 16
#define MATRIX_HEIGHT 8

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_PIN,
  NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS, NEO_GRB + NEO_KHZ800);

#define BUTTON_PIN 1

int birdY = MATRIX_HEIGHT / 2;

struct Pillar {
  int x;
  int gapStart;
  int gapHeight;
};
Pillar pillars[5];

int gameSpeed = 200; // Initial game speed
int difficultyCounter = 0; // Difficulty tracking

void setup() {
  matrix.begin();
  matrix.setBrightness(50);
  matrix.fillScreen(0);
  matrix.show();

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  for (int i = 0; i < 5; i++) {
    pillars[i].x = MATRIX_WIDTH + i * 8;
    pillars[i].gapStart = random(2, MATRIX_HEIGHT - 2);
    pillars[i].gapHeight = 3;
  }

  Serial.begin(115200);
}

void loop() {
  matrix.fillScreen(0);

  // Bird movement based on button hold
  if (digitalRead(BUTTON_PIN) == LOW) {
    birdY -= 1; // Bird moves upward while button is held
  } else {
    birdY += 1; // Gravity pulls bird downward when button is released
  }

  // Clamp bird position to stay within matrix bounds
  if (birdY < 0) birdY = 0;
  if (birdY >= MATRIX_HEIGHT) birdY = MATRIX_HEIGHT - 1;

  // Draw bird
  matrix.drawPixel(3, birdY, matrix.Color(255, 255, 0)); // Yellow bird

  // Move and draw pillars
  uint32_t pillarColor = matrix.Color(128, 0, 128); // Purple for initial difficulty
  if (difficultyCounter > 50) {
    pillarColor = matrix.Color(255, 0, 0); // Transition to red for higher difficulty
  }

  for (int i = 0; i < 5; i++) {
    pillars[i].x--;
    if (pillars[i].x < 0) {
      pillars[i].x = MATRIX_WIDTH;
      pillars[i].gapStart = random(2, MATRIX_HEIGHT - 2);
      difficultyCounter++;
    }

    for (int y = 0; y < MATRIX_HEIGHT; y++) {
      if (y < pillars[i].gapStart || y > pillars[i].gapStart + pillars[i].gapHeight) {
        matrix.drawPixel(pillars[i].x, y, pillarColor);
      }
    }
  }

  // Collision detection
  for (int i = 0; i < 5; i++) {
    if (pillars[i].x == 3 && (birdY < pillars[i].gapStart || birdY > pillars[i].gapStart + pillars[i].gapHeight)) {
      matrix.fillScreen(matrix.Color(255, 0, 0)); // Game over
      matrix.show();
      delay(2000);
      setup(); // Restart game
      return;
    }
  }

  matrix.show();
  if (difficultyCounter % 10 == 0 && gameSpeed > 150) {
    gameSpeed -= 5;
  }

  delay(gameSpeed);
}

Credits

Arnov Sharma
338 projects • 344 followers
Just your average MAKER
Contact

Comments

Please log in or sign up to comment.