#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
// Grove SH1107 128x128, I2C, page buffer mode (low RAM)
U8G2_SH1107_SEEED_128X128_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
// If the picture is garbled, try this instead:
// U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
// ---------- Hardware config ----------
#define PIN_X A0
#define PIN_Y A1
#define PIN_BTN A2
#define INVERT_X true
#define INVERT_Y true
// ---------- Game constants ----------
const uint8_t W = 10, H = 20;
const uint8_t CELL = 6;
const uint8_t FX = 4, FY = 4;
const uint16_t PIECES[7][4] PROGMEM = {
{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];
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;
uint16_t mask(int8_t p, int8_t r) {
return pgm_read_word(&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] = 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;
}
void spawn() {
curP = nextP;
nextP = random(7);
curR = 0; curX = 3; curY = 0;
if (!fits(curP, curR, curX, curY)) state = STATE_OVER;
}
void newGame() {
memset(board, 0, sizeof(board));
score = 0; lines = 0;
state = STATE_PLAY;
nextP = random(7);
spawn();
lastDrop = millis();
}
unsigned long dropInterval() {
int v = 600 - (lines / 10) * 50;
return v < 100 ? 100 : v;
}
void drawPiece(int8_t p, int8_t r, int ox, int oy, int8_t cx, int8_t cy, uint8_t size) {
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 by = cy + row;
if (by >= 0)
u8g2.drawBox(ox + (cx + col) * size, oy + by * size, size - 1, size - 1);
}
if (++col == 4) { col = 0; row++; }
}
}
void drawCentered(const __FlashStringHelper *s, int y) {
// getStrWidth needs a RAM string, so copy it
char buf[24];
strncpy_P(buf, (const char *)s, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = 0;
u8g2.drawStr((128 - u8g2.getStrWidth(buf)) / 2, y, buf);
}
void drawTitle() {
u8g2.setFont(u8g2_font_logisoso16_tr);
drawCentered(F("TETRIS"), 40);
// decorative pieces
drawPiece(0, 0, 14, 52, 0, 0, 5); // I
drawPiece(5, 0, 50, 52, 0, 0, 5); // T
drawPiece(3, 0, 82, 52, 0, 0, 5); // O
drawPiece(4, 0, 100, 52, 0, 0, 5); // S
u8g2.setFont(u8g2_font_5x8_tr);
if (blinkOn) drawCentered(F("PRESS SPACE"), 92);
drawCentered(F("TO START"), 104);
}
void drawGame() {
u8g2.drawFrame(FX - 1, FY - 1, W * CELL + 2, H * CELL + 2);
for (uint8_t y = 0; y < H; y++)
for (uint8_t x = 0; x < W; x++)
if (board[y][x])
u8g2.drawBox(FX + x * CELL, FY + y * CELL, CELL - 1, CELL - 1);
if (state == STATE_PLAY) drawPiece(curP, curR, FX, FY, curX, curY, CELL);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.setCursor(72, 14); u8g2.print(F("SCORE"));
u8g2.setCursor(72, 26); u8g2.print(score);
u8g2.setCursor(72, 44); u8g2.print(F("LEVEL"));
u8g2.setCursor(72, 56); u8g2.print(lines / 10 + 1);
u8g2.setCursor(72, 74); u8g2.print(F("NEXT"));
drawPiece(nextP, 0, 76, 80, 0, 0, 5);
if (state == STATE_OVER) {
u8g2.setDrawColor(0);
u8g2.drawBox(8, 46, 52, 32);
u8g2.setDrawColor(1);
u8g2.drawFrame(8, 46, 52, 32);
u8g2.setCursor(18, 58); u8g2.print(F("GAME"));
u8g2.setCursor(18, 70); u8g2.print(F("OVER"));
}
}
void render() {
u8g2.firstPage();
do {
if (state == STATE_TITLE) drawTitle();
else drawGame();
} while (u8g2.nextPage());
}
void setup() {
// Initialize OLED display
u8g2.begin();
// Configure joystick button
pinMode(PIN_BTN, INPUT_PULLUP);
// Initialize random generator
randomSeed(analogRead(A3));
// Initialize timers
lastBlink = millis();
lastDrop = millis();
// Show title screen
render();
}
void loop() {
unsigned long now = millis();
bool btn = !digitalRead(PIN_BTN);
// ---------- Title screen ----------
if (state == STATE_TITLE) {
if (now - lastBlink > 500) {
lastBlink = now;
blinkOn = !blinkOn;
render();
}
if (btn && !prevBtn) {
randomSeed(micros() ^ analogRead(A3)); // different pieces every run
newGame();
render();
}
prevBtn = btn; // keeps the start press from counting as a hard drop
return;
}
// ---------- Game over ----------
if (state == STATE_OVER) {
if (btn && !prevBtn) { newGame(); render(); }
prevBtn = btn;
return;
}
// ---------- Playing ----------
int ax = analogRead(PIN_X), ay = analogRead(PIN_Y);
if (INVERT_X) ax = 1023 - ax;
if (INVERT_Y) ay = 1023 - ay;
bool left = ax < 300, right = ax > 700;
bool up = ay < 300, down = ay > 700;
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++;
lockPiece(); spawn();
lastDrop = now; dirty = true;
}
prevBtn = btn;
if (down && now - lastSoft > 50) {
lastSoft = now;
lastDrop = 0;
}
if (now - lastDrop >= dropInterval()) {
if (fits(curP, curR, curX, curY + 1)) curY++;
else { lockPiece(); spawn(); }
lastDrop = now;
dirty = true;
}
if (dirty) render();
}Loading
grove-oled-sh1107
grove-oled-sh1107