Jordan Sanchez / Mbed 2 deprecated Tetris

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Game.cpp Source File

Game.cpp

00001 #include "Game.h"
00002 
00003 /* 
00004 ======================================                                  
00005 Init
00006 ====================================== 
00007 */
00008 Game::Game(Board *pBoard, Pieces *pPieces, int pScreenHeight, uLCD_4DGL *pLCD,int initPiece,int initPos,int initNextPiece,int initNextPos) 
00009 {
00010     mScreenHeight = pScreenHeight;
00011     
00012     // Get the pointer to the Board and Pieces classes
00013     mBoard = pBoard;
00014     mPieces = pPieces;
00015     uLCD = pLCD;
00016     // Game initialization
00017     InitGame (initPiece,initPos,initNextPiece,initNextPos);
00018 }
00019 
00020 /* 
00021 ======================================                                  
00022 Initial parameters of the game
00023 ====================================== 
00024 */
00025 void Game::InitGame(int initPiece,int initPos,int initNextPiece,int initNextPos)
00026 {
00027     //points
00028     points = 0;
00029     clearedLineCount=0;
00030 
00031     // First piece
00032     mPiece          = initPiece;
00033     mRotation       = initPos;
00034     mPosX           = (BOARD_WIDTH / 2) + mPieces->GetXInitialPosition (mPiece, mRotation);
00035     mPosY           = mPieces->GetYInitialPosition (mPiece, mRotation);
00036 
00037     //  Next piece
00038     mNextPiece      = initNextPiece;
00039     mNextRotation   = initNextPos;
00040     mNextPosX       = BOARD_WIDTH + 2;
00041     mNextPosY       = 2;    
00042 }
00043 
00044 /* 
00045 ======================================                                  
00046 Create a random piece
00047 ====================================== 
00048 */
00049 void Game::CreateNewPiece(int piece,int pos)
00050 {
00051     // The new piece
00052     mPiece          = mNextPiece;
00053     mRotation       = mNextRotation;
00054     mPosX           = (BOARD_WIDTH / 2) + mPieces->GetXInitialPosition (mPiece, mRotation);
00055     mPosY           = mPieces->GetYInitialPosition (mPiece, mRotation);
00056 
00057     // Random next piece
00058     mNextPiece      = piece;
00059     mNextRotation   = pos;
00060 }
00061 
00062 /* 
00063 ======================================                                  
00064 Draw piece
00065 
00066 Parameters:
00067 
00068 >> pX:      Horizontal position in blocks
00069 >> pY:      Vertical position in blocks
00070 >> pPiece:  Piece to draw
00071 >> pRotation:   1 of the 4 possible rotations
00072 ====================================== 
00073 */
00074 void Game::DrawPiece (int pX, int pY, int pPiece, int pRotation, int colorIndex)
00075 {
00076     // Obtain the position in pixel in the screen of the block we want to draw
00077     int mPixelsX = mBoard->GetXPosInPixels (pX);
00078     int mPixelsY = mBoard->GetYPosInPixels (pY);
00079     int mColors [7]={16711680, 15955765, 16761893, 45401, 44763, 12462572, 29669};
00080     // Travel the matrix of blocks of the piece and draw the blocks that are filled
00081     for (int i = 0; i < PIECE_BLOCKS; i++)
00082     {
00083         for (int j = 0; j < PIECE_BLOCKS; j++)
00084         {
00085             if (mPieces->GetBlockType (pPiece, pRotation, j, i) != 0)
00086                 uLCD->filled_rectangle  (mPixelsX + i * BLOCK_SIZE, 
00087                                     mPixelsY + j * BLOCK_SIZE, 
00088                                     (mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1, 
00089                                     (mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, 
00090                                     mColors[colorIndex]);
00091         }
00092     }
00093 }
00094 
00095 /* 
00096 ======================================                                  
00097 Draw board
00098 
00099 Draw the two lines that delimit the board
00100 ====================================== 
00101 */
00102 void Game::DrawBoard ()
00103 {
00104     // Calculate the limits of the board in pixels  
00105     int mX1 = BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) - 1;
00106     int mX2 = BOARD_POSITION + (BLOCK_SIZE * (BOARD_WIDTH / 2));
00107     int mY = mScreenHeight - (BLOCK_SIZE * BOARD_HEIGHT);
00108     
00109     // Check that the vertical margin is not to small
00110     //assert (mY > MIN_VERTICAL_MARGIN);
00111 
00112     // Rectangles that delimits the board
00113     uLCD->filled_rectangle (mX1 - BOARD_LINE_WIDTH, mY, mX1, mScreenHeight - 1, BLUE);
00114     uLCD->filled_rectangle (mX2, mY, mX2 + BOARD_LINE_WIDTH, mScreenHeight - 1, BLUE);
00115     
00116     // Check that the horizontal margin is not to small
00117     //assert (mX1 > MIN_HORIZONTAL_MARGIN);
00118 
00119     // Drawing the blocks that are already stored in the board
00120     mX1 += 1;
00121     for (int i = 0; i < BOARD_WIDTH; i++)
00122     {
00123         for (int j = 0; j < BOARD_HEIGHT; j++)
00124         {   
00125             // Check if the block is filled, if so, draw it
00126             if (!mBoard->IsFreeBlock(i, j)) 
00127                 uLCD->filled_rectangle (    mX1 + i * BLOCK_SIZE, 
00128                                         mY + j * BLOCK_SIZE, 
00129                                         (mX1 + i * BLOCK_SIZE) + BLOCK_SIZE - 1, 
00130                                         (mY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, 
00131                                         10066329);
00132         }
00133     }   
00134 }
00135 
00136 /* 
00137 ======================================                                  
00138 Draw scene
00139 
00140 Draw all the objects of the scene
00141 ====================================== 
00142 */
00143 void Game::DrawScene ()
00144 {
00145     DrawBoard();
00146     DrawPiece (mPosX, mPosY, mPiece, mRotation, mPiece);                    // Draw the playing piece
00147     DrawPiece (mNextPosX, mNextPosY, mNextPiece, mNextRotation, mNextPiece);    // Draw the next piece
00148 }
00149 
00150 void Game::ErasePiece (int pX, int pY, int pPiece, int pRotation)
00151 {
00152     if(pPiece==-1) return;
00153     // Obtain the position in pixel in the screen of the block we want to draw
00154     int mPixelsX = mBoard->GetXPosInPixels (pX);
00155     int mPixelsY = mBoard->GetYPosInPixels (pY);
00156 
00157     // Travel the matrix of blocks of the piece and draw the blocks that are filled
00158     for (int i = 0; i < PIECE_BLOCKS; i++)
00159     {
00160         for (int j = 0; j < PIECE_BLOCKS; j++)
00161         {
00162             if (mPieces->GetBlockType (pPiece, pRotation, j, i) != 0)
00163                 uLCD->filled_rectangle  (mPixelsX + i * BLOCK_SIZE, 
00164                                     mPixelsY + j * BLOCK_SIZE, 
00165                                     (mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1, 
00166                                     (mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, 
00167                                     0);
00168         }
00169     }
00170 }
00171 
00172 void Game::AddPoints(int newGain)
00173 {
00174     points+=newGain;
00175 }
00176 
00177 void Game::AddClearedLines(int newGain)
00178 {
00179     clearedLineCount+=newGain;
00180 }
00181 
00182 int Game::GetPoints()
00183 {
00184     return points;
00185 }
00186 
00187 int Game::GetClearedLines()
00188 {
00189     return clearedLineCount;
00190 }