SharpShooter
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
MainGame.cpp@10:92538c02e6c8, 2016-10-28 (annotated)
- Committer:
- SeanBuckingham
- Date:
- Fri Oct 28 19:02:28 2016 +0000
- Revision:
- 10:92538c02e6c8
- Parent:
- 8:56a24df93680
- Child:
- 11:55b65415b6ba
latest version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jboettcher | 0:137546fb5da1 | 1 | #include "mbed.h" |
jboettcher | 0:137546fb5da1 | 2 | #include "Speaker.h" |
jboettcher | 0:137546fb5da1 | 3 | #include "uLCD_4DGL.h" |
jboettcher | 0:137546fb5da1 | 4 | #include "SDFileSystem.h" |
jboettcher | 0:137546fb5da1 | 5 | #include "wave_player.h" |
jboettcher | 1:8a3fa9e90572 | 6 | #include "Nav_Switch.h" |
jboettcher | 1:8a3fa9e90572 | 7 | #include "rtos.h" |
jboettcher | 0:137546fb5da1 | 8 | |
SeanBuckingham | 10:92538c02e6c8 | 9 | DigitalOut myled1(LED1); |
SeanBuckingham | 10:92538c02e6c8 | 10 | DigitalOut myled2(LED2); |
SeanBuckingham | 10:92538c02e6c8 | 11 | DigitalOut myled3(LED3); |
SeanBuckingham | 10:92538c02e6c8 | 12 | DigitalOut myled4(LED4); |
SeanBuckingham | 10:92538c02e6c8 | 13 | |
SeanBuckingham | 10:92538c02e6c8 | 14 | ///sd/wavfiles/def_swar.wav |
SeanBuckingham | 10:92538c02e6c8 | 15 | |
SeanBuckingham | 8:56a24df93680 | 16 | /*INSTANTIATION*/ |
jboettcher | 0:137546fb5da1 | 17 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
jboettcher | 1:8a3fa9e90572 | 18 | Nav_Switch myNav( p24, p25, p26, p27, p28); //up, down, left, right, fire |
SeanBuckingham | 8:56a24df93680 | 19 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
jboettcher | 0:137546fb5da1 | 20 | AnalogOut DACout(p18); |
jboettcher | 0:137546fb5da1 | 21 | wave_player waver(&DACout); |
jboettcher | 0:137546fb5da1 | 22 | |
SeanBuckingham | 10:92538c02e6c8 | 23 | /*STRUCT INITIALIZATION*/ |
SeanBuckingham | 10:92538c02e6c8 | 24 | struct ObstLocation { |
SeanBuckingham | 10:92538c02e6c8 | 25 | int x1, y1, x2, y2; |
SeanBuckingham | 10:92538c02e6c8 | 26 | } |
SeanBuckingham | 10:92538c02e6c8 | 27 | |
SeanBuckingham | 10:92538c02e6c8 | 28 | struct TargetLocation { |
SeanBuckingham | 10:92538c02e6c8 | 29 | int x,y,r; |
SeanBuckingham | 10:92538c02e6c8 | 30 | } |
SeanBuckingham | 10:92538c02e6c8 | 31 | |
SeanBuckingham | 10:92538c02e6c8 | 32 | struct BulletLocation { |
SeanBuckingham | 10:92538c02e6c8 | 33 | int x, topY, bottomY; |
SeanBuckingham | 10:92538c02e6c8 | 34 | } |
SeanBuckingham | 10:92538c02e6c8 | 35 | |
SeanBuckingham | 8:56a24df93680 | 36 | /*LOCAL VARIABLES*/ |
SeanBuckingham | 8:56a24df93680 | 37 | int numTries; |
SeanBuckingham | 8:56a24df93680 | 38 | int levelNum; |
SeanBuckingham | 10:92538c02e6c8 | 39 | BulletLocation bulletLocation; |
SeanBuckingham | 10:92538c02e6c8 | 40 | ObstLocation * obstaclePtr; |
SeanBuckingham | 10:92538c02e6c8 | 41 | TargetLocation * targetsPtr = new TargetLocation[3]; |
SeanBuckingham | 10:92538c02e6c8 | 42 | |
SeanBuckingham | 10:92538c02e6c8 | 43 | |
SeanBuckingham | 8:56a24df93680 | 44 | |
SeanBuckingham | 8:56a24df93680 | 45 | void startGame() { |
SeanBuckingham | 8:56a24df93680 | 46 | uLCD.locate(34,34); |
SeanBuckingham | 8:56a24df93680 | 47 | uLCD.set_font_size(4, 4); |
SeanBuckingham | 8:56a24df93680 | 48 | uLCD.printf("\nSharp Shooter!!\n"); |
SeanBuckingham | 10:92538c02e6c8 | 49 | wait(0.1); |
SeanBuckingham | 8:56a24df93680 | 50 | |
SeanBuckingham | 8:56a24df93680 | 51 | FILE *wave_file; |
SeanBuckingham | 10:92538c02e6c8 | 52 | wave_file=fopen("/sd/wavfiles/pitfall.wav","r"); |
SeanBuckingham | 8:56a24df93680 | 53 | waver.play(wave_file); |
SeanBuckingham | 8:56a24df93680 | 54 | fclose(wave_file); |
jboettcher | 0:137546fb5da1 | 55 | } |
jboettcher | 0:137546fb5da1 | 56 | |
SeanBuckingham | 10:92538c02e6c8 | 57 | void initializeLevel(int level) { |
SeanBuckingham | 10:92538c02e6c8 | 58 | levelNum = level; |
SeanBuckingham | 10:92538c02e6c8 | 59 | numTries = 10; |
SeanBuckingham | 10:92538c02e6c8 | 60 | //draw header (level #, numTries) at the top |
SeanBuckingham | 10:92538c02e6c8 | 61 | |
SeanBuckingham | 10:92538c02e6c8 | 62 | |
SeanBuckingham | 10:92538c02e6c8 | 63 | createTargets(); |
SeanBuckingham | 10:92538c02e6c8 | 64 | createObstacles(level); |
SeanBuckingham | 10:92538c02e6c8 | 65 | createShooter(); |
SeanBuckingham | 10:92538c02e6c8 | 66 | } |
SeanBuckingham | 10:92538c02e6c8 | 67 | |
SeanBuckingham | 10:92538c02e6c8 | 68 | void createTargets() { |
SeanBuckingham | 10:92538c02e6c8 | 69 | //draw all targets |
SeanBuckingham | 10:92538c02e6c8 | 70 | //initialize targetPtr w locations of where theyre drawn |
SeanBuckingham | 10:92538c02e6c8 | 71 | } |
SeanBuckingham | 10:92538c02e6c8 | 72 | |
SeanBuckingham | 10:92538c02e6c8 | 73 | void createShooter() { |
SeanBuckingham | 10:92538c02e6c8 | 74 | |
SeanBuckingham | 10:92538c02e6c8 | 75 | } |
SeanBuckingham | 10:92538c02e6c8 | 76 | |
SeanBuckingham | 10:92538c02e6c8 | 77 | void createObstacles(int num) { |
SeanBuckingham | 10:92538c02e6c8 | 78 | Obstacle obsArr[num]; |
SeanBuckingham | 10:92538c02e6c8 | 79 | obstaclePtr = new ObstLocation[num]; |
SeanBuckingham | 10:92538c02e6c8 | 80 | //dummy x values, CHANGE LATER |
SeanBuckingham | 10:92538c02e6c8 | 81 | if (num == 1) { |
SeanBuckingham | 10:92538c02e6c8 | 82 | obsArr[0] = new Obstacle(15, 15, 115, 115, uLCD); |
SeanBuckingham | 10:92538c02e6c8 | 83 | } else if (num == 2) { |
SeanBuckingham | 10:92538c02e6c8 | 84 | obsArr[0] = new Obstacle(15, 15, 115, 115, uLCD); |
SeanBuckingham | 10:92538c02e6c8 | 85 | obsArr[1] = new Obstacle(15, 15, 115, 115, uLCD); |
SeanBuckingham | 10:92538c02e6c8 | 86 | } else if (num == 3) { |
SeanBuckingham | 10:92538c02e6c8 | 87 | obsArr[0] = new Obstacle(15, 15, 115, 115, uLCD); |
SeanBuckingham | 10:92538c02e6c8 | 88 | obsArr[1] = new Obstacle(15, 15, 115, 115, uLCD); |
SeanBuckingham | 10:92538c02e6c8 | 89 | obsArr[2] = new Obstacle(15, 15, 115, 115, uLCD); |
SeanBuckingham | 10:92538c02e6c8 | 90 | } |
SeanBuckingham | 10:92538c02e6c8 | 91 | for (int i = 0; i < num; i++) { |
SeanBuckingham | 10:92538c02e6c8 | 92 | obstaclePtr[i] = obsArr[i].getLocation(); |
SeanBuckingham | 10:92538c02e6c8 | 93 | } |
SeanBuckingham | 10:92538c02e6c8 | 94 | } |
SeanBuckingham | 10:92538c02e6c8 | 95 | |
SeanBuckingham | 10:92538c02e6c8 | 96 | void shoot() { |
SeanBuckingham | 10:92538c02e6c8 | 97 | |
SeanBuckingham | 10:92538c02e6c8 | 98 | } |
SeanBuckingham | 8:56a24df93680 | 99 | |
SeanBuckingham | 8:56a24df93680 | 100 | void checkIfHit(); |
SeanBuckingham | 8:56a24df93680 | 101 | |
SeanBuckingham | 8:56a24df93680 | 102 | void gameOver(); |
SeanBuckingham | 8:56a24df93680 | 103 | |
SeanBuckingham | 10:92538c02e6c8 | 104 | |
SeanBuckingham | 8:56a24df93680 | 105 | |
SeanBuckingham | 8:56a24df93680 | 106 | void handleObstacleHit(); |
SeanBuckingham | 8:56a24df93680 | 107 | |
SeanBuckingham | 8:56a24df93680 | 108 | void handleTargetHit(); |
SeanBuckingham | 8:56a24df93680 | 109 | |
SeanBuckingham | 8:56a24df93680 | 110 | void redrawTarget(); |
SeanBuckingham | 8:56a24df93680 | 111 | |
jboettcher | 0:137546fb5da1 | 112 | int main() { |
SeanBuckingham | 10:92538c02e6c8 | 113 | int currLevel = 1; |
jboettcher | 1:8a3fa9e90572 | 114 | while(1) { |
jboettcher | 1:8a3fa9e90572 | 115 | |
SeanBuckingham | 8:56a24df93680 | 116 | startGame(); |
SeanBuckingham | 10:92538c02e6c8 | 117 | //uLCD.rectangle(0,0,100,100, 0xffffff); |
SeanBuckingham | 10:92538c02e6c8 | 118 | initializeLevel(currLevel); |
SeanBuckingham | 10:92538c02e6c8 | 119 | bool play = true; |
SeanBuckingham | 8:56a24df93680 | 120 | |
SeanBuckingham | 10:92538c02e6c8 | 121 | while (play) { //actual game play code |
SeanBuckingham | 10:92538c02e6c8 | 122 | //handle controls (5 tactile swtich) |
SeanBuckingham | 10:92538c02e6c8 | 123 | if(myNav.fire()) shoot(); |
SeanBuckingham | 10:92538c02e6c8 | 124 | if(myNav.right()) moveShooter(); |
SeanBuckingham | 10:92538c02e6c8 | 125 | if(myNav.left()) myled=!myled; |
SeanBuckingham | 10:92538c02e6c8 | 126 | |
SeanBuckingham | 10:92538c02e6c8 | 127 | |
SeanBuckingham | 10:92538c02e6c8 | 128 | |
SeanBuckingham | 10:92538c02e6c8 | 129 | } |
SeanBuckingham | 8:56a24df93680 | 130 | |
SeanBuckingham | 8:56a24df93680 | 131 | |
SeanBuckingham | 8:56a24df93680 | 132 | |
SeanBuckingham | 8:56a24df93680 | 133 | |
SeanBuckingham | 8:56a24df93680 | 134 | |
SeanBuckingham | 8:56a24df93680 | 135 | |
SeanBuckingham | 8:56a24df93680 | 136 | } |
jboettcher | 0:137546fb5da1 | 137 | } |