SharpShooter
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
MainGame.cpp@13:67e79a582164, 2016-10-29 (annotated)
- Committer:
- jboettcher
- Date:
- Sat Oct 29 20:01:27 2016 +0000
- Revision:
- 13:67e79a582164
- Parent:
- 12:2f358065ba3f
- Child:
- 15:e09ab0d14d4b
Stopping point;
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 | 11:55b65415b6ba | 8 | #include "Obstacle.h" |
jboettcher | 11:55b65415b6ba | 9 | #include "Shooter.h" |
jboettcher | 11:55b65415b6ba | 10 | #include "Bullet.h" |
jboettcher | 0:137546fb5da1 | 11 | |
SeanBuckingham | 10:92538c02e6c8 | 12 | DigitalOut myled1(LED1); |
SeanBuckingham | 10:92538c02e6c8 | 13 | DigitalOut myled2(LED2); |
SeanBuckingham | 10:92538c02e6c8 | 14 | DigitalOut myled3(LED3); |
SeanBuckingham | 10:92538c02e6c8 | 15 | DigitalOut myled4(LED4); |
SeanBuckingham | 10:92538c02e6c8 | 16 | |
jboettcher | 12:2f358065ba3f | 17 | InterruptIn center(p25); |
jboettcher | 12:2f358065ba3f | 18 | InterruptIn left(p26); |
jboettcher | 12:2f358065ba3f | 19 | InterruptIn right(p28); |
jboettcher | 12:2f358065ba3f | 20 | |
SeanBuckingham | 10:92538c02e6c8 | 21 | ///sd/wavfiles/def_swar.wav |
SeanBuckingham | 10:92538c02e6c8 | 22 | |
SeanBuckingham | 8:56a24df93680 | 23 | /*INSTANTIATION*/ |
jboettcher | 0:137546fb5da1 | 24 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
jboettcher | 1:8a3fa9e90572 | 25 | Nav_Switch myNav( p24, p25, p26, p27, p28); //up, down, left, right, fire |
SeanBuckingham | 8:56a24df93680 | 26 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
jboettcher | 0:137546fb5da1 | 27 | AnalogOut DACout(p18); |
jboettcher | 0:137546fb5da1 | 28 | wave_player waver(&DACout); |
jboettcher | 12:2f358065ba3f | 29 | Shooter player1; |
jboettcher | 12:2f358065ba3f | 30 | Bullet bullet; |
jboettcher | 12:2f358065ba3f | 31 | bool isBullet; |
jboettcher | 12:2f358065ba3f | 32 | Mutex mutex; |
jboettcher | 0:137546fb5da1 | 33 | |
SeanBuckingham | 10:92538c02e6c8 | 34 | /*STRUCT INITIALIZATION*/ |
jboettcher | 12:2f358065ba3f | 35 | /* |
SeanBuckingham | 10:92538c02e6c8 | 36 | struct ObstLocation { |
SeanBuckingham | 10:92538c02e6c8 | 37 | int x1, y1, x2, y2; |
jboettcher | 11:55b65415b6ba | 38 | }; |
jboettcher | 12:2f358065ba3f | 39 | */ |
SeanBuckingham | 10:92538c02e6c8 | 40 | |
SeanBuckingham | 10:92538c02e6c8 | 41 | struct TargetLocation { |
SeanBuckingham | 10:92538c02e6c8 | 42 | int x,y,r; |
jboettcher | 11:55b65415b6ba | 43 | }; |
jboettcher | 12:2f358065ba3f | 44 | |
jboettcher | 12:2f358065ba3f | 45 | |
jboettcher | 11:55b65415b6ba | 46 | /* |
SeanBuckingham | 10:92538c02e6c8 | 47 | struct BulletLocation { |
SeanBuckingham | 10:92538c02e6c8 | 48 | int x, topY, bottomY; |
jboettcher | 11:55b65415b6ba | 49 | }; |
jboettcher | 11:55b65415b6ba | 50 | */ |
SeanBuckingham | 8:56a24df93680 | 51 | /*LOCAL VARIABLES*/ |
SeanBuckingham | 8:56a24df93680 | 52 | int numTries; |
SeanBuckingham | 8:56a24df93680 | 53 | int levelNum; |
SeanBuckingham | 10:92538c02e6c8 | 54 | BulletLocation bulletLocation; |
SeanBuckingham | 10:92538c02e6c8 | 55 | ObstLocation * obstaclePtr; |
jboettcher | 12:2f358065ba3f | 56 | Obstacle obsArr[3]; |
SeanBuckingham | 10:92538c02e6c8 | 57 | TargetLocation * targetsPtr = new TargetLocation[3]; |
jboettcher | 12:2f358065ba3f | 58 | bool play = 0; |
SeanBuckingham | 10:92538c02e6c8 | 59 | |
SeanBuckingham | 10:92538c02e6c8 | 60 | |
SeanBuckingham | 8:56a24df93680 | 61 | |
SeanBuckingham | 8:56a24df93680 | 62 | void startGame() { |
jboettcher | 12:2f358065ba3f | 63 | uLCD.background_color(BACKGROUND); |
jboettcher | 12:2f358065ba3f | 64 | uLCD.locate(0,0); |
jboettcher | 12:2f358065ba3f | 65 | //uLCD.set_font_size(44, 44); |
SeanBuckingham | 8:56a24df93680 | 66 | uLCD.printf("\nSharp Shooter!!\n"); |
SeanBuckingham | 10:92538c02e6c8 | 67 | wait(0.1); |
SeanBuckingham | 8:56a24df93680 | 68 | |
SeanBuckingham | 8:56a24df93680 | 69 | FILE *wave_file; |
SeanBuckingham | 10:92538c02e6c8 | 70 | wave_file=fopen("/sd/wavfiles/pitfall.wav","r"); |
SeanBuckingham | 8:56a24df93680 | 71 | waver.play(wave_file); |
SeanBuckingham | 8:56a24df93680 | 72 | fclose(wave_file); |
SeanBuckingham | 10:92538c02e6c8 | 73 | |
jboettcher | 12:2f358065ba3f | 74 | wait(2); |
jboettcher | 12:2f358065ba3f | 75 | uLCD.cls(); |
jboettcher | 12:2f358065ba3f | 76 | //uLCD.filled_rectangle(0,0,127,127,BACKGROUND); |
SeanBuckingham | 10:92538c02e6c8 | 77 | } |
SeanBuckingham | 10:92538c02e6c8 | 78 | |
SeanBuckingham | 10:92538c02e6c8 | 79 | void createTargets() { |
SeanBuckingham | 10:92538c02e6c8 | 80 | //draw all targets |
SeanBuckingham | 10:92538c02e6c8 | 81 | //initialize targetPtr w locations of where theyre drawn |
jboettcher | 12:2f358065ba3f | 82 | uLCD.filled_circle(24,15,5,TARGET); |
jboettcher | 12:2f358065ba3f | 83 | uLCD.filled_circle(64,15,5,TARGET); |
jboettcher | 12:2f358065ba3f | 84 | uLCD.filled_circle(104,15,5,TARGET); |
jboettcher | 12:2f358065ba3f | 85 | |
SeanBuckingham | 10:92538c02e6c8 | 86 | } |
SeanBuckingham | 10:92538c02e6c8 | 87 | |
SeanBuckingham | 10:92538c02e6c8 | 88 | void createShooter() { |
jboettcher | 12:2f358065ba3f | 89 | player1.drawShooter(); |
SeanBuckingham | 10:92538c02e6c8 | 90 | } |
jboettcher | 12:2f358065ba3f | 91 | |
SeanBuckingham | 10:92538c02e6c8 | 92 | void createObstacles(int num) { |
SeanBuckingham | 10:92538c02e6c8 | 93 | obstaclePtr = new ObstLocation[num]; |
SeanBuckingham | 10:92538c02e6c8 | 94 | //dummy x values, CHANGE LATER |
SeanBuckingham | 10:92538c02e6c8 | 95 | if (num == 1) { |
jboettcher | 12:2f358065ba3f | 96 | obsArr[0] = Obstacle(2, 62, 50, 65); |
SeanBuckingham | 10:92538c02e6c8 | 97 | } else if (num == 2) { |
jboettcher | 12:2f358065ba3f | 98 | obsArr[0] = Obstacle(15, 15, 115, 115); |
jboettcher | 12:2f358065ba3f | 99 | obsArr[1] = Obstacle(15, 15, 115, 115); |
SeanBuckingham | 10:92538c02e6c8 | 100 | } else if (num == 3) { |
jboettcher | 12:2f358065ba3f | 101 | obsArr[0] = Obstacle(15, 15, 115, 115); |
jboettcher | 12:2f358065ba3f | 102 | obsArr[1] = Obstacle(15, 15, 115, 115); |
jboettcher | 12:2f358065ba3f | 103 | obsArr[2] = Obstacle(15, 15, 115, 115); |
SeanBuckingham | 10:92538c02e6c8 | 104 | } |
SeanBuckingham | 10:92538c02e6c8 | 105 | for (int i = 0; i < num; i++) { |
SeanBuckingham | 10:92538c02e6c8 | 106 | obstaclePtr[i] = obsArr[i].getLocation(); |
jboettcher | 12:2f358065ba3f | 107 | obsArr[i].drawObstacle(); |
jboettcher | 12:2f358065ba3f | 108 | } |
jboettcher | 12:2f358065ba3f | 109 | } |
jboettcher | 12:2f358065ba3f | 110 | |
jboettcher | 12:2f358065ba3f | 111 | void initializeLevel() { |
jboettcher | 12:2f358065ba3f | 112 | numTries = 10; |
jboettcher | 12:2f358065ba3f | 113 | //draw header (level #, numTries) at the top |
jboettcher | 12:2f358065ba3f | 114 | uLCD.locate(0,0); |
jboettcher | 12:2f358065ba3f | 115 | uLCD.printf("LEVEL:%d BULLETS:%d", levelNum, numTries); |
jboettcher | 12:2f358065ba3f | 116 | |
jboettcher | 12:2f358065ba3f | 117 | createTargets(); |
jboettcher | 12:2f358065ba3f | 118 | createObstacles(levelNum); |
jboettcher | 12:2f358065ba3f | 119 | createShooter(); |
jboettcher | 12:2f358065ba3f | 120 | } |
jboettcher | 12:2f358065ba3f | 121 | |
jboettcher | 12:2f358065ba3f | 122 | void movingBullet(void const *args) { |
jboettcher | 12:2f358065ba3f | 123 | while (true) { |
jboettcher | 12:2f358065ba3f | 124 | if(isBullet) { |
jboettcher | 12:2f358065ba3f | 125 | mutex.lock(); |
jboettcher | 12:2f358065ba3f | 126 | bullet.move(); |
jboettcher | 12:2f358065ba3f | 127 | mutex.unlock(); |
jboettcher | 12:2f358065ba3f | 128 | |
jboettcher | 12:2f358065ba3f | 129 | Thread::wait(100); |
jboettcher | 12:2f358065ba3f | 130 | } |
SeanBuckingham | 10:92538c02e6c8 | 131 | } |
SeanBuckingham | 10:92538c02e6c8 | 132 | } |
jboettcher | 12:2f358065ba3f | 133 | |
jboettcher | 12:2f358065ba3f | 134 | void movingObs(void const *args) { |
jboettcher | 12:2f358065ba3f | 135 | while (true) { |
jboettcher | 12:2f358065ba3f | 136 | if(play) { |
jboettcher | 12:2f358065ba3f | 137 | mutex.lock(); |
jboettcher | 12:2f358065ba3f | 138 | if (levelNum == 1) { |
jboettcher | 12:2f358065ba3f | 139 | obsArr[0].setDirection(1); |
jboettcher | 12:2f358065ba3f | 140 | obsArr[0].move(3); |
jboettcher | 12:2f358065ba3f | 141 | } else if (levelNum == 2) { |
jboettcher | 12:2f358065ba3f | 142 | obsArr[0].setDirection(1); |
jboettcher | 12:2f358065ba3f | 143 | obsArr[0].move(6); |
jboettcher | 12:2f358065ba3f | 144 | obsArr[1].setDirection(0); |
jboettcher | 12:2f358065ba3f | 145 | obsArr[1].move(6); |
jboettcher | 12:2f358065ba3f | 146 | } else if (levelNum == 3) { |
jboettcher | 12:2f358065ba3f | 147 | obsArr[0].setDirection(1); |
jboettcher | 12:2f358065ba3f | 148 | obsArr[0].move(9); |
jboettcher | 12:2f358065ba3f | 149 | obsArr[1].setDirection(0); |
jboettcher | 12:2f358065ba3f | 150 | obsArr[1].move(9); |
jboettcher | 12:2f358065ba3f | 151 | obsArr[2].setDirection(1); |
jboettcher | 12:2f358065ba3f | 152 | obsArr[2].move(9); |
jboettcher | 12:2f358065ba3f | 153 | } |
jboettcher | 12:2f358065ba3f | 154 | mutex.unlock(); |
jboettcher | 13:67e79a582164 | 155 | Thread::wait(500); |
jboettcher | 12:2f358065ba3f | 156 | } |
jboettcher | 12:2f358065ba3f | 157 | } |
jboettcher | 12:2f358065ba3f | 158 | } |
jboettcher | 12:2f358065ba3f | 159 | |
SeanBuckingham | 10:92538c02e6c8 | 160 | void shoot() { |
jboettcher | 12:2f358065ba3f | 161 | bullet.drawBullet(player1.getLocation(), 115); |
jboettcher | 12:2f358065ba3f | 162 | isBullet = 1; |
SeanBuckingham | 10:92538c02e6c8 | 163 | } |
SeanBuckingham | 8:56a24df93680 | 164 | |
SeanBuckingham | 8:56a24df93680 | 165 | void checkIfHit(); |
SeanBuckingham | 8:56a24df93680 | 166 | |
SeanBuckingham | 8:56a24df93680 | 167 | void gameOver(); |
SeanBuckingham | 8:56a24df93680 | 168 | |
SeanBuckingham | 10:92538c02e6c8 | 169 | |
SeanBuckingham | 8:56a24df93680 | 170 | |
SeanBuckingham | 8:56a24df93680 | 171 | void handleObstacleHit(); |
SeanBuckingham | 8:56a24df93680 | 172 | |
SeanBuckingham | 8:56a24df93680 | 173 | void handleTargetHit(); |
SeanBuckingham | 8:56a24df93680 | 174 | |
SeanBuckingham | 8:56a24df93680 | 175 | void redrawTarget(); |
SeanBuckingham | 8:56a24df93680 | 176 | |
jboettcher | 0:137546fb5da1 | 177 | int main() { |
jboettcher | 12:2f358065ba3f | 178 | levelNum = 1; |
jboettcher | 12:2f358065ba3f | 179 | isBullet = 0; |
jboettcher | 12:2f358065ba3f | 180 | Thread bulletThread(movingBullet); |
jboettcher | 12:2f358065ba3f | 181 | Thread obstacleThread(movingObs); |
jboettcher | 1:8a3fa9e90572 | 182 | while(1) { |
jboettcher | 1:8a3fa9e90572 | 183 | |
SeanBuckingham | 8:56a24df93680 | 184 | startGame(); |
SeanBuckingham | 10:92538c02e6c8 | 185 | //uLCD.rectangle(0,0,100,100, 0xffffff); |
jboettcher | 12:2f358065ba3f | 186 | initializeLevel(); |
jboettcher | 12:2f358065ba3f | 187 | play = 1; |
SeanBuckingham | 10:92538c02e6c8 | 188 | while (play) { //actual game play code |
jboettcher | 12:2f358065ba3f | 189 | if(!right.read()) player1.moveRight(); |
jboettcher | 12:2f358065ba3f | 190 | if(!left.read()) player1.moveLeft(); |
jboettcher | 12:2f358065ba3f | 191 | if(!center.read()) shoot(); |
jboettcher | 12:2f358065ba3f | 192 | //if(isBullet) bullet.move(); |
jboettcher | 12:2f358065ba3f | 193 | wait(0.12); |
SeanBuckingham | 10:92538c02e6c8 | 194 | } |
SeanBuckingham | 8:56a24df93680 | 195 | } |
jboettcher | 0:137546fb5da1 | 196 | } |