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