Tetris Base Game
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Game.cpp@0:1858f2b100fd, 20 months ago (annotated)
- Committer:
- jsanchez307
- Date:
- Wed Nov 30 21:57:25 2022 +0000
- Revision:
- 0:1858f2b100fd
Tetris Skeleton V 1.0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jsanchez307 | 0:1858f2b100fd | 1 | #include "Game.h" |
| jsanchez307 | 0:1858f2b100fd | 2 | |
| jsanchez307 | 0:1858f2b100fd | 3 | /* |
| jsanchez307 | 0:1858f2b100fd | 4 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 5 | Init |
| jsanchez307 | 0:1858f2b100fd | 6 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 7 | */ |
| jsanchez307 | 0:1858f2b100fd | 8 | Game::Game(Board *pBoard, Pieces *pPieces, int pScreenHeight, uLCD_4DGL *pLCD,int initPiece,int initPos,int initNextPiece,int initNextPos) |
| jsanchez307 | 0:1858f2b100fd | 9 | { |
| jsanchez307 | 0:1858f2b100fd | 10 | mScreenHeight = pScreenHeight; |
| jsanchez307 | 0:1858f2b100fd | 11 | |
| jsanchez307 | 0:1858f2b100fd | 12 | // Get the pointer to the Board and Pieces classes |
| jsanchez307 | 0:1858f2b100fd | 13 | mBoard = pBoard; |
| jsanchez307 | 0:1858f2b100fd | 14 | mPieces = pPieces; |
| jsanchez307 | 0:1858f2b100fd | 15 | uLCD = pLCD; |
| jsanchez307 | 0:1858f2b100fd | 16 | // Game initialization |
| jsanchez307 | 0:1858f2b100fd | 17 | InitGame (initPiece,initPos,initNextPiece,initNextPos); |
| jsanchez307 | 0:1858f2b100fd | 18 | } |
| jsanchez307 | 0:1858f2b100fd | 19 | |
| jsanchez307 | 0:1858f2b100fd | 20 | /* |
| jsanchez307 | 0:1858f2b100fd | 21 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 22 | Initial parameters of the game |
| jsanchez307 | 0:1858f2b100fd | 23 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 24 | */ |
| jsanchez307 | 0:1858f2b100fd | 25 | void Game::InitGame(int initPiece,int initPos,int initNextPiece,int initNextPos) |
| jsanchez307 | 0:1858f2b100fd | 26 | { |
| jsanchez307 | 0:1858f2b100fd | 27 | //points |
| jsanchez307 | 0:1858f2b100fd | 28 | points = 0; |
| jsanchez307 | 0:1858f2b100fd | 29 | clearedLineCount=0; |
| jsanchez307 | 0:1858f2b100fd | 30 | |
| jsanchez307 | 0:1858f2b100fd | 31 | // First piece |
| jsanchez307 | 0:1858f2b100fd | 32 | mPiece = initPiece; |
| jsanchez307 | 0:1858f2b100fd | 33 | mRotation = initPos; |
| jsanchez307 | 0:1858f2b100fd | 34 | mPosX = (BOARD_WIDTH / 2) + mPieces->GetXInitialPosition (mPiece, mRotation); |
| jsanchez307 | 0:1858f2b100fd | 35 | mPosY = mPieces->GetYInitialPosition (mPiece, mRotation); |
| jsanchez307 | 0:1858f2b100fd | 36 | |
| jsanchez307 | 0:1858f2b100fd | 37 | // Next piece |
| jsanchez307 | 0:1858f2b100fd | 38 | mNextPiece = initNextPiece; |
| jsanchez307 | 0:1858f2b100fd | 39 | mNextRotation = initNextPos; |
| jsanchez307 | 0:1858f2b100fd | 40 | mNextPosX = BOARD_WIDTH + 2; |
| jsanchez307 | 0:1858f2b100fd | 41 | mNextPosY = 2; |
| jsanchez307 | 0:1858f2b100fd | 42 | } |
| jsanchez307 | 0:1858f2b100fd | 43 | |
| jsanchez307 | 0:1858f2b100fd | 44 | /* |
| jsanchez307 | 0:1858f2b100fd | 45 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 46 | Create a random piece |
| jsanchez307 | 0:1858f2b100fd | 47 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 48 | */ |
| jsanchez307 | 0:1858f2b100fd | 49 | void Game::CreateNewPiece(int piece,int pos) |
| jsanchez307 | 0:1858f2b100fd | 50 | { |
| jsanchez307 | 0:1858f2b100fd | 51 | // The new piece |
| jsanchez307 | 0:1858f2b100fd | 52 | mPiece = mNextPiece; |
| jsanchez307 | 0:1858f2b100fd | 53 | mRotation = mNextRotation; |
| jsanchez307 | 0:1858f2b100fd | 54 | mPosX = (BOARD_WIDTH / 2) + mPieces->GetXInitialPosition (mPiece, mRotation); |
| jsanchez307 | 0:1858f2b100fd | 55 | mPosY = mPieces->GetYInitialPosition (mPiece, mRotation); |
| jsanchez307 | 0:1858f2b100fd | 56 | |
| jsanchez307 | 0:1858f2b100fd | 57 | // Random next piece |
| jsanchez307 | 0:1858f2b100fd | 58 | mNextPiece = piece; |
| jsanchez307 | 0:1858f2b100fd | 59 | mNextRotation = pos; |
| jsanchez307 | 0:1858f2b100fd | 60 | } |
| jsanchez307 | 0:1858f2b100fd | 61 | |
| jsanchez307 | 0:1858f2b100fd | 62 | /* |
| jsanchez307 | 0:1858f2b100fd | 63 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 64 | Draw piece |
| jsanchez307 | 0:1858f2b100fd | 65 | |
| jsanchez307 | 0:1858f2b100fd | 66 | Parameters: |
| jsanchez307 | 0:1858f2b100fd | 67 | |
| jsanchez307 | 0:1858f2b100fd | 68 | >> pX: Horizontal position in blocks |
| jsanchez307 | 0:1858f2b100fd | 69 | >> pY: Vertical position in blocks |
| jsanchez307 | 0:1858f2b100fd | 70 | >> pPiece: Piece to draw |
| jsanchez307 | 0:1858f2b100fd | 71 | >> pRotation: 1 of the 4 possible rotations |
| jsanchez307 | 0:1858f2b100fd | 72 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 73 | */ |
| jsanchez307 | 0:1858f2b100fd | 74 | void Game::DrawPiece (int pX, int pY, int pPiece, int pRotation, int colorIndex) |
| jsanchez307 | 0:1858f2b100fd | 75 | { |
| jsanchez307 | 0:1858f2b100fd | 76 | // Obtain the position in pixel in the screen of the block we want to draw |
| jsanchez307 | 0:1858f2b100fd | 77 | int mPixelsX = mBoard->GetXPosInPixels (pX); |
| jsanchez307 | 0:1858f2b100fd | 78 | int mPixelsY = mBoard->GetYPosInPixels (pY); |
| jsanchez307 | 0:1858f2b100fd | 79 | int mColors [7]={16711680, 15955765, 16761893, 45401, 44763, 12462572, 29669}; |
| jsanchez307 | 0:1858f2b100fd | 80 | // Travel the matrix of blocks of the piece and draw the blocks that are filled |
| jsanchez307 | 0:1858f2b100fd | 81 | for (int i = 0; i < PIECE_BLOCKS; i++) |
| jsanchez307 | 0:1858f2b100fd | 82 | { |
| jsanchez307 | 0:1858f2b100fd | 83 | for (int j = 0; j < PIECE_BLOCKS; j++) |
| jsanchez307 | 0:1858f2b100fd | 84 | { |
| jsanchez307 | 0:1858f2b100fd | 85 | if (mPieces->GetBlockType (pPiece, pRotation, j, i) != 0) |
| jsanchez307 | 0:1858f2b100fd | 86 | uLCD->filled_rectangle (mPixelsX + i * BLOCK_SIZE, |
| jsanchez307 | 0:1858f2b100fd | 87 | mPixelsY + j * BLOCK_SIZE, |
| jsanchez307 | 0:1858f2b100fd | 88 | (mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1, |
| jsanchez307 | 0:1858f2b100fd | 89 | (mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, |
| jsanchez307 | 0:1858f2b100fd | 90 | mColors[colorIndex]); |
| jsanchez307 | 0:1858f2b100fd | 91 | } |
| jsanchez307 | 0:1858f2b100fd | 92 | } |
| jsanchez307 | 0:1858f2b100fd | 93 | } |
| jsanchez307 | 0:1858f2b100fd | 94 | |
| jsanchez307 | 0:1858f2b100fd | 95 | /* |
| jsanchez307 | 0:1858f2b100fd | 96 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 97 | Draw board |
| jsanchez307 | 0:1858f2b100fd | 98 | |
| jsanchez307 | 0:1858f2b100fd | 99 | Draw the two lines that delimit the board |
| jsanchez307 | 0:1858f2b100fd | 100 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 101 | */ |
| jsanchez307 | 0:1858f2b100fd | 102 | void Game::DrawBoard () |
| jsanchez307 | 0:1858f2b100fd | 103 | { |
| jsanchez307 | 0:1858f2b100fd | 104 | // Calculate the limits of the board in pixels |
| jsanchez307 | 0:1858f2b100fd | 105 | int mX1 = BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) - 1; |
| jsanchez307 | 0:1858f2b100fd | 106 | int mX2 = BOARD_POSITION + (BLOCK_SIZE * (BOARD_WIDTH / 2)); |
| jsanchez307 | 0:1858f2b100fd | 107 | int mY = mScreenHeight - (BLOCK_SIZE * BOARD_HEIGHT); |
| jsanchez307 | 0:1858f2b100fd | 108 | |
| jsanchez307 | 0:1858f2b100fd | 109 | // Check that the vertical margin is not to small |
| jsanchez307 | 0:1858f2b100fd | 110 | //assert (mY > MIN_VERTICAL_MARGIN); |
| jsanchez307 | 0:1858f2b100fd | 111 | |
| jsanchez307 | 0:1858f2b100fd | 112 | // Rectangles that delimits the board |
| jsanchez307 | 0:1858f2b100fd | 113 | uLCD->filled_rectangle (mX1 - BOARD_LINE_WIDTH, mY, mX1, mScreenHeight - 1, BLUE); |
| jsanchez307 | 0:1858f2b100fd | 114 | uLCD->filled_rectangle (mX2, mY, mX2 + BOARD_LINE_WIDTH, mScreenHeight - 1, BLUE); |
| jsanchez307 | 0:1858f2b100fd | 115 | |
| jsanchez307 | 0:1858f2b100fd | 116 | // Check that the horizontal margin is not to small |
| jsanchez307 | 0:1858f2b100fd | 117 | //assert (mX1 > MIN_HORIZONTAL_MARGIN); |
| jsanchez307 | 0:1858f2b100fd | 118 | |
| jsanchez307 | 0:1858f2b100fd | 119 | // Drawing the blocks that are already stored in the board |
| jsanchez307 | 0:1858f2b100fd | 120 | mX1 += 1; |
| jsanchez307 | 0:1858f2b100fd | 121 | for (int i = 0; i < BOARD_WIDTH; i++) |
| jsanchez307 | 0:1858f2b100fd | 122 | { |
| jsanchez307 | 0:1858f2b100fd | 123 | for (int j = 0; j < BOARD_HEIGHT; j++) |
| jsanchez307 | 0:1858f2b100fd | 124 | { |
| jsanchez307 | 0:1858f2b100fd | 125 | // Check if the block is filled, if so, draw it |
| jsanchez307 | 0:1858f2b100fd | 126 | if (!mBoard->IsFreeBlock(i, j)) |
| jsanchez307 | 0:1858f2b100fd | 127 | uLCD->filled_rectangle ( mX1 + i * BLOCK_SIZE, |
| jsanchez307 | 0:1858f2b100fd | 128 | mY + j * BLOCK_SIZE, |
| jsanchez307 | 0:1858f2b100fd | 129 | (mX1 + i * BLOCK_SIZE) + BLOCK_SIZE - 1, |
| jsanchez307 | 0:1858f2b100fd | 130 | (mY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, |
| jsanchez307 | 0:1858f2b100fd | 131 | 10066329); |
| jsanchez307 | 0:1858f2b100fd | 132 | } |
| jsanchez307 | 0:1858f2b100fd | 133 | } |
| jsanchez307 | 0:1858f2b100fd | 134 | } |
| jsanchez307 | 0:1858f2b100fd | 135 | |
| jsanchez307 | 0:1858f2b100fd | 136 | /* |
| jsanchez307 | 0:1858f2b100fd | 137 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 138 | Draw scene |
| jsanchez307 | 0:1858f2b100fd | 139 | |
| jsanchez307 | 0:1858f2b100fd | 140 | Draw all the objects of the scene |
| jsanchez307 | 0:1858f2b100fd | 141 | ====================================== |
| jsanchez307 | 0:1858f2b100fd | 142 | */ |
| jsanchez307 | 0:1858f2b100fd | 143 | void Game::DrawScene () |
| jsanchez307 | 0:1858f2b100fd | 144 | { |
| jsanchez307 | 0:1858f2b100fd | 145 | DrawBoard(); |
| jsanchez307 | 0:1858f2b100fd | 146 | DrawPiece (mPosX, mPosY, mPiece, mRotation, mPiece); // Draw the playing piece |
| jsanchez307 | 0:1858f2b100fd | 147 | DrawPiece (mNextPosX, mNextPosY, mNextPiece, mNextRotation, mNextPiece); // Draw the next piece |
| jsanchez307 | 0:1858f2b100fd | 148 | } |
| jsanchez307 | 0:1858f2b100fd | 149 | |
| jsanchez307 | 0:1858f2b100fd | 150 | void Game::ErasePiece (int pX, int pY, int pPiece, int pRotation) |
| jsanchez307 | 0:1858f2b100fd | 151 | { |
| jsanchez307 | 0:1858f2b100fd | 152 | if(pPiece==-1) return; |
| jsanchez307 | 0:1858f2b100fd | 153 | // Obtain the position in pixel in the screen of the block we want to draw |
| jsanchez307 | 0:1858f2b100fd | 154 | int mPixelsX = mBoard->GetXPosInPixels (pX); |
| jsanchez307 | 0:1858f2b100fd | 155 | int mPixelsY = mBoard->GetYPosInPixels (pY); |
| jsanchez307 | 0:1858f2b100fd | 156 | |
| jsanchez307 | 0:1858f2b100fd | 157 | // Travel the matrix of blocks of the piece and draw the blocks that are filled |
| jsanchez307 | 0:1858f2b100fd | 158 | for (int i = 0; i < PIECE_BLOCKS; i++) |
| jsanchez307 | 0:1858f2b100fd | 159 | { |
| jsanchez307 | 0:1858f2b100fd | 160 | for (int j = 0; j < PIECE_BLOCKS; j++) |
| jsanchez307 | 0:1858f2b100fd | 161 | { |
| jsanchez307 | 0:1858f2b100fd | 162 | if (mPieces->GetBlockType (pPiece, pRotation, j, i) != 0) |
| jsanchez307 | 0:1858f2b100fd | 163 | uLCD->filled_rectangle (mPixelsX + i * BLOCK_SIZE, |
| jsanchez307 | 0:1858f2b100fd | 164 | mPixelsY + j * BLOCK_SIZE, |
| jsanchez307 | 0:1858f2b100fd | 165 | (mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1, |
| jsanchez307 | 0:1858f2b100fd | 166 | (mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, |
| jsanchez307 | 0:1858f2b100fd | 167 | 0); |
| jsanchez307 | 0:1858f2b100fd | 168 | } |
| jsanchez307 | 0:1858f2b100fd | 169 | } |
| jsanchez307 | 0:1858f2b100fd | 170 | } |
| jsanchez307 | 0:1858f2b100fd | 171 | |
| jsanchez307 | 0:1858f2b100fd | 172 | void Game::AddPoints(int newGain) |
| jsanchez307 | 0:1858f2b100fd | 173 | { |
| jsanchez307 | 0:1858f2b100fd | 174 | points+=newGain; |
| jsanchez307 | 0:1858f2b100fd | 175 | } |
| jsanchez307 | 0:1858f2b100fd | 176 | |
| jsanchez307 | 0:1858f2b100fd | 177 | void Game::AddClearedLines(int newGain) |
| jsanchez307 | 0:1858f2b100fd | 178 | { |
| jsanchez307 | 0:1858f2b100fd | 179 | clearedLineCount+=newGain; |
| jsanchez307 | 0:1858f2b100fd | 180 | } |
| jsanchez307 | 0:1858f2b100fd | 181 | |
| jsanchez307 | 0:1858f2b100fd | 182 | int Game::GetPoints() |
| jsanchez307 | 0:1858f2b100fd | 183 | { |
| jsanchez307 | 0:1858f2b100fd | 184 | return points; |
| jsanchez307 | 0:1858f2b100fd | 185 | } |
| jsanchez307 | 0:1858f2b100fd | 186 | |
| jsanchez307 | 0:1858f2b100fd | 187 | int Game::GetClearedLines() |
| jsanchez307 | 0:1858f2b100fd | 188 | { |
| jsanchez307 | 0:1858f2b100fd | 189 | return clearedLineCount; |
| jsanchez307 | 0:1858f2b100fd | 190 | } |