SharpShooter
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Revision 15:e09ab0d14d4b, committed 2016-10-31
- Comitter:
- jboettcher
- Date:
- Mon Oct 31 18:15:21 2016 +0000
- Parent:
- 14:3f7b826ad15e
- Commit message:
- Finished
Changed in this revision
diff -r 3f7b826ad15e -r e09ab0d14d4b Bullet.h --- a/Bullet.h Sat Oct 29 20:02:57 2016 +0000 +++ b/Bullet.h Mon Oct 31 18:15:21 2016 +0000 @@ -28,13 +28,6 @@ bottomY=3; } -/*Bullet::Bullet(int x, int bottomY, uLCD_4DGL uLCD) { - this->x = x; - this->bottomY = bottomY; - this->topY = bottomY + length; - this->uLCD = uLCD; -}*/ - void Bullet::drawBullet(int x2, int bottomY2) { x = x2; bottomY = bottomY2;
diff -r 3f7b826ad15e -r e09ab0d14d4b MainGame.cpp --- a/MainGame.cpp Sat Oct 29 20:02:57 2016 +0000 +++ b/MainGame.cpp Mon Oct 31 18:15:21 2016 +0000 @@ -9,6 +9,10 @@ #include "Shooter.h" #include "Bullet.h" +/*DIRECTIVES*/ +#define NUMTRIES 10 + +/*INSTANTIATION*/ DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); @@ -18,9 +22,6 @@ InterruptIn left(p26); InterruptIn right(p28); -///sd/wavfiles/def_swar.wav - -/*INSTANTIATION*/ uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; Nav_Switch myNav( p24, p25, p26, p27, p28); //up, down, left, right, fire SDFileSystem sd(p5, p6, p7, p8, "sd"); @@ -32,57 +33,47 @@ Mutex mutex; /*STRUCT INITIALIZATION*/ -/* -struct ObstLocation { - int x1, y1, x2, y2; -}; -*/ - struct TargetLocation { int x,y,r; }; - -/* -struct BulletLocation { - int x, topY, bottomY; -}; -*/ -/*LOCAL VARIABLES*/ +/*GLOBAL VARIABLES*/ int numTries; int levelNum; BulletLocation bulletLocation; -ObstLocation * obstaclePtr; Obstacle obsArr[3]; TargetLocation * targetsPtr = new TargetLocation[3]; bool play = 0; - - +int targetY = 15; +int targetRad = 5; +int targetsLeft = 3; +bool targetsHit[3]; void startGame() { uLCD.background_color(BACKGROUND); - uLCD.locate(0,0); - //uLCD.set_font_size(44, 44); - uLCD.printf("\nSharp Shooter!!\n"); + uLCD.filled_rectangle(0,0,127,127,BLUE); + uLCD.locate(3,6); + uLCD.printf("Sharp Shooter!"); wait(0.1); FILE *wave_file; - wave_file=fopen("/sd/wavfiles/pitfall.wav","r"); + wave_file=fopen("/sd/wavfiles/intro.wav","r"); waver.play(wave_file); fclose(wave_file); - wait(2); + wait(0.3); uLCD.cls(); - //uLCD.filled_rectangle(0,0,127,127,BACKGROUND); } void createTargets() { //draw all targets - //initialize targetPtr w locations of where theyre drawn - uLCD.filled_circle(24,15,5,TARGET); - uLCD.filled_circle(64,15,5,TARGET); - uLCD.filled_circle(104,15,5,TARGET); - + uLCD.filled_circle(24,targetY,targetRad,TARGET); + uLCD.filled_circle(64,targetY,targetRad,TARGET); + uLCD.filled_circle(104,targetY,targetRad,TARGET); + targetsLeft = 3; + for (int i=0; i<targetsLeft; i++) { + targetsHit[i] = 0; + } } void createShooter() { @@ -90,40 +81,114 @@ } void createObstacles(int num) { - obstaclePtr = new ObstLocation[num]; - //dummy x values, CHANGE LATER - if (num == 1) { + //instantiate obstacles and store in obsArr + if (num == 1) { //if Level 1 + obsArr[0] = Obstacle(2, 62, 50, 65); + obsArr[0].setDirection(1); + } else if (num == 2) { //if Level 2 obsArr[0] = Obstacle(2, 62, 50, 65); - } else if (num == 2) { - obsArr[0] = Obstacle(15, 15, 115, 115); - obsArr[1] = Obstacle(15, 15, 115, 115); - } else if (num == 3) { - obsArr[0] = Obstacle(15, 15, 115, 115); - obsArr[1] = Obstacle(15, 15, 115, 115); - obsArr[2] = Obstacle(15, 15, 115, 115); + obsArr[0].setDirection(1); + obsArr[1] = Obstacle(67, 32, 115, 35); + obsArr[1].setDirection(0); + } else if (num == 3) { //if Level 3 + obsArr[0] = Obstacle(2, 62, 50, 65); + obsArr[0].setDirection(1); + obsArr[1] = Obstacle(67, 32, 115, 35); + obsArr[1].setDirection(0); + obsArr[2] = Obstacle(48, 48, 96, 51); + obsArr[2].setDirection(1); } + //draw obstacles for (int i = 0; i < num; i++) { - obstaclePtr[i] = obsArr[i].getLocation(); obsArr[i].drawObstacle(); } } -void initializeLevel() { - numTries = 10; - //draw header (level #, numTries) at the top +void drawHeader() { + mutex.lock(); + if (numTries>=9) uLCD.filled_rectangle(0,0,128,7,BLACK); //clear header if over double digits uLCD.locate(0,0); uLCD.printf("LEVEL:%d BULLETS:%d", levelNum, numTries); - + mutex.unlock(); +} + +void initializeLevel() { + mutex.lock(); + uLCD.cls(); + mutex.unlock(); + numTries = NUMTRIES; + drawHeader(); createTargets(); createObstacles(levelNum); createShooter(); } +void gameOver() { + play = 0; + mutex.lock(); + uLCD.filled_rectangle(0,0,127,127,RED); + uLCD.locate(5,6); + uLCD.printf("GAM3 0V3R"); + mutex.unlock(); + + FILE *wave_file; + wave_file=fopen("/sd/wavfiles/gameover.wav","r"); + waver.play(wave_file); + fclose(wave_file); + + while(1); //gameOver screen forever +} + +void removeBullet() { + bullet.eraseBullet(); + isBullet = 0; + numTries -= 1; + drawHeader(); + if (numTries==0 && targetsLeft>0) gameOver(); +} + +void killTarget(BulletLocation currBullet,int i) { + targetsHit[i] = 1; + targetsLeft -= 1; + uLCD.filled_circle(currBullet.x,targetY,targetRad,BLUE); + uLCD.line(currBullet.x-targetRad,targetY+targetRad,currBullet.x+targetRad,targetY-targetRad,BLACK); + uLCD.line(currBullet.x-targetRad,targetY-targetRad,currBullet.x+targetRad,targetY+targetRad,BLACK); + + FILE *wave_file; + wave_file=fopen("/sd/wavfiles/hittarget.wav","r"); + waver.play(wave_file); + fclose(wave_file); +} + void movingBullet(void const *args) { while (true) { if(isBullet) { mutex.lock(); bullet.move(); + // Remove bullet if it hits an obstacle + ObstLocation currObs; + BulletLocation currBullet; + for (int i=0; i<levelNum; i++) { + currObs = obsArr[i].getLocation(); // {x1, y1, x2, y2} + currBullet = bullet.getLocation(); // {x, topY, bottomY} + if ((currBullet.topY<=currObs.y2 && currBullet.topY>=currObs.y1) || (currBullet.bottomY<=currObs.y2 && currBullet.bottomY>=currObs.y1)) { + if (currBullet.x>=currObs.x1 && currBullet.x<=currObs.x2) { + removeBullet(); + FILE *wave_file; + wave_file=fopen("/sd/wavfiles/hitobstacle.wav","r"); + waver.play(wave_file); + fclose(wave_file); + } + } + } + // Remove bullet, and draw dead target if it hits a target + if (currBullet.topY<=targetY+targetRad) { + //if target is already hit + if (currBullet.x==24 && targetsHit[0]==0) killTarget(currBullet,0); + if (currBullet.x==64 && targetsHit[1]==0) killTarget(currBullet,1); + if (currBullet.x==104 && targetsHit[2]==0) killTarget(currBullet,2); + removeBullet(); + } mutex.unlock(); Thread::wait(100); @@ -136,19 +201,13 @@ if(play) { mutex.lock(); if (levelNum == 1) { - obsArr[0].setDirection(1); obsArr[0].move(3); } else if (levelNum == 2) { - obsArr[0].setDirection(1); obsArr[0].move(6); - obsArr[1].setDirection(0); obsArr[1].move(6); } else if (levelNum == 3) { - obsArr[0].setDirection(1); obsArr[0].move(9); - obsArr[1].setDirection(0); obsArr[1].move(9); - obsArr[2].setDirection(1); obsArr[2].move(9); } mutex.unlock(); @@ -158,38 +217,34 @@ } void shoot() { - bullet.drawBullet(player1.getLocation(), 115); - isBullet = 1; + if (!isBullet) { + bullet.drawBullet(player1.getLocation(), 115); + isBullet = 1; + + FILE *wave_file; + wave_file=fopen("/sd/wavfiles/shoot.wav","r"); + waver.play(wave_file); + fclose(wave_file); + } } -void checkIfHit(); - -void gameOver(); - - - -void handleObstacleHit(); - -void handleTargetHit(); - -void redrawTarget(); - int main() { levelNum = 1; isBullet = 0; Thread bulletThread(movingBullet); Thread obstacleThread(movingObs); + startGame(); while(1) { - - startGame(); - //uLCD.rectangle(0,0,100,100, 0xffffff); initializeLevel(); play = 1; while (play) { //actual game play code + if(targetsLeft==0) { + play = 0; + levelNum++; + } if(!right.read()) player1.moveRight(); if(!left.read()) player1.moveLeft(); if(!center.read()) shoot(); - //if(isBullet) bullet.move(); wait(0.12); } }
diff -r 3f7b826ad15e -r e09ab0d14d4b Obstacle.h --- a/Obstacle.h Sat Oct 29 20:02:57 2016 +0000 +++ b/Obstacle.h Mon Oct 31 18:15:21 2016 +0000 @@ -54,9 +54,8 @@ void Obstacle::move(int speed) { mutex.lock(); Obstacle::eraseObstacle(); - if (x1<=1 || x2>=127) { + if ((x1<=1 && direction==0) || (x2>=127 && direction==1)) { Obstacle::changeDirection(); - myled1=!myled1; } if (direction==1) { x1 += speed;
diff -r 3f7b826ad15e -r e09ab0d14d4b Shooter.h --- a/Shooter.h Sat Oct 29 20:02:57 2016 +0000 +++ b/Shooter.h Mon Oct 31 18:15:21 2016 +0000 @@ -1,5 +1,6 @@ extern uLCD_4DGL uLCD; extern Mutex mutex; +extern wave_player waver; class Shooter { @@ -39,6 +40,11 @@ if (x==104) x = 64; Shooter::drawShooter(); mutex.unlock(); + + FILE *wave_file; + wave_file=fopen("/sd/wavfiles/move.wav","r"); + waver.play(wave_file); + fclose(wave_file); } void Shooter::moveRight() { @@ -49,6 +55,11 @@ if (x==24) x = 64; Shooter::drawShooter(); mutex.unlock(); + + FILE *wave_file; + wave_file=fopen("/sd/wavfiles/move.wav","r"); + waver.play(wave_file); + fclose(wave_file); } int Shooter::getLocation() {