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