No title

TFT VCC  → 3.3V

TFT GND  → GND

TFT SCK  → GPIO 18

TFT SDA  → GPIO 23

TFT RES  → GPIO 4

TFT DC   → GPIO 2

TFT CS   → GPIO 5


Button:

One side → GPIO 15

Other → GND

           CODE 

 #include <Adafruit_GFX.h>

#include <Adafruit_ST7735.h>

#include <SPI.h>


#define TFT_CS     5

#define TFT_RST    4

#define TFT_DC     2


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);


#define BTN 15


int birdY = 60;

int velocity = 0;


int pipeX = 160;

int gapY = 50;


int score = 0;

bool gameOver = false;


void setup() {

  pinMode(BTN, INPUT_PULLUP);


  tft.initR(INITR_BLACKTAB);

  tft.fillScreen(ST77XX_BLACK);

}


void loop() {


  if (gameOver) {

    tft.setCursor(20, 60);

    tft.setTextColor(ST77XX_RED);

    tft.setTextSize(2);

    tft.print("GAME OVER");

    delay(2000);

    resetGame();

  }


  // Button jump

  if (digitalRead(BTN) == LOW) {

    velocity = -6;

  }


  // Gravity

  velocity += 1;

  birdY += velocity;


  // Move pipe

  pipeX -= 3;


  if (pipeX < 0) {

    pipeX = 160;

    gapY = random(20, 100);

    score++;

  }


  // Collision

  if (birdY < 0 || birdY > 128) gameOver = true;


  if (pipeX < 30 && pipeX > 10) {

    if (birdY < gapY || birdY > gapY + 40) {

      gameOver = true;

    }

  }


  drawGame();

  delay(30);

}


void drawGame() {

  tft.fillScreen(ST77XX_BLACK);


  // Bird

  tft.fillCircle(20, birdY, 5, ST77XX_YELLOW);


  // Pipes

  tft.fillRect(pipeX, 0, 10, gapY, ST77XX_GREEN);

  tft.fillRect(pipeX, gapY + 40, 10, 128, ST77XX_GREEN);


  // Score

  tft.setCursor(5, 5);

  tft.setTextColor(ST77XX_WHITE);

  tft.setTextSize(1);

  tft.print("Score: ");

  tft.print(score);

}


void resetGame() {

  birdY = 60;

  velocity = 0;

  pipeX = 160;

  score = 0;

  gameOver = false;

}

Post a Comment

Previous Post Next Post

Contact Form