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-12
- Revision:
- 28:e09b7ac11dea
- Parent:
- 27:2ed9e3c9f4e9
- Child:
- 29:d59fbe128d1f
File content as of revision 28:e09b7ac11dea:
#include "Game.h" #include "Graphics.h" #include "Input.h" #include "Engine.h" #include "MenuManager.h" #include "GameOverMenu.h" #include "PausedMenu.h" #include "Prefs.h" #include "Math.h" Game::Game(Difficulty difficulty) { currentTetromino = Tetromino::getTetrominoOfType( Tetromino::getRandomTetrominoType()); nextTetrominoType = Tetromino::getRandomTetrominoType(); switch (difficulty) { case EASY: move_frames = Engine::FPS / 6; // ~ 160ms scoreForRow = 6; break; case MEDIUM: move_frames = Engine::FPS / 9; // ~ 110ms scoreForRow = 8; break; case HARD: move_frames = Engine::FPS / 12; // ~ 80ms scoreForRow = 10; break; } frames = 0; last_move_frame = 0; start_x = Grid::GRID_WIDTH / 2 - 1; currentTetromino = currentTetromino.teleportedTo(start_x); score = 0; playerNumber = Prefs::getInstance()->getKey(Prefs::LAST_PLAYER); if (playerNumber == Prefs::EMPTY) { playerNumber = 1; } else { playerNumber += 1; } } 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(); } } if (Input::buttonHit(Input::UP)) { if (grid.isSpaceForTetromino(currentTetromino.rotatedClockwise())) { currentTetromino = currentTetromino.rotatedClockwise(); } } if (Input::buttonHit(Input::DOWN)) { dropCurrentTetromino(); } if (Input::buttonHit(Input::START)) { Menus::MenuManager::add(new Menus::PausedMenu()); } } void Game::addScore(int rowsCleared) { score += scoreForRow * rowsCleared * rowsCleared; move_frames = Math::lerp(move_frames, 1, (double)score/9999); if (score > 9999) { score = 9999; } } void Game::moveCurrentTetrominoDown() { if (grid.isSpaceForTetromino(currentTetromino.movedDown())) { currentTetromino = currentTetromino.movedDown(); } else { int rowsCleared = grid.placeTetromino(currentTetromino); if (rowsCleared != 0) { addScore(rowsCleared); } 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())) { score += 1; currentTetromino = currentTetromino.movedDown(); } moveCurrentTetrominoDown(); Graphics::Game::shake(3); } void Game::draw() { currentTetromino.draw(); grid.draw(); } void Game::gameOver() { printf("Player %i finished game with %i points\n", playerNumber, score); Prefs::getInstance()->setKey(Prefs::LAST_PLAYER, playerNumber); Prefs::getInstance()->setKey(Prefs::LAST_SCORE, score); Prefs::getInstance()->printAll(); printf("WRITTEN DATA TO WHATEVER THING\n"); printf("UPDATING LEADERBOARD\n"); updateLeaderboard(); Prefs::getInstance()->printAll(); printf("DONE UPDATING LEADERBOARD\n"); Menus::MenuManager::add(new Menus::GameOverMenu()); } void Game::updateLeaderboard() { for (int i = 0; i <= Prefs::HIGHSCORE3; i++) { if (score > Prefs::getInstance()->getKey((Prefs::Key)i)) { // move all scores down for (int j = Prefs::HIGHSCORE3 - 1; j >= i; j--) { Prefs::getInstance()->setKey( (Prefs::Key)(j + 1), Prefs::getInstance()->getKey((Prefs::Key)i) ); Prefs::getInstance()->setKey( (Prefs::Key)((j + 1) + 3), Prefs::getInstance()->getKey((Prefs::Key)(i + 3)) ); } // set ith score to the current score Prefs::getInstance()->setKey((Prefs::Key)i, score); Prefs::getInstance()->setKey((Prefs::Key)(i + 3), playerNumber); break; } } }