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
Diff: Game/Game.cpp
- Revision:
- 25:3995271e411c
- Parent:
- 22:236319885874
- Child:
- 26:8a85aede976d
--- a/Game/Game.cpp Wed Apr 03 16:19:05 2019 +0000 +++ b/Game/Game.cpp Wed Apr 03 20:03:47 2019 +0000 @@ -1,126 +1,129 @@ #include "Game.h" Game::Game(){ - noOfCubes = 25; - homeSelection = 0; + 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++){ - cubeArray[i].translate(rand()%250-125,0,20+ i*10); + 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; - selection = true; - playing = true; - - while(1) { - renderer.clear(); - coord = gamepad.get_coord(); - - renderer.drawHorizon(coord.x/15); + 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++){ - if(playing){ - if(score < 2000) - cubeArray[c].translate(-coord.x*1.4f,0,-1-(float)score/1000); - else{ - cubeArray[c].translate(-coord.x*1.4f,0,-3); - } - } - else{ - coord.x = 0; - } - for (int i = 0; i < 6; i++){ - faceArray[c*6 + i] = cubeArray[c].getFace(i); - } - if (cubeArray[c].despawn()){ - cubeArray[c].resetPos(); - cubeArray[c].translate(rand()%250-125,0,140); - } - if (cubeArray[c].tooClose()){ - playing = false; - cubeArray[c].resetPos(); - cubeArray[c].translate(rand()%250-125,0,140); - } + 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); - - if(!playing){ - deathScreen(); - if(deathButtonSelections()){ - break; - } - } - else{ - addScore(); - } - - - char buf[5]; - sprintf(buf, "%d", score/3); - renderer.print(buf, 0, 0); - memset(buf, 0, sizeof buf); - renderer.refresh(); - wait_ms(1000/60); + 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::addScore(){ +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(){ +void Game::resetScore(){ //reset score to 0 score = 0; } -bool Game::deathButtonSelections(){ - if(gamepad.check_event(Gamepad::Y_PRESSED) == true){ - selection = true; +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){ - selection = false; + else if(gamepad.check_event(Gamepad::A_PRESSED) == true){ //if a pressed highlight bottom option (home menu) + deathMenuSelection = false; } - if (selection == true && gamepad.check_event(Gamepad::B_PRESSED) == true){ + 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(selection == false && gamepad.check_event(Gamepad::B_PRESSED) == true){ + else if(deathMenuSelection == false && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom option highlighted and b pressed then go to home screen resetScore(); - return true; + backToMenu = true; } - return false; } -void Game::homeButtonSelections(){ - if(gamepad.check_event(Gamepad::Y_PRESSED) == true && homeSelection > 0){ +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){ + 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 (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){ + else if(homeSelection == 2 && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom highlighted and b pressed then exit game //return true; } } - -void Game::deathScreen(){ - renderer.drawDeathScreen(selection); -} void Game::homeScreen(){ while(1){ - renderer.clear(); - homeButtonSelections(); - renderer.drawHomeScreen(homeSelection); - renderer.refresh(); - wait_ms(1000/60); + renderer.clear();//clear screen + homeButtonSelections(); //determine selection on home screen + renderer.drawHomeScreen(homeSelection); //draw home screen + renderer.refresh(); //refresh lcd } } \ No newline at end of file