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
Game/Game.cpp
- Committer:
- el17cd
- Date:
- 2019-04-03
- Revision:
- 25:3995271e411c
- Parent:
- 22:236319885874
- Child:
- 26:8a85aede976d
File content as of revision 25:3995271e411c:
#include "Game.h" Game::Game(){ noOfCubes = 25; //How many cubes are in the scene homeSelection = 0; //default selection for the home screen gamepad.init(); renderer.init(); for(int i = 0; i < noOfCubes; i++){ //Set initial position of all cubes cubeArray[i].translate(rand()%250-125,0,20+ i*10); //Position will be based on random integers } } void Game::run(){ backToMenu = false; score = 0; deathMenuSelection = playing = true; //set game state to playing and death screen selection to 'restart' while(!backToMenu) { renderer.clear(); //clear the screen coord = gamepad.get_coord(); //get coordinates from the joystick renderer.drawHorizon(coord.x/15); //draw the horizon line for (int c = 0; c< noOfCubes; c++){ moveCubes(&cubeArray[c]); //translate cubes depending on joystick location and score cubeToBeRendered(&cubeArray[c], c); //add all cube faces to array to later be rendered checkDespawn(&cubeArray[c]); //check if any cubes are behind the perspective checkDeath(&cubeArray[c]); //check if any cubes are too close to user } renderer.drawAllFaces(faceArray, noOfCubes, coord.x); //draw all faces added to the face array displayDeathMenu(); //display death menu if cube is too close to user, add score if not renderer.printScore(score); //print score on top left of screen renderer.refresh(); //update display } } void Game::checkDespawn(Cube *cube){ //checks if the cube is behind the perspective if (cube->despawn()){ cube->resetPos(); //reset position to origin cube->translate(rand()%250-125,0,140); //translate cube to new far away position } } void Game::checkDeath(Cube *cube){ //Check whether the user has hit a cube if (cube->tooClose()){ playing = false; //set state of game to not playing cube->resetPos(); cube->translate(rand()%250-125,0,140); //reset and translate cube to back position } } void Game::cubeToBeRendered(Cube *cube, int cubeIndex){ //Adds all faces within the cube to an array of faces to be rendered for (int i = 0; i < 6; i++){ faceArray[cubeIndex*6 + i] = cube->getFace(i); } } void Game::moveCubes(Cube *cube){ //animate cubes if(playing){ if(score < 2000) cube->translate(-coord.x*1.4f,0,-1-(float)score/1000); //move cubes closer to user and in x axis depending on joystick pos else{ cube->translate(-coord.x*1.4f,0,-3); //once max speed reached maintain constant z speed } } else{ coord.x = 0; //if not playing (dead) prevent joystick moving anything } } void Game::displayDeathMenu(){ //display restart or back to home menu if(!playing){ renderer.drawDeathScreen(deathMenuSelection); //draw death screen if game over deathButtonSelections() //select menu option } else{ addScore(); //add score if still playing } } void Game::addScore(){ //increment score by 1 each frame score++; } void Game::resetScore(){ //reset score to 0 score = 0; } void Game::deathButtonSelections(){ //determine selection on death screen if(gamepad.check_event(Gamepad::Y_PRESSED) == true){ //if y pressed highlight top option (restart) deathMenuSelection = true; } else if(gamepad.check_event(Gamepad::A_PRESSED) == true){ //if a pressed highlight bottom option (home menu) deathMenuSelection = false; } if (deathMenuSelection == true && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if top option highlighted and b pressed then restart game playing = true; score = 0; resetScore(); } else if(deathMenuSelection == false && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom option highlighted and b pressed then go to home screen resetScore(); backToMenu = true; } } void Game::homeButtonSelections(){ //determine selection on home screen if(gamepad.check_event(Gamepad::Y_PRESSED) == true && homeSelection > 0){ //if top option isnt highlighted and y pressed then move highlight up homeSelection--; } else if(gamepad.check_event(Gamepad::A_PRESSED) == true && homeSelection < 2){ //if bottom option isnt highlighted and a pressed then move highlight down homeSelection++; } if (homeSelection == 0 && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if top highlighted and b pressed then start game run(); } else if(homeSelection == 2 && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom highlighted and b pressed then exit game //return true; } } void Game::homeScreen(){ while(1){ renderer.clear();//clear screen homeButtonSelections(); //determine selection on home screen renderer.drawHomeScreen(homeSelection); //draw home screen renderer.refresh(); //refresh lcd } }