SharpShooter
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
MainGame.cpp
- Committer:
- jboettcher
- Date:
- 2016-10-29
- Revision:
- 13:67e79a582164
- Parent:
- 12:2f358065ba3f
- Child:
- 15:e09ab0d14d4b
File content as of revision 13:67e79a582164:
#include "mbed.h" #include "Speaker.h" #include "uLCD_4DGL.h" #include "SDFileSystem.h" #include "wave_player.h" #include "Nav_Switch.h" #include "rtos.h" #include "Obstacle.h" #include "Shooter.h" #include "Bullet.h" DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4); InterruptIn center(p25); 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"); AnalogOut DACout(p18); wave_player waver(&DACout); Shooter player1; Bullet bullet; bool isBullet; 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*/ int numTries; int levelNum; BulletLocation bulletLocation; ObstLocation * obstaclePtr; Obstacle obsArr[3]; TargetLocation * targetsPtr = new TargetLocation[3]; bool play = 0; void startGame() { uLCD.background_color(BACKGROUND); uLCD.locate(0,0); //uLCD.set_font_size(44, 44); uLCD.printf("\nSharp Shooter!!\n"); wait(0.1); FILE *wave_file; wave_file=fopen("/sd/wavfiles/pitfall.wav","r"); waver.play(wave_file); fclose(wave_file); wait(2); 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); } void createShooter() { player1.drawShooter(); } void createObstacles(int num) { obstaclePtr = new ObstLocation[num]; //dummy x values, CHANGE LATER if (num == 1) { 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); } 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 uLCD.locate(0,0); uLCD.printf("LEVEL:%d BULLETS:%d", levelNum, numTries); createTargets(); createObstacles(levelNum); createShooter(); } void movingBullet(void const *args) { while (true) { if(isBullet) { mutex.lock(); bullet.move(); mutex.unlock(); Thread::wait(100); } } } void movingObs(void const *args) { while (true) { 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(); Thread::wait(500); } } } void shoot() { bullet.drawBullet(player1.getLocation(), 115); isBullet = 1; } void checkIfHit(); void gameOver(); void handleObstacleHit(); void handleTargetHit(); void redrawTarget(); int main() { levelNum = 1; isBullet = 0; Thread bulletThread(movingBullet); Thread obstacleThread(movingObs); while(1) { startGame(); //uLCD.rectangle(0,0,100,100, 0xffffff); initializeLevel(); play = 1; while (play) { //actual game play code if(!right.read()) player1.moveRight(); if(!left.read()) player1.moveLeft(); if(!center.read()) shoot(); //if(isBullet) bullet.move(); wait(0.12); } } }