#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// Pines del TFT ILI9341
#define TFT_CS 10
#define TFT_RST 8
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Pin del botón
#define BUTTON_PIN 2
// Variables del juego
int birdY;
int birdVelocity;
int gravity = 1;
int flapStrength = -10;
int pipeX, pipeGapY;
bool gameRunning = true;
void setup() {
// Inicializar pantalla
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
// Inicializar botón
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Inicializar variables del juego
birdY = tft.height() / 2;
birdVelocity = 0;
pipeX = tft.width();
pipeGapY = random(50, tft.height() - 50);
}
void loop() {
if (gameRunning) {
// Leer el botón
if (digitalRead(BUTTON_PIN) == LOW) {
birdVelocity = flapStrength;
}
// Actualizar posición del pájaro
birdVelocity += gravity;
birdY += birdVelocity;
// Mover el tubo
pipeX -= 2;
if (pipeX < -20) {
pipeX = tft.width();
pipeGapY = random(50, tft.height() - 50);
}
// Dibujar escena
tft.fillScreen(ILI9341_BLACK);
drawBird(birdY);
drawPipe(pipeX, pipeGapY);
// Comprobar colisiones
if (birdY > tft.height() || birdY < 0 || checkCollision(pipeX, pipeGapY)) {
gameRunning = false;
}
delay(20); // Controlar la velocidad del juego
} else {
tft.setCursor(50, tft.height() / 2);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Game Over");
}
}
void drawBird(int y) {
tft.fillRect(30, y, 10, 10, ILI9341_YELLOW);
}
void drawPipe(int x, int gapY) {
tft.fillRect(x, 0, 20, gapY, ILI9341_GREEN);
tft.fillRect(x, gapY + 60, 20, tft.height() - gapY - 60, ILI9341_GREEN);
}
bool checkCollision(int pipeX, int pipeGapY) {
if (pipeX < 40 && pipeX > 20) {
if (birdY < pipeGapY || birdY > pipeGapY + 60) {
return true;
}
}
return false;
}