#include #include #include #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define SCREEN_ADDRESS 0x3C Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1, 1700000UL, 1700000UL); #define N_TOUCH 6 #define THRESHOLD 30 #define START_BUTTON 0 int touch_pins[N_TOUCH] = {3, 4, 2, 27, 1, 26}; int touch_values[N_TOUCH] = {0, 0, 0, 0, 0, 0}; bool pin_touched_now[N_TOUCH] = {false, false, false, false, false, false}; bool pin_touched_past[N_TOUCH] = {false, false, false, false, false, false}; struct Tile { int column; // which of the 4 columns (0–3) int y; // vertical position (0 = top, 64 = bottom) bool active; // is this tile currently falling? }; #define MAX_TILES 5 Tile tiles[MAX_TILES]; bool column_available[4] = {true, true, true, true}; // game state int score = 0; bool game_lost = false; int speed = 1; // reset the game void reset_game() { score = 0; for (int c = 0; c < 4; c++) { column_available[c] = true; } game_lost = false; for (int i=0; i THRESHOLD; } } void create_tile() { // find all the taken columns for (int c = 0; c < 4; c++) { if (column_available[c]) { // find first inactive tile slot for (int i = 0; i < MAX_TILES; i++) { if (!tiles[i].active) { tiles[i].active = true; tiles[i].column = c; tiles[i].y = 0; column_available[c] = false; // mark as taken return; } } } } } // set up function from sample code void setup() { Serial.begin(0); // give the screen some time to power up delay(50); // initialize display display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); display.clearDisplay(); display.display(); // text settings display.setTextSize(1); display.setTextColor(SSD1306_WHITE); for (int i = 0; i < MAX_TILES; i++) tiles[i].active = false; } void game_over_display() { display.clearDisplay(); display.setCursor(15, 10); display.setTextSize(2); display.print("Game Over"); display.setCursor(18,32); display.setTextSize(1); display.print("Your score is: "); display.print(score); display.setCursor(30, 45); display.setTextSize(1); display.print("Press Start"); display.display(); } // the main loop void loop() { update_touch(); if (!game_lost) { if (random(0,10) > 7) create_tile(); for (int i=0; i 0 && pin_touched_now[tiles[i].column+2]) { score++; tiles[i].active = false; column_available[tiles[i].column] = true; } if (tiles[i].active && tiles[i].y >= 56) { game_lost = true; } } } else { game_over_display(); // if Start button is touched, restart if (pin_touched_now[START_BUTTON]) { reset_game(); } delay(50); return; } display.clearDisplay(); for (int i=0; i