This is a remake of tetris game for mbed. Please see detail here http://developer.mbed.org/users/sucrelv/notebook/tetris-game-on-mbed

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player

Committer:
sucrelv
Date:
Tue Oct 21 15:10:36 2014 +0000
Revision:
0:3b5e97ab5884
initial upload

Who changed what in which revision?

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