Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed
tetris.cpp
00001 #include "mbed.h" 00002 #include "Game.h" 00003 #include "globals.h" 00004 00005 00006 int SinglePiecePoints = 50; 00007 int LineClearPoints = 200; 00008 int key_input = 0; 00009 bool gameStarted = false; 00010 void input_left() 00011 { 00012 if(!gameStarted) 00013 { gameStarted=true; 00014 return; 00015 } 00016 key_input = 1; 00017 } 00018 00019 void input_right() 00020 { 00021 if(!gameStarted) 00022 { gameStarted=true; 00023 return; 00024 } 00025 key_input = 2; 00026 } 00027 00028 void input_rotate() 00029 { 00030 if(!gameStarted) 00031 { gameStarted=true; 00032 return; 00033 } 00034 key_input = 3; 00035 } 00036 00037 void input_down() 00038 { 00039 if(!gameStarted) 00040 { gameStarted=true; 00041 return; 00042 } 00043 key_input=4; 00044 } 00045 00046 void input_menu() 00047 { 00048 if(!gameStarted) 00049 { gameStarted=true; 00050 return; 00051 } 00052 key_input = 5; 00053 } 00054 00055 void clear_board() 00056 { 00057 uLCD.filled_rectangle(20,0,79,128,0); 00058 } 00059 00060 void clear_next_piece() 00061 { 00062 uLCD.filled_rectangle(92,20,122,50,0); 00063 } 00064 00065 void UpdateGameStatus(int points) // used to include int lines 00066 { 00067 uLCD.locate(13,12); 00068 uLCD.printf("Score"); 00069 uLCD.locate(13,13); 00070 uLCD.printf("%d",points); 00071 } 00072 00073 00074 00075 void tetrisGame() 00076 { 00077 PRINTF("POINT 1"); 00078 while (game1 == false) { 00079 00080 PRINTF("[TETRIS] Idle\r\n"); 00081 Thread::wait(500); 00082 } 00083 00084 00085 uLCD.text_width(2); 00086 uLCD.text_height(2); 00087 uLCD.color(WHITE); 00088 uLCD.locate(1,0); 00089 uLCD.printf("TETRIS"); 00090 Thread::wait(2000); 00091 00092 uLCD.baudrate(3000000); 00093 00094 00095 bool isGameOver = false; 00096 int mScreenHeight = 128; 00097 Pieces mPieces; 00098 Board mBoard (&mPieces, mScreenHeight); 00099 int a = 1; // RandomGen('a') 00100 int b = 2; 00101 int c = 3; 00102 int d = 4; 00103 Game mGame (&mBoard, &mPieces, mScreenHeight, &uLCD,a,b,c,d); 00104 00105 00106 // ----- Main Loop ----- 00107 int prevX=0; 00108 int prevY=0; 00109 int prevPiece=-1; 00110 int prevRot=0; 00111 Timer timer; 00112 timer.start(); 00113 key_input=0; 00114 bool needErase = false; 00115 uLCD.cls(); 00116 00117 00118 int piece = 0; 00119 int rotate = 0; 00120 00121 //float x = 0.0; 00122 00123 Rgb = 1; 00124 rGb = 0; 00125 rgB = 0; 00126 int count = 0; 00127 00128 UpdateGameStatus(mGame.GetPoints()); 00129 00130 while (1) 00131 { 00132 PRINTF("POINT 2"); 00133 // Wait until the game starts 00134 while (game1 == false) { 00135 00136 PRINTF("[TETRIS] Idle\r\n"); 00137 Thread::wait(500); 00138 } 00139 00140 if (myNav.up()) input_rotate(); 00141 if (myNav.down()) input_down(); 00142 if (myNav.left()) input_left(); 00143 if (myNav.right()) input_right(); 00144 //if (myNav.fire()) input_menu(); 00145 00146 if(isGameOver) 00147 { 00148 Thread::wait(1000); 00149 uLCD.cls(); 00150 uLCD.text_width(2); 00151 uLCD.text_height(2); 00152 uLCD.printf("GAME OVER"); 00153 Thread::wait(2000); 00154 uLCD.cls(); 00155 00156 //isGameOver = false; 00157 00158 Thread::wait(1000); 00159 00160 mBoard.InitBoard(); 00161 uLCD.cls(); 00162 00163 mGame.points = 0; 00164 count = 0; 00165 00166 //mGame.InitGame(); 00167 isGameOver = false; 00168 00169 menu_flag = 0; 00170 00171 game1 = false; 00172 00173 00174 //mBoard.InitBoard(); 00175 00176 //free(mPieces); 00177 //free(mBoard); 00178 //free(mGame); 00179 00180 } 00181 // ----- Draw ---- 00182 if(needErase) 00183 { 00184 mGame.ErasePiece(prevX,prevY,prevPiece,prevRot); 00185 needErase=false; 00186 } 00187 mGame.DrawScene(); 00188 00189 prevX=mGame.mPosX; 00190 prevY=mGame.mPosY; 00191 prevPiece=mGame.mPiece; 00192 prevRot=mGame.mRotation; 00193 00194 // ----- Input ----- 00195 switch (key_input) 00196 { 00197 case (2): //right 00198 { 00199 if (mBoard.IsPossibleMovement (mGame.mPosX + 1, mGame.mPosY, mGame.mPiece, mGame.mRotation)) 00200 {mGame.mPosX++;needErase=true;} 00201 break; 00202 } 00203 00204 case (1): //left 00205 { 00206 if (mBoard.IsPossibleMovement (mGame.mPosX - 1, mGame.mPosY, mGame.mPiece, mGame.mRotation)) 00207 {mGame.mPosX--;needErase=true;} 00208 break; 00209 } 00210 00211 case (4)://down 00212 { 00213 // Check collision from up to down 00214 while (mBoard.IsPossibleMovement(mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation)) { mGame.mPosY++; } 00215 needErase=true; 00216 mBoard.StorePiece (mGame.mPosX, mGame.mPosY - 1, mGame.mPiece, mGame.mRotation); 00217 Rgb = 1; 00218 rGb = 1; 00219 rgB = 1; 00220 Thread::wait(100); 00221 Rgb = 0; 00222 rGb = 0; 00223 rgB = 0; 00224 mGame.AddPoints(SinglePiecePoints); 00225 int linesDeleted = mBoard.DeletePossibleLines (); 00226 if(linesDeleted>0) 00227 { 00228 mGame.AddClearedLines(linesDeleted); 00229 mGame.AddPoints(LineClearPoints*linesDeleted); 00230 clear_board(); 00231 } 00232 UpdateGameStatus(mGame.GetPoints()); // FIX 00233 00234 if (mBoard.IsGameOver()) 00235 { 00236 isGameOver=true; 00237 uLCD.cls(); 00238 00239 } 00240 00241 if(!isGameOver) 00242 { 00243 mGame.CreateNewPiece(piece,rotate); 00244 clear_next_piece(); 00245 00246 piece++; 00247 rotate++; 00248 00249 if (piece==7) piece = 0; 00250 if (rotate==4) rotate = 0; 00251 } 00252 break; 00253 } 00254 00255 case (3)://rotate 00256 { 00257 if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY, mGame.mPiece, (mGame.mRotation + 1) % 4)) 00258 {mGame.mRotation = (mGame.mRotation + 1) % 4;needErase=true;} 00259 break; 00260 } 00261 00262 case (5)://menu 00263 { 00264 Thread::wait(1000); 00265 uLCD.cls(); 00266 uLCD.text_width(2); 00267 uLCD.text_height(2); 00268 uLCD.printf("MENU"); 00269 Thread::wait(2000); 00270 uLCD.cls(); 00271 00272 Thread::wait(1000); // FIX? 00273 00274 menu_flag = 0; 00275 00276 game1 = false; // FIX? put after Thread::wait() 00277 00278 00279 } 00280 00281 case (0):{break;} 00282 } 00283 key_input = 0; 00284 00285 // ----- Vertical movement ----- 00286 00287 if(timer.read_ms()>WAIT_TIME) 00288 { 00289 needErase=true; 00290 if(!isGameOver) 00291 { 00292 if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY + 1, mGame.mPiece, mGame.mRotation)) 00293 { 00294 mGame.mPosY++; 00295 } 00296 else 00297 { 00298 mBoard.StorePiece (mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation); 00299 mGame.AddPoints(SinglePiecePoints); 00300 int linesDeleted = mBoard.DeletePossibleLines (); 00301 if(linesDeleted>0) 00302 { 00303 mGame.AddClearedLines(linesDeleted); 00304 mGame.AddPoints(LineClearPoints*linesDeleted); 00305 clear_board(); 00306 } 00307 UpdateGameStatus(mGame.GetPoints()); 00308 00309 if (mBoard.IsGameOver()) 00310 { 00311 isGameOver=true; 00312 uLCD.cls(); 00313 } 00314 00315 if(!isGameOver) 00316 { 00317 mGame.CreateNewPiece(piece,rotate); 00318 clear_next_piece(); // 0-6 Pieces, 0-3 rotations^^ 00319 piece++; 00320 rotate++; 00321 00322 if (piece==7) piece = 0; 00323 if (rotate==4) rotate = 0; 00324 } 00325 } 00326 } 00327 timer.reset(); 00328 } 00329 Thread::wait(100); 00330 00331 //get a new random number for PWM 00332 //x = rand() / float(RAND_MAX); 00333 //add some exponential brightness scaling 00334 //for more of a fast flash effect 00335 /*x = x*x*x; 00336 Rgb = x; 00337 rGb = x; 00338 rgB = x; 00339 //fast update rate for welding flashes 00340 //Thread::wait(20); 00341 //add a random pause between welds 00342 if (rand() / float(RAND_MAX) > 0.95) { 00343 Rgb = 0; 00344 rGb = 0; 00345 rgB = 0; 00346 //Thread::wait(4000.0 * rand() / float(RAND_MAX)); 00347 }*/ 00348 // if (count % 10 == 0) { 00349 // if (Rgb == 1) { 00350 // Rgb = 0; 00351 // rGb = 1; 00352 // rgB = 0; 00353 // } 00354 // 00355 // else if (rGb == 1) { 00356 // Rgb = 0; 00357 // rGb = 0; 00358 // rgB = 1; 00359 // } 00360 // else if (rgB == 1) { 00361 // Rgb = 1; 00362 // rGb = 0; 00363 // rgB = 0; 00364 // } else if (Rgb == 0) { 00365 // Rgb = 0; 00366 // rGb = 1; 00367 // rgB = 0; 00368 // } 00369 // } 00370 // 00371 // count++; 00372 } 00373 }
Generated on Fri Dec 9 2022 21:28:44 by
1.7.2