#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <Wire.h>
// ---- TFT Pins ----
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 cts = Adafruit_FT6206();
// ---- Game Variables ----
int birdX = 40;
int birdY = 120;
float velocity = 0;
float gravity = 0.5;
float flapForce = -6;
// Pipes
int pipeX = 240;
int pipeGap = 60;
int pipeTopHeight = 80;
int pipeWidth = 30;
// Score
int score = 0;
bool gameOver = false;
void resetGame() {
birdY = 120;
velocity = 0;
pipeX = 240;
pipeTopHeight = random(40, 150);
score = 0;
gameOver = false;
tft.fillScreen(ILI9341_BLACK);
}
void drawBird(int y) {
tft.fillCircle(birdX, y, 7, ILI9341_YELLOW);
}
void drawPipe(int x, int topHeight) {
tft.fillRect(x, 0, pipeWidth, topHeight, ILI9341_GREEN);
tft.fillRect(x, topHeight + pipeGap, pipeWidth, 240, ILI9341_GREEN);
}
bool checkCollision() {
if (birdY < 0 || birdY > 240) return true;
if (birdX + 7 > pipeX && birdX - 7 < pipeX + pipeWidth) {
if (birdY - 7 < pipeTopHeight ||
birdY + 7 > pipeTopHeight + pipeGap)
return true;
}
return false;
}
void showGameOverScreen() {
tft.fillScreen(ILI9341_RED);
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(30, 80);
tft.print("GAME OVER");
tft.setTextSize(2);
tft.setCursor(30, 140);
tft.print("Score: ");
tft.print(score);
tft.setCursor(20, 190);
tft.print("Tap to restart!");
}
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(1);
if (!cts.begin()) {
Serial.println("Touch controller NOT found!");
}
resetGame();
}
void loop() {
if (gameOver) {
if (cts.touched()) {
resetGame();
}
return;
}
// --- Touch Input ---
if (cts.touched()) {
velocity = flapForce;
}
// --- Update Bird ---
velocity += gravity;
birdY += velocity;
// --- Clear Screen ---
tft.fillScreen(ILI9341_BLACK);
// --- Update Pipes ---
pipeX -= 3;
if (pipeX < -pipeWidth) {
pipeX = 240;
pipeTopHeight = random(40, 150);
score++;
}
// --- Draw Everything ---
drawPipe(pipeX, pipeTopHeight);
drawBird(birdY);
// --- Display Score ---
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.print("Score: ");
tft.print(score);
// --- Check Collision ---
if (checkCollision()) {
gameOver = true;
showGameOverScreen();
delay(1200);
}
delay(20);
}