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@20:3ca430241df0, 2019-04-01 (annotated)
- Committer:
- el17cd
- Date:
- Mon Apr 01 17:11:40 2019 +0000
- Revision:
- 20:3ca430241df0
- Parent:
- 19:ec4cb22accb0
- Child:
- 21:6b5d2d75e083
Made renderer sort all faces by z axis and draw instead of being done in game class
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 | 18:8256546a3cbf | 3 | Serial pc(USBTX, USBRX); // tx, rx |
el17cd | 15:8fbbdefbe720 | 4 | |
el17cd | 19:ec4cb22accb0 | 5 | |
el17cd | 15:8fbbdefbe720 | 6 | Game::Game(){ |
el17cd | 20:3ca430241df0 | 7 | noOfCubes = 10; |
el17cd | 18:8256546a3cbf | 8 | homeSelection = 0; |
el17cd | 15:8fbbdefbe720 | 9 | gamepad.init(); |
el17cd | 15:8fbbdefbe720 | 10 | renderer.init(); |
el17cd | 18:8256546a3cbf | 11 | timer.start(); |
el17cd | 20:3ca430241df0 | 12 | for(int i = 0; i < noOfCubes; i++){ |
el17cd | 20:3ca430241df0 | 13 | cubeArray[i].translate(rand()%100-50,0,20+ i*10); |
el17cd | 15:8fbbdefbe720 | 14 | } |
el17cd | 15:8fbbdefbe720 | 15 | } |
el17cd | 15:8fbbdefbe720 | 16 | |
el17cd | 15:8fbbdefbe720 | 17 | void Game::run(){ |
el17cd | 18:8256546a3cbf | 18 | score = 0; |
el17cd | 18:8256546a3cbf | 19 | selection = true; |
el17cd | 18:8256546a3cbf | 20 | playing = true; |
el17cd | 19:ec4cb22accb0 | 21 | timer.reset(); |
el17cd | 20:3ca430241df0 | 22 | |
el17cd | 15:8fbbdefbe720 | 23 | while(1) { |
el17cd | 15:8fbbdefbe720 | 24 | Vector2D coord = gamepad.get_coord(); |
el17cd | 15:8fbbdefbe720 | 25 | renderer.clear(); |
el17cd | 15:8fbbdefbe720 | 26 | renderer.drawHorizon(coord.x/15); |
el17cd | 20:3ca430241df0 | 27 | //pc.printf("a"); |
el17cd | 20:3ca430241df0 | 28 | for (int c = 0; c< noOfCubes; c++){ |
el17cd | 15:8fbbdefbe720 | 29 | if(playing){ |
el17cd | 18:8256546a3cbf | 30 | if(score < 1500) |
el17cd | 20:3ca430241df0 | 31 | cubeArray[c].translate((float)-coord.x*1.4,0,-2-(float)score/500); |
el17cd | 16:64cd7bc094f9 | 32 | else{ |
el17cd | 20:3ca430241df0 | 33 | cubeArray[c].translate((float)-coord.x*1.4,0,-3.5); |
el17cd | 16:64cd7bc094f9 | 34 | } |
el17cd | 15:8fbbdefbe720 | 35 | } |
el17cd | 20:3ca430241df0 | 36 | else{ |
el17cd | 20:3ca430241df0 | 37 | coord.x = 0; |
el17cd | 20:3ca430241df0 | 38 | coord.y = 0; |
el17cd | 20:3ca430241df0 | 39 | } |
el17cd | 15:8fbbdefbe720 | 40 | for (int i = 0; i < 6; i++){ |
el17cd | 20:3ca430241df0 | 41 | faceArray[c*6 + i] = cubeArray[c].getFace(i); |
el17cd | 15:8fbbdefbe720 | 42 | } |
el17cd | 20:3ca430241df0 | 43 | if (cubeArray[c].despawn()){ |
el17cd | 20:3ca430241df0 | 44 | cubeArray[c].resetPos(); |
el17cd | 20:3ca430241df0 | 45 | cubeArray[c].translate(rand()%100-50,0,90); |
el17cd | 20:3ca430241df0 | 46 | } |
el17cd | 20:3ca430241df0 | 47 | if (cubeArray[c].tooClose()){ |
el17cd | 15:8fbbdefbe720 | 48 | playing = false; |
el17cd | 20:3ca430241df0 | 49 | cubeArray[c].resetPos(); |
el17cd | 20:3ca430241df0 | 50 | cubeArray[c].translate(rand()%100-50,0,90); |
el17cd | 15:8fbbdefbe720 | 51 | } |
el17cd | 15:8fbbdefbe720 | 52 | } |
el17cd | 20:3ca430241df0 | 53 | |
el17cd | 20:3ca430241df0 | 54 | renderer.drawAllFaces(faceArray, noOfCubes, coord.x);//faceArray, noOfCubes, coord.x); |
el17cd | 20:3ca430241df0 | 55 | |
el17cd | 18:8256546a3cbf | 56 | if(playing){ |
el17cd | 18:8256546a3cbf | 57 | score = timer.read_ms()/200; |
el17cd | 18:8256546a3cbf | 58 | } |
el17cd | 18:8256546a3cbf | 59 | else{ |
el17cd | 16:64cd7bc094f9 | 60 | deathScreen(); |
el17cd | 18:8256546a3cbf | 61 | if(deathButtonSelections()){ |
el17cd | 18:8256546a3cbf | 62 | break; |
el17cd | 18:8256546a3cbf | 63 | } |
el17cd | 16:64cd7bc094f9 | 64 | } |
el17cd | 20:3ca430241df0 | 65 | |
el17cd | 18:8256546a3cbf | 66 | |
el17cd | 20:3ca430241df0 | 67 | char buf[5]; |
el17cd | 15:8fbbdefbe720 | 68 | sprintf(buf, "%d", score); |
el17cd | 16:64cd7bc094f9 | 69 | renderer.print(buf, 0, 0); |
el17cd | 15:8fbbdefbe720 | 70 | memset(buf, 0, sizeof buf); |
el17cd | 15:8fbbdefbe720 | 71 | renderer.refresh(); |
el17cd | 16:64cd7bc094f9 | 72 | |
el17cd | 20:3ca430241df0 | 73 | wait_ms(1000/28); |
el17cd | 15:8fbbdefbe720 | 74 | } |
el17cd | 15:8fbbdefbe720 | 75 | } |
el17cd | 18:8256546a3cbf | 76 | |
el17cd | 18:8256546a3cbf | 77 | bool Game::deathButtonSelections(){ |
el17cd | 18:8256546a3cbf | 78 | if(gamepad.check_event(Gamepad::Y_PRESSED) == true){ |
el17cd | 18:8256546a3cbf | 79 | selection = true; |
el17cd | 18:8256546a3cbf | 80 | } |
el17cd | 18:8256546a3cbf | 81 | else if(gamepad.check_event(Gamepad::A_PRESSED) == true){ |
el17cd | 18:8256546a3cbf | 82 | selection = false; |
el17cd | 18:8256546a3cbf | 83 | } |
el17cd | 18:8256546a3cbf | 84 | if (selection == true && gamepad.check_event(Gamepad::B_PRESSED) == true){ |
el17cd | 18:8256546a3cbf | 85 | playing = true; |
el17cd | 18:8256546a3cbf | 86 | score = 0; |
el17cd | 18:8256546a3cbf | 87 | timer.reset(); |
el17cd | 18:8256546a3cbf | 88 | } |
el17cd | 18:8256546a3cbf | 89 | else if(selection == false && gamepad.check_event(Gamepad::B_PRESSED) == true){ |
el17cd | 18:8256546a3cbf | 90 | timer.reset(); |
el17cd | 18:8256546a3cbf | 91 | return true; |
el17cd | 18:8256546a3cbf | 92 | } |
el17cd | 18:8256546a3cbf | 93 | return false; |
el17cd | 18:8256546a3cbf | 94 | } |
el17cd | 18:8256546a3cbf | 95 | |
el17cd | 18:8256546a3cbf | 96 | void Game::homeButtonSelections(){ |
el17cd | 18:8256546a3cbf | 97 | if(gamepad.check_event(Gamepad::Y_PRESSED) == true && homeSelection > 0){ |
el17cd | 18:8256546a3cbf | 98 | homeSelection--; |
el17cd | 18:8256546a3cbf | 99 | } |
el17cd | 18:8256546a3cbf | 100 | else if(gamepad.check_event(Gamepad::A_PRESSED) == true && homeSelection < 2){ |
el17cd | 18:8256546a3cbf | 101 | homeSelection++; |
el17cd | 18:8256546a3cbf | 102 | } |
el17cd | 18:8256546a3cbf | 103 | if (homeSelection == 0 && gamepad.check_event(Gamepad::B_PRESSED) == true){ |
el17cd | 18:8256546a3cbf | 104 | run(); |
el17cd | 18:8256546a3cbf | 105 | } |
el17cd | 18:8256546a3cbf | 106 | else if(homeSelection == 2 && gamepad.check_event(Gamepad::B_PRESSED) == true){ |
el17cd | 18:8256546a3cbf | 107 | //return true; |
el17cd | 18:8256546a3cbf | 108 | } |
el17cd | 18:8256546a3cbf | 109 | } |
el17cd | 16:64cd7bc094f9 | 110 | |
el17cd | 16:64cd7bc094f9 | 111 | void Game::deathScreen(){ |
el17cd | 16:64cd7bc094f9 | 112 | //int selection = 1; |
el17cd | 16:64cd7bc094f9 | 113 | renderer.drawDeathScreen(selection); |
el17cd | 16:64cd7bc094f9 | 114 | } |
el17cd | 18:8256546a3cbf | 115 | |
el17cd | 18:8256546a3cbf | 116 | void Game::homeScreen(){ |
el17cd | 18:8256546a3cbf | 117 | while(1){ |
el17cd | 18:8256546a3cbf | 118 | renderer.clear(); |
el17cd | 18:8256546a3cbf | 119 | homeButtonSelections(); |
el17cd | 18:8256546a3cbf | 120 | renderer.drawHomeScreen(homeSelection); |
el17cd | 18:8256546a3cbf | 121 | renderer.refresh(); |
el17cd | 20:3ca430241df0 | 122 | wait_ms(1000/28); |
el17cd | 18:8256546a3cbf | 123 | } |
el17cd | 18:8256546a3cbf | 124 | } |
el17cd | 15:8fbbdefbe720 | 125 |