Pssst, like flappy bird? Me too. Upload this code to your Adafriend and see what happens. Have fun! You're welcome ;)

// Arduino Flappy Bird homage by [email protected]
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <Tone.h>

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

Tone tone1;

#define OCTAVE_OFFSET 0

const int notes[] = { 
  0,
  NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
  NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
  NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
  NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};

static const uint8_t PROGMEM
bird0[] =
{ 
  B00011000,
  B00111100,
  B00110100,
  B11111111,
  B10111111,
  B11011100,
  B01111100,
  B00111000 }
,
bird1[] =
{ 
  B00000000,
  B00011000,
  B00111100,
  B00110111,
  B11111111,
  B10011100,
  B11111100,
  B00111000 }
,
bird2[] =
{ 
  B00011000,
  B00111100,
  B00110100,
  B00111111,
  B01111111,
  B11011100,
  B10111100,
  B11111000 };

const byte vibration PROGMEM = A0; // vibration sensor
const int tapLevel PROGMEM = 512;

double initYVelocity = 6.5;
double gravity = -9.8;
long birdTime;
long previousBirdTime;
byte birdPos = 3;
byte tailPos = 3;
byte birdStart;
byte topWall;
byte bottomWall;
int wallDelay = 250;
byte wallGap = 5;
long wallTime;
int wallCount;
long previousWallTime;
byte wallPosition = 0;
boolean gameMode = false;
long frameTime;
long previousFrameTime;
byte frame = 0;
byte frameChange = 1;
byte lives = 2;

void setup() {

  pinMode(vibration, INPUT_PULLUP);

  matrix.begin(0x70);
  matrix.setRotation(3);
  matrix.setBrightness(3);
  matrix.setTextSize(1);
  matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  matrix.setTextColor(LED_ON);
  matrix.clear();
  matrix.writeDisplay();
  previousBirdTime = millis();
  topWall = random(0,4);
  bottomWall = topWall + 4;
  wallPosition = 0;
  gameMode = false;
}

void loop() {
  matrix.clear();

  if(gameMode == false)
  {

    if(analogRead(vibration)< tapLevel)
    {
      gameMode = true;
    }

    frameTime = millis();

    if(frameTime - previousFrameTime > 250) {
      previousFrameTime = frameTime;   
      frame += frameChange;
      if(frame >= 2 || frame <= 0) frameChange *= -1;
    }

    if(frame == 0) matrix.drawBitmap(0, 0, bird0, 8, 8, LED_ON);
    else if(frame == 1) matrix.drawBitmap(0, 0, bird1, 8, 8, LED_ON);
    else if(frame == 2) matrix.drawBitmap(0, 0, bird2, 8, 8, LED_ON);
  } 

  else 
  {
    if(analogRead(vibration)<tapLevel)
    {
      previousBirdTime = millis();
      birdStart = birdPos;
    }

    drawWalls();
    drawBird();

    if(wallPosition == 6)
    {
      byte next = 7 - calculateNextY();
      if(next >= bottomWall || next <= topWall)
      {
        gameMode = false;

        int scoreInt = wallCount-1;

        const char* temp = "Game Over";
   
        for (int8_t x=7; x>=-1*9*5+1; x--)
        {
          matrix.clear();
          matrix.setCursor(x,0);
          matrix.print(temp);
          matrix.writeDisplay();
          delay(75);
        }
      }
      wallCount++;
    }

  }

  matrix.writeDisplay(); 
}

void drawWalls()
{
  wallTime = millis();

  if(wallPosition > 7) 
  {
    topWall = random(0,4);
    bottomWall = topWall + 4;
    wallPosition = 0;
  }

  matrix.drawLine(7-wallPosition, 0, 7-wallPosition, topWall, LED_ON); 

  matrix.drawLine(7-wallPosition, bottomWall, 7-wallPosition, 7, LED_ON); 

  if(wallTime - previousWallTime > wallDelay) {
    previousWallTime = wallTime;   
    wallPosition++;
  }
}

int calculateY()
{
  return (int)(birdStart+((birdTime/1000.0)*(initYVelocity+gravity*(birdTime/1000.0)/2.0)));
}

int calculateTail()
{
  return (int)(birdStart+(((birdTime-100)/1000.0)*(initYVelocity+gravity*((birdTime-100)/1000.0)/2.0)));
}

int calculateNextY()
{
  return (int)(birdStart+(((birdTime+100)/1000.0)*(initYVelocity+gravity*((birdTime+100)/1000.0)/2.0)));
}

void drawBird()
{
  birdTime = millis() - previousBirdTime;
  birdPos = calculateY();
  tailPos = calculateTail();
  if(calculateY() < 0) birdPos = 0;
  if(calculateTail() < 0) tailPos = 0;
  matrix.drawPixel(0, 7-tailPos, LED_ON); 
  matrix.drawPixel(1, 7-birdPos, LED_ON); 
  matrix.writeDisplay(); 
}


This guide was first published on Jun 28, 2016. It was last updated on Jun 28, 2016.

This page (...) was last updated on Jun 21, 2016.

Text editor powered by tinymce.