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
- Committer:
- el17ajf
- Date:
- 2019-04-03
- Revision:
- 18:24ce897024d0
- Parent:
- 17:cc448ab7266f
- Child:
- 19:370d83a8dc33
File content as of revision 18:24ce897024d0:
#include "Game.h" #include "Input.h" #include "Engine.h" #include "Menus.h" Game::Game(Difficulty difficulty) { currentTetromino = Tetromino::getTetrominoOfType( Tetromino::getRandomTetrominoType()); nextTetrominoType = Tetromino::getRandomTetrominoType(); switch (difficulty) { case EASY: move_frames = Engine::FPS / 6; // ~ 160ms break; case MEDIUM: move_frames = Engine::FPS / 9; // ~ 110ms break; case HARD: move_frames = Engine::FPS / 12; // ~ 80ms break; } frames = 0; last_move_frame = 0; start_x = Grid::GRID_WIDTH / 2 - 1; currentTetromino = currentTetromino.teleportedTo(start_x); } Game::~Game() { } void Game::update() { if (frames++ > last_move_frame + move_frames) { last_move_frame = frames; moveCurrentTetrominoDown(); } if (Input::buttonHit(Input::LEFT)) { if (grid.isSpaceForTetromino(currentTetromino.movedLeft())) { currentTetromino = currentTetromino.movedLeft(); } } if (Input::buttonHit(Input::RIGHT)) { if (grid.isSpaceForTetromino(currentTetromino.movedRight())) { currentTetromino = currentTetromino.movedRight(); } else { printf("NO SPACE TO THE RIGHT"); } } if (Input::buttonHit(Input::UP)) { if (grid.isSpaceForTetromino(currentTetromino.rotatedClockwise())) { currentTetromino = currentTetromino.rotatedClockwise(); } } if (Input::buttonHit(Input::DOWN)) { dropCurrentTetromino(); } } void Game::moveCurrentTetrominoDown() { if (grid.isSpaceForTetromino(currentTetromino.movedDown())) { currentTetromino = currentTetromino.movedDown(); } else { grid.placeTetromino(currentTetromino); currentTetromino = Tetromino::getTetrominoOfType(nextTetrominoType); // centre it currentTetromino = currentTetromino.teleportedTo(start_x); if (!grid.isSpaceForTetromino(currentTetromino)) { // no space for tetromino, game over gameOver(); } nextTetrominoType = Tetromino::getRandomTetrominoType(); } } void Game::dropCurrentTetromino() { while (grid.isSpaceForTetromino(currentTetromino.movedDown())) { currentTetromino = currentTetromino.movedDown(); } moveCurrentTetrominoDown(); } void Game::draw() { currentTetromino.draw(); grid.draw(); } void Game::gameOver() { Menus::add(Menus::GAME_OVER); }