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@18:24ce897024d0, 2019-04-03 (annotated)
- Committer:
- el17ajf
- Date:
- Wed Apr 03 14:31:49 2019 +0000
- Revision:
- 18:24ce897024d0
- Parent:
- 17:cc448ab7266f
- Child:
- 19:370d83a8dc33
Added adaptable grid sizes
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 | 17:cc448ab7266f | 4 | #include "Menus.h" |
el17ajf | 2:0b5e289ef905 | 5 | |
el17ajf | 17:cc448ab7266f | 6 | Game::Game(Difficulty difficulty) { |
el17ajf | 7:2e37bad816cb | 7 | currentTetromino = Tetromino::getTetrominoOfType( |
el17ajf | 7:2e37bad816cb | 8 | Tetromino::getRandomTetrominoType()); |
el17ajf | 7:2e37bad816cb | 9 | nextTetrominoType = Tetromino::getRandomTetrominoType(); |
el17ajf | 17:cc448ab7266f | 10 | |
el17ajf | 17:cc448ab7266f | 11 | switch (difficulty) { |
el17ajf | 17:cc448ab7266f | 12 | case EASY: |
el17ajf | 18:24ce897024d0 | 13 | move_frames = Engine::FPS / 6; // ~ 160ms |
el17ajf | 17:cc448ab7266f | 14 | break; |
el17ajf | 17:cc448ab7266f | 15 | case MEDIUM: |
el17ajf | 18:24ce897024d0 | 16 | move_frames = Engine::FPS / 9; // ~ 110ms |
el17ajf | 17:cc448ab7266f | 17 | break; |
el17ajf | 17:cc448ab7266f | 18 | case HARD: |
el17ajf | 18:24ce897024d0 | 19 | move_frames = Engine::FPS / 12; // ~ 80ms |
el17ajf | 17:cc448ab7266f | 20 | break; |
el17ajf | 17:cc448ab7266f | 21 | } |
el17ajf | 17:cc448ab7266f | 22 | |
el17ajf | 15:afeefa3ceb61 | 23 | frames = 0; |
el17ajf | 15:afeefa3ceb61 | 24 | last_move_frame = 0; |
el17ajf | 15:afeefa3ceb61 | 25 | start_x = Grid::GRID_WIDTH / 2 - 1; |
el17ajf | 15:afeefa3ceb61 | 26 | currentTetromino = currentTetromino.teleportedTo(start_x); |
el17ajf | 2:0b5e289ef905 | 27 | } |
el17ajf | 2:0b5e289ef905 | 28 | |
el17ajf | 2:0b5e289ef905 | 29 | Game::~Game() { |
el17ajf | 6:a54df561f442 | 30 | |
el17ajf | 2:0b5e289ef905 | 31 | } |
el17ajf | 2:0b5e289ef905 | 32 | |
el17ajf | 2:0b5e289ef905 | 33 | void Game::update() { |
el17ajf | 15:afeefa3ceb61 | 34 | if (frames++ > last_move_frame + move_frames) { |
el17ajf | 15:afeefa3ceb61 | 35 | last_move_frame = frames; |
el17ajf | 15:afeefa3ceb61 | 36 | moveCurrentTetrominoDown(); |
el17ajf | 15:afeefa3ceb61 | 37 | } |
el17ajf | 15:afeefa3ceb61 | 38 | |
el17ajf | 9:3a7776a29a11 | 39 | if (Input::buttonHit(Input::LEFT)) { |
el17ajf | 7:2e37bad816cb | 40 | if (grid.isSpaceForTetromino(currentTetromino.movedLeft())) { |
el17ajf | 4:aa433f9865a6 | 41 | currentTetromino = currentTetromino.movedLeft(); |
el17ajf | 4:aa433f9865a6 | 42 | } |
el17ajf | 4:aa433f9865a6 | 43 | } |
el17ajf | 9:3a7776a29a11 | 44 | if (Input::buttonHit(Input::RIGHT)) { |
el17ajf | 9:3a7776a29a11 | 45 | if (grid.isSpaceForTetromino(currentTetromino.movedRight())) { |
el17ajf | 9:3a7776a29a11 | 46 | currentTetromino = currentTetromino.movedRight(); |
el17ajf | 13:59e17cab320a | 47 | } else { |
el17ajf | 13:59e17cab320a | 48 | printf("NO SPACE TO THE RIGHT"); |
el17ajf | 9:3a7776a29a11 | 49 | } |
el17ajf | 9:3a7776a29a11 | 50 | } |
el17ajf | 9:3a7776a29a11 | 51 | if (Input::buttonHit(Input::UP)) { |
el17ajf | 9:3a7776a29a11 | 52 | if (grid.isSpaceForTetromino(currentTetromino.rotatedClockwise())) { |
el17ajf | 9:3a7776a29a11 | 53 | currentTetromino = currentTetromino.rotatedClockwise(); |
el17ajf | 9:3a7776a29a11 | 54 | } |
el17ajf | 9:3a7776a29a11 | 55 | } |
el17ajf | 9:3a7776a29a11 | 56 | if (Input::buttonHit(Input::DOWN)) { |
el17ajf | 9:3a7776a29a11 | 57 | dropCurrentTetromino(); |
el17ajf | 9:3a7776a29a11 | 58 | } |
el17ajf | 4:aa433f9865a6 | 59 | } |
el17ajf | 4:aa433f9865a6 | 60 | |
el17ajf | 7:2e37bad816cb | 61 | void Game::moveCurrentTetrominoDown() { |
el17ajf | 7:2e37bad816cb | 62 | if (grid.isSpaceForTetromino(currentTetromino.movedDown())) { |
el17ajf | 4:aa433f9865a6 | 63 | currentTetromino = currentTetromino.movedDown(); |
el17ajf | 4:aa433f9865a6 | 64 | } else { |
el17ajf | 7:2e37bad816cb | 65 | grid.placeTetromino(currentTetromino); |
el17ajf | 7:2e37bad816cb | 66 | currentTetromino = Tetromino::getTetrominoOfType(nextTetrominoType); |
el17ajf | 15:afeefa3ceb61 | 67 | // centre it |
el17ajf | 15:afeefa3ceb61 | 68 | currentTetromino = currentTetromino.teleportedTo(start_x); |
el17ajf | 15:afeefa3ceb61 | 69 | if (!grid.isSpaceForTetromino(currentTetromino)) { |
el17ajf | 15:afeefa3ceb61 | 70 | // no space for tetromino, game over |
el17ajf | 15:afeefa3ceb61 | 71 | gameOver(); |
el17ajf | 15:afeefa3ceb61 | 72 | } |
el17ajf | 13:59e17cab320a | 73 | nextTetrominoType = Tetromino::getRandomTetrominoType(); |
el17ajf | 4:aa433f9865a6 | 74 | } |
el17ajf | 2:0b5e289ef905 | 75 | } |
el17ajf | 2:0b5e289ef905 | 76 | |
el17ajf | 7:2e37bad816cb | 77 | void Game::dropCurrentTetromino() { |
el17ajf | 9:3a7776a29a11 | 78 | while (grid.isSpaceForTetromino(currentTetromino.movedDown())) { |
el17ajf | 9:3a7776a29a11 | 79 | currentTetromino = currentTetromino.movedDown(); |
el17ajf | 9:3a7776a29a11 | 80 | } |
el17ajf | 9:3a7776a29a11 | 81 | moveCurrentTetrominoDown(); |
el17ajf | 7:2e37bad816cb | 82 | } |
el17ajf | 7:2e37bad816cb | 83 | |
el17ajf | 2:0b5e289ef905 | 84 | void Game::draw() { |
el17ajf | 7:2e37bad816cb | 85 | currentTetromino.draw(); |
el17ajf | 4:aa433f9865a6 | 86 | grid.draw(); |
el17ajf | 2:0b5e289ef905 | 87 | } |
el17ajf | 15:afeefa3ceb61 | 88 | |
el17ajf | 15:afeefa3ceb61 | 89 | void Game::gameOver() { |
el17ajf | 17:cc448ab7266f | 90 | Menus::add(Menus::GAME_OVER); |
el17ajf | 15:afeefa3ceb61 | 91 | } |