SharpShooter

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Committer:
jboettcher
Date:
Mon Oct 31 18:15:21 2016 +0000
Revision:
15:e09ab0d14d4b
Parent:
13:67e79a582164
Finished

Who changed what in which revision?

UserRevisionLine numberNew 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
jboettcher 15:e09ab0d14d4b 12 /*DIRECTIVES*/
jboettcher 15:e09ab0d14d4b 13 #define NUMTRIES 10
jboettcher 15:e09ab0d14d4b 14
jboettcher 15:e09ab0d14d4b 15 /*INSTANTIATION*/
SeanBuckingham 10:92538c02e6c8 16 DigitalOut myled1(LED1);
SeanBuckingham 10:92538c02e6c8 17 DigitalOut myled2(LED2);
SeanBuckingham 10:92538c02e6c8 18 DigitalOut myled3(LED3);
SeanBuckingham 10:92538c02e6c8 19 DigitalOut myled4(LED4);
SeanBuckingham 10:92538c02e6c8 20
jboettcher 12:2f358065ba3f 21 InterruptIn center(p25);
jboettcher 12:2f358065ba3f 22 InterruptIn left(p26);
jboettcher 12:2f358065ba3f 23 InterruptIn right(p28);
jboettcher 12:2f358065ba3f 24
jboettcher 0:137546fb5da1 25 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
jboettcher 1:8a3fa9e90572 26 Nav_Switch myNav( p24, p25, p26, p27, p28); //up, down, left, right, fire
SeanBuckingham 8:56a24df93680 27 SDFileSystem sd(p5, p6, p7, p8, "sd");
jboettcher 0:137546fb5da1 28 AnalogOut DACout(p18);
jboettcher 0:137546fb5da1 29 wave_player waver(&DACout);
jboettcher 12:2f358065ba3f 30 Shooter player1;
jboettcher 12:2f358065ba3f 31 Bullet bullet;
jboettcher 12:2f358065ba3f 32 bool isBullet;
jboettcher 12:2f358065ba3f 33 Mutex mutex;
jboettcher 0:137546fb5da1 34
SeanBuckingham 10:92538c02e6c8 35 /*STRUCT INITIALIZATION*/
SeanBuckingham 10:92538c02e6c8 36 struct TargetLocation {
SeanBuckingham 10:92538c02e6c8 37 int x,y,r;
jboettcher 11:55b65415b6ba 38 };
jboettcher 12:2f358065ba3f 39
jboettcher 15:e09ab0d14d4b 40 /*GLOBAL VARIABLES*/
SeanBuckingham 8:56a24df93680 41 int numTries;
SeanBuckingham 8:56a24df93680 42 int levelNum;
SeanBuckingham 10:92538c02e6c8 43 BulletLocation bulletLocation;
jboettcher 12:2f358065ba3f 44 Obstacle obsArr[3];
SeanBuckingham 10:92538c02e6c8 45 TargetLocation * targetsPtr = new TargetLocation[3];
jboettcher 12:2f358065ba3f 46 bool play = 0;
jboettcher 15:e09ab0d14d4b 47 int targetY = 15;
jboettcher 15:e09ab0d14d4b 48 int targetRad = 5;
jboettcher 15:e09ab0d14d4b 49 int targetsLeft = 3;
jboettcher 15:e09ab0d14d4b 50 bool targetsHit[3];
SeanBuckingham 8:56a24df93680 51
SeanBuckingham 8:56a24df93680 52 void startGame() {
jboettcher 12:2f358065ba3f 53 uLCD.background_color(BACKGROUND);
jboettcher 15:e09ab0d14d4b 54 uLCD.filled_rectangle(0,0,127,127,BLUE);
jboettcher 15:e09ab0d14d4b 55 uLCD.locate(3,6);
jboettcher 15:e09ab0d14d4b 56 uLCD.printf("Sharp Shooter!");
SeanBuckingham 10:92538c02e6c8 57 wait(0.1);
SeanBuckingham 8:56a24df93680 58
SeanBuckingham 8:56a24df93680 59 FILE *wave_file;
jboettcher 15:e09ab0d14d4b 60 wave_file=fopen("/sd/wavfiles/intro.wav","r");
SeanBuckingham 8:56a24df93680 61 waver.play(wave_file);
SeanBuckingham 8:56a24df93680 62 fclose(wave_file);
SeanBuckingham 10:92538c02e6c8 63
jboettcher 15:e09ab0d14d4b 64 wait(0.3);
jboettcher 12:2f358065ba3f 65 uLCD.cls();
SeanBuckingham 10:92538c02e6c8 66 }
SeanBuckingham 10:92538c02e6c8 67
SeanBuckingham 10:92538c02e6c8 68 void createTargets() {
SeanBuckingham 10:92538c02e6c8 69 //draw all targets
jboettcher 15:e09ab0d14d4b 70 uLCD.filled_circle(24,targetY,targetRad,TARGET);
jboettcher 15:e09ab0d14d4b 71 uLCD.filled_circle(64,targetY,targetRad,TARGET);
jboettcher 15:e09ab0d14d4b 72 uLCD.filled_circle(104,targetY,targetRad,TARGET);
jboettcher 15:e09ab0d14d4b 73 targetsLeft = 3;
jboettcher 15:e09ab0d14d4b 74 for (int i=0; i<targetsLeft; i++) {
jboettcher 15:e09ab0d14d4b 75 targetsHit[i] = 0;
jboettcher 15:e09ab0d14d4b 76 }
SeanBuckingham 10:92538c02e6c8 77 }
SeanBuckingham 10:92538c02e6c8 78
SeanBuckingham 10:92538c02e6c8 79 void createShooter() {
jboettcher 12:2f358065ba3f 80 player1.drawShooter();
SeanBuckingham 10:92538c02e6c8 81 }
jboettcher 12:2f358065ba3f 82
SeanBuckingham 10:92538c02e6c8 83 void createObstacles(int num) {
jboettcher 15:e09ab0d14d4b 84 //instantiate obstacles and store in obsArr
jboettcher 15:e09ab0d14d4b 85 if (num == 1) { //if Level 1
jboettcher 15:e09ab0d14d4b 86 obsArr[0] = Obstacle(2, 62, 50, 65);
jboettcher 15:e09ab0d14d4b 87 obsArr[0].setDirection(1);
jboettcher 15:e09ab0d14d4b 88 } else if (num == 2) { //if Level 2
jboettcher 12:2f358065ba3f 89 obsArr[0] = Obstacle(2, 62, 50, 65);
jboettcher 15:e09ab0d14d4b 90 obsArr[0].setDirection(1);
jboettcher 15:e09ab0d14d4b 91 obsArr[1] = Obstacle(67, 32, 115, 35);
jboettcher 15:e09ab0d14d4b 92 obsArr[1].setDirection(0);
jboettcher 15:e09ab0d14d4b 93 } else if (num == 3) { //if Level 3
jboettcher 15:e09ab0d14d4b 94 obsArr[0] = Obstacle(2, 62, 50, 65);
jboettcher 15:e09ab0d14d4b 95 obsArr[0].setDirection(1);
jboettcher 15:e09ab0d14d4b 96 obsArr[1] = Obstacle(67, 32, 115, 35);
jboettcher 15:e09ab0d14d4b 97 obsArr[1].setDirection(0);
jboettcher 15:e09ab0d14d4b 98 obsArr[2] = Obstacle(48, 48, 96, 51);
jboettcher 15:e09ab0d14d4b 99 obsArr[2].setDirection(1);
SeanBuckingham 10:92538c02e6c8 100 }
jboettcher 15:e09ab0d14d4b 101 //draw obstacles
SeanBuckingham 10:92538c02e6c8 102 for (int i = 0; i < num; i++) {
jboettcher 12:2f358065ba3f 103 obsArr[i].drawObstacle();
jboettcher 12:2f358065ba3f 104 }
jboettcher 12:2f358065ba3f 105 }
jboettcher 12:2f358065ba3f 106
jboettcher 15:e09ab0d14d4b 107 void drawHeader() {
jboettcher 15:e09ab0d14d4b 108 mutex.lock();
jboettcher 15:e09ab0d14d4b 109 if (numTries>=9) uLCD.filled_rectangle(0,0,128,7,BLACK); //clear header if over double digits
jboettcher 12:2f358065ba3f 110 uLCD.locate(0,0);
jboettcher 12:2f358065ba3f 111 uLCD.printf("LEVEL:%d BULLETS:%d", levelNum, numTries);
jboettcher 15:e09ab0d14d4b 112 mutex.unlock();
jboettcher 15:e09ab0d14d4b 113 }
jboettcher 15:e09ab0d14d4b 114
jboettcher 15:e09ab0d14d4b 115 void initializeLevel() {
jboettcher 15:e09ab0d14d4b 116 mutex.lock();
jboettcher 15:e09ab0d14d4b 117 uLCD.cls();
jboettcher 15:e09ab0d14d4b 118 mutex.unlock();
jboettcher 15:e09ab0d14d4b 119 numTries = NUMTRIES;
jboettcher 15:e09ab0d14d4b 120 drawHeader();
jboettcher 12:2f358065ba3f 121 createTargets();
jboettcher 12:2f358065ba3f 122 createObstacles(levelNum);
jboettcher 12:2f358065ba3f 123 createShooter();
jboettcher 12:2f358065ba3f 124 }
jboettcher 12:2f358065ba3f 125
jboettcher 15:e09ab0d14d4b 126 void gameOver() {
jboettcher 15:e09ab0d14d4b 127 play = 0;
jboettcher 15:e09ab0d14d4b 128 mutex.lock();
jboettcher 15:e09ab0d14d4b 129 uLCD.filled_rectangle(0,0,127,127,RED);
jboettcher 15:e09ab0d14d4b 130 uLCD.locate(5,6);
jboettcher 15:e09ab0d14d4b 131 uLCD.printf("GAM3 0V3R");
jboettcher 15:e09ab0d14d4b 132 mutex.unlock();
jboettcher 15:e09ab0d14d4b 133
jboettcher 15:e09ab0d14d4b 134 FILE *wave_file;
jboettcher 15:e09ab0d14d4b 135 wave_file=fopen("/sd/wavfiles/gameover.wav","r");
jboettcher 15:e09ab0d14d4b 136 waver.play(wave_file);
jboettcher 15:e09ab0d14d4b 137 fclose(wave_file);
jboettcher 15:e09ab0d14d4b 138
jboettcher 15:e09ab0d14d4b 139 while(1); //gameOver screen forever
jboettcher 15:e09ab0d14d4b 140 }
jboettcher 15:e09ab0d14d4b 141
jboettcher 15:e09ab0d14d4b 142 void removeBullet() {
jboettcher 15:e09ab0d14d4b 143 bullet.eraseBullet();
jboettcher 15:e09ab0d14d4b 144 isBullet = 0;
jboettcher 15:e09ab0d14d4b 145 numTries -= 1;
jboettcher 15:e09ab0d14d4b 146 drawHeader();
jboettcher 15:e09ab0d14d4b 147 if (numTries==0 && targetsLeft>0) gameOver();
jboettcher 15:e09ab0d14d4b 148 }
jboettcher 15:e09ab0d14d4b 149
jboettcher 15:e09ab0d14d4b 150 void killTarget(BulletLocation currBullet,int i) {
jboettcher 15:e09ab0d14d4b 151 targetsHit[i] = 1;
jboettcher 15:e09ab0d14d4b 152 targetsLeft -= 1;
jboettcher 15:e09ab0d14d4b 153 uLCD.filled_circle(currBullet.x,targetY,targetRad,BLUE);
jboettcher 15:e09ab0d14d4b 154 uLCD.line(currBullet.x-targetRad,targetY+targetRad,currBullet.x+targetRad,targetY-targetRad,BLACK);
jboettcher 15:e09ab0d14d4b 155 uLCD.line(currBullet.x-targetRad,targetY-targetRad,currBullet.x+targetRad,targetY+targetRad,BLACK);
jboettcher 15:e09ab0d14d4b 156
jboettcher 15:e09ab0d14d4b 157 FILE *wave_file;
jboettcher 15:e09ab0d14d4b 158 wave_file=fopen("/sd/wavfiles/hittarget.wav","r");
jboettcher 15:e09ab0d14d4b 159 waver.play(wave_file);
jboettcher 15:e09ab0d14d4b 160 fclose(wave_file);
jboettcher 15:e09ab0d14d4b 161 }
jboettcher 15:e09ab0d14d4b 162
jboettcher 12:2f358065ba3f 163 void movingBullet(void const *args) {
jboettcher 12:2f358065ba3f 164 while (true) {
jboettcher 12:2f358065ba3f 165 if(isBullet) {
jboettcher 12:2f358065ba3f 166 mutex.lock();
jboettcher 12:2f358065ba3f 167 bullet.move();
jboettcher 15:e09ab0d14d4b 168 // Remove bullet if it hits an obstacle
jboettcher 15:e09ab0d14d4b 169 ObstLocation currObs;
jboettcher 15:e09ab0d14d4b 170 BulletLocation currBullet;
jboettcher 15:e09ab0d14d4b 171 for (int i=0; i<levelNum; i++) {
jboettcher 15:e09ab0d14d4b 172 currObs = obsArr[i].getLocation(); // {x1, y1, x2, y2}
jboettcher 15:e09ab0d14d4b 173 currBullet = bullet.getLocation(); // {x, topY, bottomY}
jboettcher 15:e09ab0d14d4b 174 if ((currBullet.topY<=currObs.y2 && currBullet.topY>=currObs.y1) || (currBullet.bottomY<=currObs.y2 && currBullet.bottomY>=currObs.y1)) {
jboettcher 15:e09ab0d14d4b 175 if (currBullet.x>=currObs.x1 && currBullet.x<=currObs.x2) {
jboettcher 15:e09ab0d14d4b 176 removeBullet();
jboettcher 15:e09ab0d14d4b 177 FILE *wave_file;
jboettcher 15:e09ab0d14d4b 178 wave_file=fopen("/sd/wavfiles/hitobstacle.wav","r");
jboettcher 15:e09ab0d14d4b 179 waver.play(wave_file);
jboettcher 15:e09ab0d14d4b 180 fclose(wave_file);
jboettcher 15:e09ab0d14d4b 181 }
jboettcher 15:e09ab0d14d4b 182 }
jboettcher 15:e09ab0d14d4b 183 }
jboettcher 15:e09ab0d14d4b 184 // Remove bullet, and draw dead target if it hits a target
jboettcher 15:e09ab0d14d4b 185 if (currBullet.topY<=targetY+targetRad) {
jboettcher 15:e09ab0d14d4b 186 //if target is already hit
jboettcher 15:e09ab0d14d4b 187 if (currBullet.x==24 && targetsHit[0]==0) killTarget(currBullet,0);
jboettcher 15:e09ab0d14d4b 188 if (currBullet.x==64 && targetsHit[1]==0) killTarget(currBullet,1);
jboettcher 15:e09ab0d14d4b 189 if (currBullet.x==104 && targetsHit[2]==0) killTarget(currBullet,2);
jboettcher 15:e09ab0d14d4b 190 removeBullet();
jboettcher 15:e09ab0d14d4b 191 }
jboettcher 12:2f358065ba3f 192 mutex.unlock();
jboettcher 12:2f358065ba3f 193
jboettcher 12:2f358065ba3f 194 Thread::wait(100);
jboettcher 12:2f358065ba3f 195 }
SeanBuckingham 10:92538c02e6c8 196 }
SeanBuckingham 10:92538c02e6c8 197 }
jboettcher 12:2f358065ba3f 198
jboettcher 12:2f358065ba3f 199 void movingObs(void const *args) {
jboettcher 12:2f358065ba3f 200 while (true) {
jboettcher 12:2f358065ba3f 201 if(play) {
jboettcher 12:2f358065ba3f 202 mutex.lock();
jboettcher 12:2f358065ba3f 203 if (levelNum == 1) {
jboettcher 12:2f358065ba3f 204 obsArr[0].move(3);
jboettcher 12:2f358065ba3f 205 } else if (levelNum == 2) {
jboettcher 12:2f358065ba3f 206 obsArr[0].move(6);
jboettcher 12:2f358065ba3f 207 obsArr[1].move(6);
jboettcher 12:2f358065ba3f 208 } else if (levelNum == 3) {
jboettcher 12:2f358065ba3f 209 obsArr[0].move(9);
jboettcher 12:2f358065ba3f 210 obsArr[1].move(9);
jboettcher 12:2f358065ba3f 211 obsArr[2].move(9);
jboettcher 12:2f358065ba3f 212 }
jboettcher 12:2f358065ba3f 213 mutex.unlock();
jboettcher 13:67e79a582164 214 Thread::wait(500);
jboettcher 12:2f358065ba3f 215 }
jboettcher 12:2f358065ba3f 216 }
jboettcher 12:2f358065ba3f 217 }
jboettcher 12:2f358065ba3f 218
SeanBuckingham 10:92538c02e6c8 219 void shoot() {
jboettcher 15:e09ab0d14d4b 220 if (!isBullet) {
jboettcher 15:e09ab0d14d4b 221 bullet.drawBullet(player1.getLocation(), 115);
jboettcher 15:e09ab0d14d4b 222 isBullet = 1;
jboettcher 15:e09ab0d14d4b 223
jboettcher 15:e09ab0d14d4b 224 FILE *wave_file;
jboettcher 15:e09ab0d14d4b 225 wave_file=fopen("/sd/wavfiles/shoot.wav","r");
jboettcher 15:e09ab0d14d4b 226 waver.play(wave_file);
jboettcher 15:e09ab0d14d4b 227 fclose(wave_file);
jboettcher 15:e09ab0d14d4b 228 }
SeanBuckingham 10:92538c02e6c8 229 }
SeanBuckingham 8:56a24df93680 230
jboettcher 0:137546fb5da1 231 int main() {
jboettcher 12:2f358065ba3f 232 levelNum = 1;
jboettcher 12:2f358065ba3f 233 isBullet = 0;
jboettcher 12:2f358065ba3f 234 Thread bulletThread(movingBullet);
jboettcher 12:2f358065ba3f 235 Thread obstacleThread(movingObs);
jboettcher 15:e09ab0d14d4b 236 startGame();
jboettcher 1:8a3fa9e90572 237 while(1) {
jboettcher 12:2f358065ba3f 238 initializeLevel();
jboettcher 12:2f358065ba3f 239 play = 1;
SeanBuckingham 10:92538c02e6c8 240 while (play) { //actual game play code
jboettcher 15:e09ab0d14d4b 241 if(targetsLeft==0) {
jboettcher 15:e09ab0d14d4b 242 play = 0;
jboettcher 15:e09ab0d14d4b 243 levelNum++;
jboettcher 15:e09ab0d14d4b 244 }
jboettcher 12:2f358065ba3f 245 if(!right.read()) player1.moveRight();
jboettcher 12:2f358065ba3f 246 if(!left.read()) player1.moveLeft();
jboettcher 12:2f358065ba3f 247 if(!center.read()) shoot();
jboettcher 12:2f358065ba3f 248 wait(0.12);
SeanBuckingham 10:92538c02e6c8 249 }
SeanBuckingham 8:56a24df93680 250 }
jboettcher 0:137546fb5da1 251 }