#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// ---------- Display (same wiring as the solitaire build) ----------
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
// ---------- Joystick ----------
const int JOY_X = 35, JOY_Y = 34, JOY_SEL = 27;
const bool FLIP_X = true; // flip if left/right feel reversed
const bool FLIP_Y = true; // flip if up/down feel reversed
// ---------- Layout (landscape 320x240) ----------
const uint8_t W = 10, H = 20;
const uint8_t CELL = 11; // 10*11 = 110 wide, 20*11 = 220 tall
const int FX = 20, FY = 10; // playfield top-left
const int PX = 160; // side panel left edge
// ---------- Colours (RGB565) ----------
const uint16_t BG = ILI9341_BLACK;
const uint16_t GRID = 0x2104;
const uint16_t COLORS[7] = {
0x07FF, // I cyan
0x041F, // J blue
0xFD20, // L orange
0xFFE0, // O yellow
0x07E0, // S green
0xA01F, // T purple
0xF800 // Z red
};
// ---------- Pieces: 4x4 bitmasks, 4 rotations each ----------
const uint16_t PIECES[7][4] = {
{0x0F00, 0x2222, 0x00F0, 0x4444}, // I
{0x8E00, 0x6440, 0x0E20, 0x44C0}, // J
{0x2E00, 0x4460, 0x0E80, 0xC440}, // L
{0xCC00, 0xCC00, 0xCC00, 0xCC00}, // O
{0x6C00, 0x4620, 0x06C0, 0x8C40}, // S
{0x4E00, 0x4640, 0x0E40, 0x4C40}, // T
{0xC600, 0x2640, 0x0C60, 0x4C80} // Z
};
enum State : uint8_t { STATE_TITLE, STATE_PLAY, STATE_OVER };
State state = STATE_TITLE;
uint8_t board[H][W]; // 0 = empty, 1..7 = piece type + 1
uint8_t shown[H][W]; // what is currently on screen (for diff drawing)
int8_t curP, curR, curX, curY, nextP;
unsigned int score;
uint16_t lines;
unsigned long lastDrop, lastMove, lastSoft, lastBlink;
bool prevUp, prevBtn;
bool blinkOn = true;
bool panelDirty = true;
// ---------- Piece helpers ----------
uint16_t mask(int8_t p, int8_t r) { return PIECES[p][r]; }
bool fits(int8_t p, int8_t r, int8_t x, int8_t y) {
uint16_t m = mask(p, r);
uint8_t row = 0, col = 0;
for (uint16_t bit = 0x8000; bit; bit >>= 1) {
if (m & bit) {
int8_t bx = x + col, by = y + row;
if (bx < 0 || bx >= W || by >= H) return false;
if (by >= 0 && board[by][bx]) return false;
}
if (++col == 4) { col = 0; row++; }
}
return true;
}
void lockPiece() {
uint16_t m = mask(curP, curR);
uint8_t row = 0, col = 0;
for (uint16_t bit = 0x8000; bit; bit >>= 1) {
if (m & bit) {
int8_t bx = curX + col, by = curY + row;
if (by >= 0) board[by][bx] = curP + 1;
}
if (++col == 4) { col = 0; row++; }
}
uint8_t cleared = 0;
for (int8_t y = H - 1; y >= 0; y--) {
bool full = true;
for (uint8_t x = 0; x < W; x++) if (!board[y][x]) { full = false; break; }
if (full) {
cleared++;
for (int8_t yy = y; yy > 0; yy--) memcpy(board[yy], board[yy - 1], W);
memset(board[0], 0, W);
y++;
}
}
const uint8_t pts[5] = {0, 10, 30, 60, 100};
score += pts[cleared] * (1 + lines / 10);
lines += cleared;
panelDirty = true;
}
void spawn() {
curP = nextP;
nextP = random(7);
curR = 0; curX = 3; curY = 0;
panelDirty = true;
if (!fits(curP, curR, curX, curY)) state = STATE_OVER;
}
unsigned long dropInterval() {
int v = 600 - (lines / 10) * 50;
return v < 100 ? 100 : v;
}
// ---------- Drawing ----------
// One block: t = 0 empty, 1..7 coloured piece. s = cell size in pixels.
void drawCell(int x, int y, uint8_t t, uint8_t s) {
if (t == 0) {
tft.fillRect(x, y, s, s, BG);
tft.drawPixel(x + s / 2, y + s / 2, GRID);
return;
}
uint16_t c = COLORS[t - 1];
tft.fillRect(x, y, s, s, c);
tft.drawRect(x, y, s, s, BG); // 1px gap between blocks
tft.drawFastHLine(x + 1, y + 1, s - 3, ILI9341_WHITE); // light top edge
tft.drawFastVLine(x + 1, y + 1, s - 3, ILI9341_WHITE); // light left edge
}
// Draws a piece at pixel position (ox, oy) with the given cell size (title + next preview)
void drawMiniPiece(int8_t p, int8_t r, int ox, int oy, uint8_t s) {
uint16_t m = mask(p, r);
uint8_t row = 0, col = 0;
for (uint16_t bit = 0x8000; bit; bit >>= 1) {
if (m & bit) drawCell(ox + col * s, oy + row * s, p + 1, s);
if (++col == 4) { col = 0; row++; }
}
}
// Draws only the cells that changed since the last call (no flicker)
void drawField() {
uint8_t v[H][W];
memcpy(v, board, sizeof(v));
if (state == STATE_PLAY) {
uint16_t m = mask(curP, curR);
uint8_t row = 0, col = 0;
for (uint16_t bit = 0x8000; bit; bit >>= 1) {
if (m & bit) {
int8_t bx = curX + col, by = curY + row;
if (by >= 0 && by < H && bx >= 0 && bx < W) v[by][bx] = curP + 1;
}
if (++col == 4) { col = 0; row++; }
}
}
for (uint8_t y = 0; y < H; y++)
for (uint8_t x = 0; x < W; x++)
if (v[y][x] != shown[y][x]) {
drawCell(FX + x * CELL, FY + y * CELL, v[y][x], CELL);
shown[y][x] = v[y][x];
}
}
void drawLabels() {
tft.setTextSize(2);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(PX, 16); tft.print("SCORE");
tft.setCursor(PX, 62); tft.print("LEVEL");
tft.setCursor(PX, 108); tft.print("LINES");
tft.setCursor(PX, 154); tft.print("NEXT");
}
void drawPanel() {
char buf[12];
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE, BG);
snprintf(buf, sizeof(buf), "%-7u", score); tft.setCursor(PX, 34); tft.print(buf);
snprintf(buf, sizeof(buf), "%-7u", lines / 10 + 1); tft.setCursor(PX, 80); tft.print(buf);
snprintf(buf, sizeof(buf), "%-7u", (unsigned)lines); tft.setCursor(PX, 126); tft.print(buf);
tft.fillRect(PX, 174, 56, 48, BG);
drawMiniPiece(nextP, 0, PX, 178, CELL);
panelDirty = false;
}
void drawGameOver() {
int bx = FX + 5, by = FY + 70;
tft.fillRoundRect(bx, by, 100, 80, 6, BG);
tft.drawRoundRect(bx, by, 100, 80, 6, ILI9341_YELLOW);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.setCursor(bx + 14, by + 8); tft.print("GAME");
tft.setCursor(bx + 14, by + 34); tft.print("OVER");
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.setCursor(bx + 8, by + 64); tft.print("CLICK TO RETRY");
}
void drawTitleStatic() {
tft.fillScreen(BG);
tft.setTextSize(5);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(70, 40);
tft.print("TETRIS");
// decorative pieces: I, T, O, S
drawMiniPiece(0, 0, 30, 100, 14);
drawMiniPiece(5, 0, 110, 100, 14);
drawMiniPiece(3, 0, 176, 100, 14);
drawMiniPiece(4, 0, 224, 100, 14);
}
void drawTitleBlink() {
tft.setTextSize(2);
tft.setTextColor(blinkOn ? ILI9341_WHITE : BG);
tft.setCursor(34, 180);
tft.print("PRESS BUTTON TO START");
}
// ---------- Game setup ----------
void newGame() {
memset(board, 0, sizeof(board));
memset(shown, 255, sizeof(shown)); // 255 = unknown, forces a full redraw
score = 0; lines = 0;
state = STATE_PLAY;
nextP = random(7);
spawn();
lastDrop = millis();
tft.fillScreen(BG);
tft.drawRect(FX - 2, FY - 2, W * CELL + 4, H * CELL + 4, ILI9341_WHITE);
drawLabels();
drawField();
drawPanel();
}
// lock + spawn, and show the game-over box if the new piece doesn't fit
void finishPiece() {
lockPiece();
spawn();
if (state == STATE_OVER) {
drawField();
drawPanel();
drawGameOver();
}
}
// ---------- Input ----------
void readJoy(bool &left, bool &right, bool &up, bool &down) {
int ax = analogRead(JOY_X), ay = analogRead(JOY_Y);
if (FLIP_X) ax = 4095 - ax;
if (FLIP_Y) ay = 4095 - ay;
left = ax < 1200;
right = ax > 2800;
up = ay < 1200;
down = ay > 2800;
}
// ---------- Arduino ----------
void setup() {
Serial.begin(115200);
pinMode(JOY_SEL, INPUT_PULLUP);
randomSeed(esp_random());
tft.begin();
tft.setRotation(1); // landscape 320x240
lastBlink = millis();
drawTitleStatic();
drawTitleBlink();
}
void loop() {
unsigned long now = millis();
bool btn = (digitalRead(JOY_SEL) == LOW);
// ---------- Title screen ----------
if (state == STATE_TITLE) {
if (now - lastBlink > 500) {
lastBlink = now;
blinkOn = !blinkOn;
drawTitleBlink();
}
if (btn && !prevBtn) {
randomSeed(esp_random());
newGame();
}
prevBtn = btn; // keeps the start press from counting as a hard drop
return;
}
// ---------- Game over ----------
if (state == STATE_OVER) {
if (btn && !prevBtn) newGame();
prevBtn = btn;
return;
}
// ---------- Playing ----------
bool left, right, up, down;
readJoy(left, right, up, down);
bool dirty = false;
if ((left || right) && now - lastMove > 120) {
int8_t dx = left ? -1 : 1;
if (fits(curP, curR, curX + dx, curY)) { curX += dx; dirty = true; }
lastMove = now;
}
if (up && !prevUp) {
int8_t nr = (curR + 1) & 3;
if (fits(curP, nr, curX, curY)) { curR = nr; dirty = true; }
else if (fits(curP, nr, curX - 1, curY)) { curR = nr; curX--; dirty = true; }
else if (fits(curP, nr, curX + 1, curY)) { curR = nr; curX++; dirty = true; }
}
prevUp = up;
if (btn && !prevBtn) {
while (fits(curP, curR, curX, curY + 1)) curY++;
finishPiece();
lastDrop = now; dirty = true;
}
prevBtn = btn;
if (state != STATE_PLAY) return; // game just ended
if (down && now - lastSoft > 50) {
lastSoft = now;
lastDrop = 0;
}
if (now - lastDrop >= dropInterval()) {
if (fits(curP, curR, curX, curY + 1)) curY++;
else finishPiece();
lastDrop = now;
dirty = true;
}
if (state != STATE_PLAY) return;
if (dirty) {
drawField();
if (panelDirty) drawPanel();
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4