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@17:cc448ab7266f, 2019-04-03 (annotated)
- Committer:
- el17ajf
- Date:
- Wed Apr 03 13:37:56 2019 +0000
- Revision:
- 17:cc448ab7266f
- Parent:
- 15:afeefa3ceb61
- Child:
- 18:24ce897024d0
Added UI and graphics separation
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 | 17:cc448ab7266f | 3 | #include "Menus.h" |
el17ajf | 2:0b5e289ef905 | 4 | |
el17ajf | 15:afeefa3ceb61 | 5 | /* BUGS: |
el17ajf | 15:afeefa3ceb61 | 6 | * |
el17ajf | 15:afeefa3ceb61 | 7 | * SWITCH BOUNCE |
el17ajf | 15:afeefa3ceb61 | 8 | * NO DOUBLE ROW CLEAR |
el17ajf | 15:afeefa3ceb61 | 9 | * |
el17ajf | 15:afeefa3ceb61 | 10 | */ |
el17ajf | 15:afeefa3ceb61 | 11 | |
el17ajf | 17:cc448ab7266f | 12 | Game::Game(Difficulty difficulty) { |
el17ajf | 7:2e37bad816cb | 13 | currentTetromino = Tetromino::getTetrominoOfType( |
el17ajf | 7:2e37bad816cb | 14 | Tetromino::getRandomTetrominoType()); |
el17ajf | 7:2e37bad816cb | 15 | nextTetrominoType = Tetromino::getRandomTetrominoType(); |
el17ajf | 17:cc448ab7266f | 16 | |
el17ajf | 17:cc448ab7266f | 17 | switch (difficulty) { |
el17ajf | 17:cc448ab7266f | 18 | case EASY: |
el17ajf | 17:cc448ab7266f | 19 | move_frames = 8; |
el17ajf | 17:cc448ab7266f | 20 | break; |
el17ajf | 17:cc448ab7266f | 21 | case MEDIUM: |
el17ajf | 17:cc448ab7266f | 22 | move_frames = 6; |
el17ajf | 17:cc448ab7266f | 23 | break; |
el17ajf | 17:cc448ab7266f | 24 | case HARD: |
el17ajf | 17:cc448ab7266f | 25 | move_frames = 3; |
el17ajf | 17:cc448ab7266f | 26 | break; |
el17ajf | 17:cc448ab7266f | 27 | } |
el17ajf | 17:cc448ab7266f | 28 | |
el17ajf | 15:afeefa3ceb61 | 29 | frames = 0; |
el17ajf | 15:afeefa3ceb61 | 30 | last_move_frame = 0; |
el17ajf | 15:afeefa3ceb61 | 31 | start_x = Grid::GRID_WIDTH / 2 - 1; |
el17ajf | 15:afeefa3ceb61 | 32 | currentTetromino = currentTetromino.teleportedTo(start_x); |
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 | 13:59e17cab320a | 53 | } else { |
el17ajf | 13:59e17cab320a | 54 | printf("NO SPACE TO THE RIGHT"); |
el17ajf | 9:3a7776a29a11 | 55 | } |
el17ajf | 9:3a7776a29a11 | 56 | } |
el17ajf | 9:3a7776a29a11 | 57 | if (Input::buttonHit(Input::UP)) { |
el17ajf | 9:3a7776a29a11 | 58 | if (grid.isSpaceForTetromino(currentTetromino.rotatedClockwise())) { |
el17ajf | 9:3a7776a29a11 | 59 | currentTetromino = currentTetromino.rotatedClockwise(); |
el17ajf | 9:3a7776a29a11 | 60 | } |
el17ajf | 9:3a7776a29a11 | 61 | } |
el17ajf | 9:3a7776a29a11 | 62 | if (Input::buttonHit(Input::DOWN)) { |
el17ajf | 9:3a7776a29a11 | 63 | dropCurrentTetromino(); |
el17ajf | 9:3a7776a29a11 | 64 | } |
el17ajf | 4:aa433f9865a6 | 65 | } |
el17ajf | 4:aa433f9865a6 | 66 | |
el17ajf | 7:2e37bad816cb | 67 | void Game::moveCurrentTetrominoDown() { |
el17ajf | 7:2e37bad816cb | 68 | if (grid.isSpaceForTetromino(currentTetromino.movedDown())) { |
el17ajf | 4:aa433f9865a6 | 69 | currentTetromino = currentTetromino.movedDown(); |
el17ajf | 4:aa433f9865a6 | 70 | } else { |
el17ajf | 7:2e37bad816cb | 71 | grid.placeTetromino(currentTetromino); |
el17ajf | 7:2e37bad816cb | 72 | currentTetromino = Tetromino::getTetrominoOfType(nextTetrominoType); |
el17ajf | 15:afeefa3ceb61 | 73 | // centre it |
el17ajf | 15:afeefa3ceb61 | 74 | currentTetromino = currentTetromino.teleportedTo(start_x); |
el17ajf | 15:afeefa3ceb61 | 75 | if (!grid.isSpaceForTetromino(currentTetromino)) { |
el17ajf | 15:afeefa3ceb61 | 76 | // no space for tetromino, game over |
el17ajf | 15:afeefa3ceb61 | 77 | gameOver(); |
el17ajf | 15:afeefa3ceb61 | 78 | } |
el17ajf | 13:59e17cab320a | 79 | nextTetrominoType = Tetromino::getRandomTetrominoType(); |
el17ajf | 4:aa433f9865a6 | 80 | } |
el17ajf | 2:0b5e289ef905 | 81 | } |
el17ajf | 2:0b5e289ef905 | 82 | |
el17ajf | 7:2e37bad816cb | 83 | void Game::dropCurrentTetromino() { |
el17ajf | 9:3a7776a29a11 | 84 | while (grid.isSpaceForTetromino(currentTetromino.movedDown())) { |
el17ajf | 9:3a7776a29a11 | 85 | currentTetromino = currentTetromino.movedDown(); |
el17ajf | 9:3a7776a29a11 | 86 | } |
el17ajf | 9:3a7776a29a11 | 87 | moveCurrentTetrominoDown(); |
el17ajf | 7:2e37bad816cb | 88 | } |
el17ajf | 7:2e37bad816cb | 89 | |
el17ajf | 2:0b5e289ef905 | 90 | void Game::draw() { |
el17ajf | 7:2e37bad816cb | 91 | currentTetromino.draw(); |
el17ajf | 4:aa433f9865a6 | 92 | grid.draw(); |
el17ajf | 2:0b5e289ef905 | 93 | } |
el17ajf | 15:afeefa3ceb61 | 94 | |
el17ajf | 15:afeefa3ceb61 | 95 | void Game::gameOver() { |
el17ajf | 17:cc448ab7266f | 96 | Menus::add(Menus::GAME_OVER); |
el17ajf | 15:afeefa3ceb61 | 97 | } |