Tetris.h
1 // SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include "Arduino.h" 6 7 // display size information 8 #define X_MIN 0 9 #define X_MAX 7 10 #define Y_MIN 0 11 #define Y_MAX 15 12 #define WIDTH 8 13 #define HEIGHT 16 14 15 class Tetris{ 16 private: 17 // Stores the dead blocks with 1 representing a dead block, and 0 a blank grid 18 uint8_t deadTetrisBoard[16]; 19 // deadTetrisBoard and the currentPiece is merged to form activeTetrisBoard, 20 uint8_t activeTetrisBoard[16]; 21 // Piece variables 22 uint8_t* currentPiece; 23 int8_t currentPieceX; // tracks the X coordinate of the (currentPiece[0]>>7) 24 int8_t currentPieceY; // tracks the Y coordinate of the (currentPiece[0]>>7) 25 // Game variables 26 bool moveDisabled; 27 uint8_t totalLinesCleared; 28 uint16_t timeInterval; 29 unsigned long lastMoveTime; 30 unsigned long lastLandedTime; 31 32 void generatePiece(); // randomly generate a piece from piecesGenerated 33 bool checkIfLanded(); 34 bool clearLines(); // returns false if no line is cleared 35 void flashClearedLines(uint8_t* temp); 36 void convertActiveToDead(); 37 // merges the deadTetrisBoard and the currentPiece to form the activeTetrisBoard 38 // unsuccessful merge = currentPiece out of bounds or collision between currentPiece and deadTetrisBoard 39 bool mergeTetrisBoard(); 40 void gameOver(); 41 42 public: 43 bool gameRunning; 44 void init(); 45 void run(); 46 void movePiece(int8_t dx, int8_t dy); 47 void rotatePiece(); 48 void dropPiece(); 49 uint8_t* getActiveBoard(); 50 };