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: mbed
Fork of el17ajf by
Game/Game.cpp@21:62d2b5b73160, 2019-04-03 (annotated)
- Committer:
- el17ajf
- Date:
- Wed Apr 03 20:33:27 2019 +0000
- Revision:
- 21:62d2b5b73160
- Parent:
- 19:370d83a8dc33
- Child:
- 22:0a474f074553
Moved Menus to Class based system
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17ajf | 4:aa433f9865a6 | 1 | #include "Game.h" |
el17ajf | 6:a54df561f442 | 2 | #include "Input.h" |
el17ajf | 18:24ce897024d0 | 3 | #include "Engine.h" |
el17ajf | 21:62d2b5b73160 | 4 | #include "MenuManager.h" |
el17ajf | 21:62d2b5b73160 | 5 | #include "GameOverMenu.h" |
el17ajf | 19:370d83a8dc33 | 6 | #include "Prefs.h" |
el17ajf | 2:0b5e289ef905 | 7 | |
el17ajf | 17:cc448ab7266f | 8 | Game::Game(Difficulty difficulty) { |
el17ajf | 7:2e37bad816cb | 9 | currentTetromino = Tetromino::getTetrominoOfType( |
el17ajf | 7:2e37bad816cb | 10 | Tetromino::getRandomTetrominoType()); |
el17ajf | 7:2e37bad816cb | 11 | nextTetrominoType = Tetromino::getRandomTetrominoType(); |
el17ajf | 17:cc448ab7266f | 12 | |
el17ajf | 17:cc448ab7266f | 13 | switch (difficulty) { |
el17ajf | 17:cc448ab7266f | 14 | case EASY: |
el17ajf | 18:24ce897024d0 | 15 | move_frames = Engine::FPS / 6; // ~ 160ms |
el17ajf | 19:370d83a8dc33 | 16 | scoreForRow = 6; |
el17ajf | 17:cc448ab7266f | 17 | break; |
el17ajf | 17:cc448ab7266f | 18 | case MEDIUM: |
el17ajf | 18:24ce897024d0 | 19 | move_frames = Engine::FPS / 9; // ~ 110ms |
el17ajf | 19:370d83a8dc33 | 20 | scoreForRow = 8; |
el17ajf | 17:cc448ab7266f | 21 | break; |
el17ajf | 17:cc448ab7266f | 22 | case HARD: |
el17ajf | 18:24ce897024d0 | 23 | move_frames = Engine::FPS / 12; // ~ 80ms |
el17ajf | 19:370d83a8dc33 | 24 | scoreForRow = 10; |
el17ajf | 17:cc448ab7266f | 25 | break; |
el17ajf | 17:cc448ab7266f | 26 | } |
el17ajf | 15:afeefa3ceb61 | 27 | frames = 0; |
el17ajf | 15:afeefa3ceb61 | 28 | last_move_frame = 0; |
el17ajf | 15:afeefa3ceb61 | 29 | start_x = Grid::GRID_WIDTH / 2 - 1; |
el17ajf | 15:afeefa3ceb61 | 30 | currentTetromino = currentTetromino.teleportedTo(start_x); |
el17ajf | 19:370d83a8dc33 | 31 | score = 0; |
el17ajf | 21:62d2b5b73160 | 32 | playerNumber = Prefs::getInstance()->getKey(Prefs::LAST_PLAYER) + 1; |
el17ajf | 2:0b5e289ef905 | 33 | } |
el17ajf | 2:0b5e289ef905 | 34 | |
el17ajf | 2:0b5e289ef905 | 35 | Game::~Game() { |
el17ajf | 6:a54df561f442 | 36 | |
el17ajf | 2:0b5e289ef905 | 37 | } |
el17ajf | 2:0b5e289ef905 | 38 | |
el17ajf | 2:0b5e289ef905 | 39 | void Game::update() { |
el17ajf | 15:afeefa3ceb61 | 40 | if (frames++ > last_move_frame + move_frames) { |
el17ajf | 15:afeefa3ceb61 | 41 | last_move_frame = frames; |
el17ajf | 15:afeefa3ceb61 | 42 | moveCurrentTetrominoDown(); |
el17ajf | 15:afeefa3ceb61 | 43 | } |
el17ajf | 15:afeefa3ceb61 | 44 | |
el17ajf | 9:3a7776a29a11 | 45 | if (Input::buttonHit(Input::LEFT)) { |
el17ajf | 7:2e37bad816cb | 46 | if (grid.isSpaceForTetromino(currentTetromino.movedLeft())) { |
el17ajf | 4:aa433f9865a6 | 47 | currentTetromino = currentTetromino.movedLeft(); |
el17ajf | 4:aa433f9865a6 | 48 | } |
el17ajf | 4:aa433f9865a6 | 49 | } |
el17ajf | 9:3a7776a29a11 | 50 | if (Input::buttonHit(Input::RIGHT)) { |
el17ajf | 9:3a7776a29a11 | 51 | if (grid.isSpaceForTetromino(currentTetromino.movedRight())) { |
el17ajf | 9:3a7776a29a11 | 52 | currentTetromino = currentTetromino.movedRight(); |
el17ajf | 9:3a7776a29a11 | 53 | } |
el17ajf | 9:3a7776a29a11 | 54 | } |
el17ajf | 9:3a7776a29a11 | 55 | if (Input::buttonHit(Input::UP)) { |
el17ajf | 9:3a7776a29a11 | 56 | if (grid.isSpaceForTetromino(currentTetromino.rotatedClockwise())) { |
el17ajf | 9:3a7776a29a11 | 57 | currentTetromino = currentTetromino.rotatedClockwise(); |
el17ajf | 9:3a7776a29a11 | 58 | } |
el17ajf | 9:3a7776a29a11 | 59 | } |
el17ajf | 9:3a7776a29a11 | 60 | if (Input::buttonHit(Input::DOWN)) { |
el17ajf | 9:3a7776a29a11 | 61 | dropCurrentTetromino(); |
el17ajf | 9:3a7776a29a11 | 62 | } |
el17ajf | 4:aa433f9865a6 | 63 | } |
el17ajf | 4:aa433f9865a6 | 64 | |
el17ajf | 19:370d83a8dc33 | 65 | void Game::addScore(int rowsCleared) { |
el17ajf | 19:370d83a8dc33 | 66 | score += scoreForRow * rowsCleared * rowsCleared; |
el17ajf | 19:370d83a8dc33 | 67 | if (score > 9999) { |
el17ajf | 19:370d83a8dc33 | 68 | score = 9999; |
el17ajf | 19:370d83a8dc33 | 69 | } |
el17ajf | 19:370d83a8dc33 | 70 | } |
el17ajf | 19:370d83a8dc33 | 71 | |
el17ajf | 7:2e37bad816cb | 72 | void Game::moveCurrentTetrominoDown() { |
el17ajf | 7:2e37bad816cb | 73 | if (grid.isSpaceForTetromino(currentTetromino.movedDown())) { |
el17ajf | 4:aa433f9865a6 | 74 | currentTetromino = currentTetromino.movedDown(); |
el17ajf | 4:aa433f9865a6 | 75 | } else { |
el17ajf | 19:370d83a8dc33 | 76 | int rowsCleared = grid.placeTetromino(currentTetromino); |
el17ajf | 19:370d83a8dc33 | 77 | if (rowsCleared != 0) { |
el17ajf | 19:370d83a8dc33 | 78 | addScore(rowsCleared); |
el17ajf | 19:370d83a8dc33 | 79 | } |
el17ajf | 7:2e37bad816cb | 80 | currentTetromino = Tetromino::getTetrominoOfType(nextTetrominoType); |
el17ajf | 15:afeefa3ceb61 | 81 | // centre it |
el17ajf | 15:afeefa3ceb61 | 82 | currentTetromino = currentTetromino.teleportedTo(start_x); |
el17ajf | 15:afeefa3ceb61 | 83 | if (!grid.isSpaceForTetromino(currentTetromino)) { |
el17ajf | 15:afeefa3ceb61 | 84 | // no space for tetromino, game over |
el17ajf | 15:afeefa3ceb61 | 85 | gameOver(); |
el17ajf | 15:afeefa3ceb61 | 86 | } |
el17ajf | 13:59e17cab320a | 87 | nextTetrominoType = Tetromino::getRandomTetrominoType(); |
el17ajf | 4:aa433f9865a6 | 88 | } |
el17ajf | 2:0b5e289ef905 | 89 | } |
el17ajf | 2:0b5e289ef905 | 90 | |
el17ajf | 7:2e37bad816cb | 91 | void Game::dropCurrentTetromino() { |
el17ajf | 9:3a7776a29a11 | 92 | while (grid.isSpaceForTetromino(currentTetromino.movedDown())) { |
el17ajf | 19:370d83a8dc33 | 93 | score += 1; |
el17ajf | 9:3a7776a29a11 | 94 | currentTetromino = currentTetromino.movedDown(); |
el17ajf | 9:3a7776a29a11 | 95 | } |
el17ajf | 9:3a7776a29a11 | 96 | moveCurrentTetrominoDown(); |
el17ajf | 7:2e37bad816cb | 97 | } |
el17ajf | 7:2e37bad816cb | 98 | |
el17ajf | 2:0b5e289ef905 | 99 | void Game::draw() { |
el17ajf | 7:2e37bad816cb | 100 | currentTetromino.draw(); |
el17ajf | 4:aa433f9865a6 | 101 | grid.draw(); |
el17ajf | 2:0b5e289ef905 | 102 | } |
el17ajf | 15:afeefa3ceb61 | 103 | |
el17ajf | 19:370d83a8dc33 | 104 | void Game::addScoreToLeaderboard() { |
el17ajf | 19:370d83a8dc33 | 105 | for (int i = 0; i < Prefs::HIGHSCORE3; i++) { |
el17ajf | 21:62d2b5b73160 | 106 | if (score > Prefs::getInstance()->getKey((Prefs::Key)i)) { |
el17ajf | 21:62d2b5b73160 | 107 | Prefs::getInstance()->setKey((Prefs::Key)i, score); |
el17ajf | 21:62d2b5b73160 | 108 | Prefs::getInstance()->setKey((Prefs::Key)(i + 3), playerNumber); |
el17ajf | 19:370d83a8dc33 | 109 | } |
el17ajf | 19:370d83a8dc33 | 110 | } |
el17ajf | 19:370d83a8dc33 | 111 | } |
el17ajf | 19:370d83a8dc33 | 112 | |
el17ajf | 15:afeefa3ceb61 | 113 | void Game::gameOver() { |
el17ajf | 19:370d83a8dc33 | 114 | addScoreToLeaderboard(); |
el17ajf | 21:62d2b5b73160 | 115 | Prefs::getInstance()->setKey(Prefs::LAST_PLAYER, playerNumber); |
el17ajf | 21:62d2b5b73160 | 116 | Menus::MenuManager::add(new Menus::GameOverMenu()); |
el17ajf | 15:afeefa3ceb61 | 117 | } |