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:
- 26:8a85aede976d
- Parent:
- 25:3995271e411c
- Child:
- 27:e46af658c67a
diff -r 3995271e411c -r 8a85aede976d Game/Game.cpp --- a/Game/Game.cpp Wed Apr 03 20:03:47 2019 +0000 +++ b/Game/Game.cpp Thu Apr 04 19:03:06 2019 +0000 @@ -1,38 +1,57 @@ #include "Game.h" +Serial pc(USBTX, USBRX); // tx, rx + Game::Game(){ noOfCubes = 25; //How many cubes are in the scene homeSelection = 0; //default selection for the home screen gamepad.init(); renderer.init(); + resetScene(); + selectDisable = false; + input.x = 0; + input.bCooldown = input.yCooldown = input.aCooldown = false; +} + +void Game::resetScene(){ 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 + cubeArray[i].translate(rand()%250-125,0,10+ i*5); //Position will be based on random integers } } void Game::run(){ - backToMenu = false; + backToMenu = true; 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 + deathMenuSelection = true; + playing = true; //set game state to playing and death screen selection to 'restart' + while(true) { + processInput(); + renderer.clear(); + if(backToMenu){ + homeScreen(); + } + else{ + play(); } - - 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 + renderer.refresh(); //refresh lcd } } +void Game::play(){ + renderer.drawHorizon(input.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, input.x); //draw all faces added to the face array + displayDeathMenu(); //display death menu if cube is too close to user + addScore(); + renderer.printScore(score); //print score on top left of screen +} + void Game::checkDespawn(Cube *cube){ //checks if the cube is behind the perspective if (cube->despawn()){ cube->resetPos(); //reset position to origin @@ -57,9 +76,9 @@ 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 + cube->translate(-input.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 + cube->translate(-input.x*1.4f,0,-3); //once max speed reached maintain constant z speed } } else{ @@ -70,15 +89,15 @@ 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 + deathButtonSelections(); //select menu option } - else{ - addScore(); //add score if still playing - } + } void Game::addScore(){ //increment score by 1 each frame - score++; + if(playing){ + score++; //add score if still playing + } } void Game::resetScore(){ //reset score to 0 @@ -86,44 +105,108 @@ } void Game::deathButtonSelections(){ //determine selection on death screen - if(gamepad.check_event(Gamepad::Y_PRESSED) == true){ //if y pressed highlight top option (restart) + if(input.yButton){ //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) + else if(input.aButton){ //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 + if (deathMenuSelection && input.bButton){ //if top option highlighted and b pressed then restart game playing = true; score = 0; resetScore(); + resetScene(); } - else if(deathMenuSelection == false && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom option highlighted and b pressed then go to home screen + else if(!deathMenuSelection && input.bButton){ //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 + if(input.yButton && 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 + else if(input.aButton && 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(); + if (input.bButton && homeSelection == 0){ //if top highlighted and b pressed then start game + backToMenu = false; } - else if(homeSelection == 2 && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom highlighted and b pressed then exit game - //return true; + else if(input.bButton && homeSelection == 1){ //if bottom highlighted and b pressed then exit game + renderer.drawHelp(); + } + else if(input.bButton && homeSelection == 2){ //if bottom highlighted and b pressed then exit game + renderer.turnOff(); + exit(0); } } 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 + homeButtonSelections(); //determine selection on home screen + //if(helpScreen > -1) { + // + //} + //else{ + // renderer.drawHomeScreen(homeSelection); //draw home screen + //} +} + +void Game::processInput(){ + input.x = gamepad.get_coord().x; + bool y = gamepad.check_event(Gamepad::Y_PRESSED); + bool a = gamepad.check_event(Gamepad::A_PRESSED); + bool b = gamepad.check_event(Gamepad::B_PRESSED); + + disableYButton(y); + disableAButton(a); + disableBButton(b); +} + +void Game::disableYButton(bool y){ + if(!input.yCooldown && y){ + input.yCooldown = true; + disableY.attach(this, &Game::enableY, 0.3); + input.yButton = true; + } + else{ + input.yButton = false; } } - \ No newline at end of file + +void Game::disableAButton(bool a){ + if(!input.aCooldown && a){ + input.aCooldown = true; + disableA.attach(this, &Game::enableA, 0.3); + input.aButton = true; + } + else{ + input.aButton = false; + } +} + +void Game::disableBButton(bool b){ + if(!input.bCooldown && b){ + input.bCooldown = true; + disableB.attach(this, &Game::enableB, 0.3); + input.bButton = true; + } + else{ + input.bButton = false; + } +} + +void Game::enableA(){ + disableA.detach(); + input.aCooldown = false; +} + +void Game::enableB(){ + disableB.detach(); + input.bCooldown = false; +} + +void Game::enableY(){ + disableY.detach(); + input.yCooldown = false; +} \ No newline at end of file