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@25:3995271e411c, 2019-04-03 (annotated)
- Committer:
- el17cd
- Date:
- Wed Apr 03 20:03:47 2019 +0000
- Revision:
- 25:3995271e411c
- Parent:
- 22:236319885874
- Child:
- 26:8a85aede976d
amended cube.cpp and documented cube.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17cd | 15:8fbbdefbe720 | 1 | #include "Game.h" |
el17cd | 19:ec4cb22accb0 | 2 | |
el17cd | 15:8fbbdefbe720 | 3 | Game::Game(){ |
el17cd | 25:3995271e411c | 4 | noOfCubes = 25; //How many cubes are in the scene |
el17cd | 25:3995271e411c | 5 | homeSelection = 0; //default selection for the home screen |
el17cd | 15:8fbbdefbe720 | 6 | gamepad.init(); |
el17cd | 15:8fbbdefbe720 | 7 | renderer.init(); |
el17cd | 25:3995271e411c | 8 | for(int i = 0; i < noOfCubes; i++){ //Set initial position of all cubes |
el17cd | 25:3995271e411c | 9 | cubeArray[i].translate(rand()%250-125,0,20+ i*10); //Position will be based on random integers |
el17cd | 15:8fbbdefbe720 | 10 | } |
el17cd | 15:8fbbdefbe720 | 11 | } |
el17cd | 15:8fbbdefbe720 | 12 | |
el17cd | 15:8fbbdefbe720 | 13 | void Game::run(){ |
el17cd | 25:3995271e411c | 14 | backToMenu = false; |
el17cd | 18:8256546a3cbf | 15 | score = 0; |
el17cd | 25:3995271e411c | 16 | deathMenuSelection = playing = true; //set game state to playing and death screen selection to 'restart' |
el17cd | 25:3995271e411c | 17 | while(!backToMenu) { |
el17cd | 25:3995271e411c | 18 | renderer.clear(); //clear the screen |
el17cd | 25:3995271e411c | 19 | coord = gamepad.get_coord(); //get coordinates from the joystick |
el17cd | 25:3995271e411c | 20 | renderer.drawHorizon(coord.x/15); //draw the horizon line |
el17cd | 22:236319885874 | 21 | |
el17cd | 20:3ca430241df0 | 22 | for (int c = 0; c< noOfCubes; c++){ |
el17cd | 25:3995271e411c | 23 | moveCubes(&cubeArray[c]); //translate cubes depending on joystick location and score |
el17cd | 25:3995271e411c | 24 | cubeToBeRendered(&cubeArray[c], c); //add all cube faces to array to later be rendered |
el17cd | 25:3995271e411c | 25 | checkDespawn(&cubeArray[c]); //check if any cubes are behind the perspective |
el17cd | 25:3995271e411c | 26 | checkDeath(&cubeArray[c]); //check if any cubes are too close to user |
el17cd | 15:8fbbdefbe720 | 27 | } |
el17cd | 20:3ca430241df0 | 28 | |
el17cd | 25:3995271e411c | 29 | renderer.drawAllFaces(faceArray, noOfCubes, coord.x); //draw all faces added to the face array |
el17cd | 25:3995271e411c | 30 | displayDeathMenu(); //display death menu if cube is too close to user, add score if not |
el17cd | 25:3995271e411c | 31 | renderer.printScore(score); //print score on top left of screen |
el17cd | 25:3995271e411c | 32 | renderer.refresh(); //update display |
el17cd | 25:3995271e411c | 33 | } |
el17cd | 25:3995271e411c | 34 | } |
el17cd | 25:3995271e411c | 35 | |
el17cd | 25:3995271e411c | 36 | void Game::checkDespawn(Cube *cube){ //checks if the cube is behind the perspective |
el17cd | 25:3995271e411c | 37 | if (cube->despawn()){ |
el17cd | 25:3995271e411c | 38 | cube->resetPos(); //reset position to origin |
el17cd | 25:3995271e411c | 39 | cube->translate(rand()%250-125,0,140); //translate cube to new far away position |
el17cd | 25:3995271e411c | 40 | } |
el17cd | 25:3995271e411c | 41 | } |
el17cd | 25:3995271e411c | 42 | |
el17cd | 25:3995271e411c | 43 | void Game::checkDeath(Cube *cube){ //Check whether the user has hit a cube |
el17cd | 25:3995271e411c | 44 | if (cube->tooClose()){ |
el17cd | 25:3995271e411c | 45 | playing = false; //set state of game to not playing |
el17cd | 25:3995271e411c | 46 | cube->resetPos(); |
el17cd | 25:3995271e411c | 47 | cube->translate(rand()%250-125,0,140); //reset and translate cube to back position |
el17cd | 15:8fbbdefbe720 | 48 | } |
el17cd | 15:8fbbdefbe720 | 49 | } |
el17cd | 18:8256546a3cbf | 50 | |
el17cd | 25:3995271e411c | 51 | void Game::cubeToBeRendered(Cube *cube, int cubeIndex){ //Adds all faces within the cube to an array of faces to be rendered |
el17cd | 25:3995271e411c | 52 | for (int i = 0; i < 6; i++){ |
el17cd | 25:3995271e411c | 53 | faceArray[cubeIndex*6 + i] = cube->getFace(i); |
el17cd | 25:3995271e411c | 54 | } |
el17cd | 25:3995271e411c | 55 | } |
el17cd | 25:3995271e411c | 56 | |
el17cd | 25:3995271e411c | 57 | void Game::moveCubes(Cube *cube){ //animate cubes |
el17cd | 25:3995271e411c | 58 | if(playing){ |
el17cd | 25:3995271e411c | 59 | if(score < 2000) |
el17cd | 25:3995271e411c | 60 | cube->translate(-coord.x*1.4f,0,-1-(float)score/1000); //move cubes closer to user and in x axis depending on joystick pos |
el17cd | 25:3995271e411c | 61 | else{ |
el17cd | 25:3995271e411c | 62 | cube->translate(-coord.x*1.4f,0,-3); //once max speed reached maintain constant z speed |
el17cd | 25:3995271e411c | 63 | } |
el17cd | 25:3995271e411c | 64 | } |
el17cd | 25:3995271e411c | 65 | else{ |
el17cd | 25:3995271e411c | 66 | coord.x = 0; //if not playing (dead) prevent joystick moving anything |
el17cd | 25:3995271e411c | 67 | } |
el17cd | 25:3995271e411c | 68 | } |
el17cd | 25:3995271e411c | 69 | |
el17cd | 25:3995271e411c | 70 | void Game::displayDeathMenu(){ //display restart or back to home menu |
el17cd | 25:3995271e411c | 71 | if(!playing){ |
el17cd | 25:3995271e411c | 72 | renderer.drawDeathScreen(deathMenuSelection); //draw death screen if game over |
el17cd | 25:3995271e411c | 73 | deathButtonSelections() //select menu option |
el17cd | 25:3995271e411c | 74 | } |
el17cd | 25:3995271e411c | 75 | else{ |
el17cd | 25:3995271e411c | 76 | addScore(); //add score if still playing |
el17cd | 25:3995271e411c | 77 | } |
el17cd | 25:3995271e411c | 78 | } |
el17cd | 25:3995271e411c | 79 | |
el17cd | 25:3995271e411c | 80 | void Game::addScore(){ //increment score by 1 each frame |
el17cd | 21:6b5d2d75e083 | 81 | score++; |
el17cd | 21:6b5d2d75e083 | 82 | } |
el17cd | 21:6b5d2d75e083 | 83 | |
el17cd | 25:3995271e411c | 84 | void Game::resetScore(){ //reset score to 0 |
el17cd | 21:6b5d2d75e083 | 85 | score = 0; |
el17cd | 21:6b5d2d75e083 | 86 | } |
el17cd | 22:236319885874 | 87 | |
el17cd | 25:3995271e411c | 88 | void Game::deathButtonSelections(){ //determine selection on death screen |
el17cd | 25:3995271e411c | 89 | if(gamepad.check_event(Gamepad::Y_PRESSED) == true){ //if y pressed highlight top option (restart) |
el17cd | 25:3995271e411c | 90 | deathMenuSelection = true; |
el17cd | 18:8256546a3cbf | 91 | } |
el17cd | 25:3995271e411c | 92 | else if(gamepad.check_event(Gamepad::A_PRESSED) == true){ //if a pressed highlight bottom option (home menu) |
el17cd | 25:3995271e411c | 93 | deathMenuSelection = false; |
el17cd | 18:8256546a3cbf | 94 | } |
el17cd | 25:3995271e411c | 95 | if (deathMenuSelection == true && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if top option highlighted and b pressed then restart game |
el17cd | 18:8256546a3cbf | 96 | playing = true; |
el17cd | 18:8256546a3cbf | 97 | score = 0; |
el17cd | 21:6b5d2d75e083 | 98 | resetScore(); |
el17cd | 18:8256546a3cbf | 99 | } |
el17cd | 25:3995271e411c | 100 | else if(deathMenuSelection == false && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom option highlighted and b pressed then go to home screen |
el17cd | 21:6b5d2d75e083 | 101 | resetScore(); |
el17cd | 25:3995271e411c | 102 | backToMenu = true; |
el17cd | 18:8256546a3cbf | 103 | } |
el17cd | 18:8256546a3cbf | 104 | } |
el17cd | 18:8256546a3cbf | 105 | |
el17cd | 25:3995271e411c | 106 | void Game::homeButtonSelections(){ //determine selection on home screen |
el17cd | 25:3995271e411c | 107 | if(gamepad.check_event(Gamepad::Y_PRESSED) == true && homeSelection > 0){ //if top option isnt highlighted and y pressed then move highlight up |
el17cd | 18:8256546a3cbf | 108 | homeSelection--; |
el17cd | 18:8256546a3cbf | 109 | } |
el17cd | 25:3995271e411c | 110 | else if(gamepad.check_event(Gamepad::A_PRESSED) == true && homeSelection < 2){ //if bottom option isnt highlighted and a pressed then move highlight down |
el17cd | 18:8256546a3cbf | 111 | homeSelection++; |
el17cd | 18:8256546a3cbf | 112 | } |
el17cd | 25:3995271e411c | 113 | if (homeSelection == 0 && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if top highlighted and b pressed then start game |
el17cd | 18:8256546a3cbf | 114 | run(); |
el17cd | 18:8256546a3cbf | 115 | } |
el17cd | 25:3995271e411c | 116 | else if(homeSelection == 2 && gamepad.check_event(Gamepad::B_PRESSED) == true){ //if bottom highlighted and b pressed then exit game |
el17cd | 18:8256546a3cbf | 117 | //return true; |
el17cd | 18:8256546a3cbf | 118 | } |
el17cd | 18:8256546a3cbf | 119 | } |
el17cd | 18:8256546a3cbf | 120 | |
el17cd | 18:8256546a3cbf | 121 | void Game::homeScreen(){ |
el17cd | 18:8256546a3cbf | 122 | while(1){ |
el17cd | 25:3995271e411c | 123 | renderer.clear();//clear screen |
el17cd | 25:3995271e411c | 124 | homeButtonSelections(); //determine selection on home screen |
el17cd | 25:3995271e411c | 125 | renderer.drawHomeScreen(homeSelection); //draw home screen |
el17cd | 25:3995271e411c | 126 | renderer.refresh(); //refresh lcd |
el17cd | 18:8256546a3cbf | 127 | } |
el17cd | 18:8256546a3cbf | 128 | } |
el17cd | 15:8fbbdefbe720 | 129 |