Daniel Nguyen
Dependencies: 4DGL-uLCD-SE mbed
main.cpp
- Committer:
- dnguyen314
- Date:
- 2017-11-02
- Revision:
- 0:7d7f6032c719
File content as of revision 0:7d7f6032c719:
#include "mbed.h" #include "Bullet.h" #include "ScreenAliens.h" #include "AlienBob.h" #include "AlienAlice.h" #include "AlienMoo.h" #include "AlienJun.h" #include "SpaceShip.h" #include "uLCD_4DGL.h" #include "Speaker.h" #include <stdio.h> #include <cstdlib> #include <iostream> #include <ctime> #include <time.h> #define ALIEN_HEIGHT 8 #define ALIEN_WIDTH 11 #define _ 0x000000 //BLACK #define X 0xFFFFFF //WHITE using namespace std; DigitalOut myLed1(LED1); DigitalOut myLed2(LED2); DigitalOut myLed3(LED3); DigitalIn pbLeft(p8); DigitalIn pbRight(p9); DigitalIn pbShoot(p10); Speaker mySpeaker(p21); uLCD_4DGL uLCD(p28,p27,p29); Timer t; int main() { //Initializations uLCD.cls(); uLCD.baudrate(600000); wait(0.2); pbLeft.mode(PullUp); myLed1 = pbLeft; pbRight.mode(PullUp); myLed2 = pbRight; pbShoot.mode(PullUp); myLed3 = pbShoot; wait(0.2); //Starts timer for game t.start(); uLCD.display_control(LANDSCAPE); /////////Creates SpaceShip//////// SpaceShip ship; ////////Creates Bullet///// Bullet shot; // Though does not appear yet until fired bool shotBullet = false; ///////Creates Aliens////// srand (time(NULL)); ScreenAliens ** aliens = new ScreenAliens*[6]; // Will hold pointers to the six different aliens int x = 0; int posX = 0; int posY = 30; for (int i = 0; i < 6; i++) // Will randomly create the aliens { x = rand() % 4 + 1; posX = rand() % 100 + 1; switch(x) { case 1: aliens[i] = new AlienBob(posX, posY); aliens[i]->isLeft(1); //Decides is alien should first move left or right break; case 2: aliens[i] = new AlienAlice(posX, posY); aliens[i]->isLeft(0); break; case 3: aliens[i] = new AlienMoo(posX, posY); aliens[i]->isLeft(1); break; case 4: aliens[i] = new AlienJun(posX, posY); aliens[i]->isLeft(0); break; default: break; } posY += 10; //Positions the next alien 10 pixels down } int numOfAliens = 6; // When alien is hit, counter goes down. // When 0, game ends and player wins. bool start = true; //////GAME STARTS HERE//////// while (start) { uLCD.rectangle(2, 2, 125, 125, RED); ////Take SpaceShip Input//// if (!pbRight) { ship.move(3); } if (!pbLeft) { ship.move(-3); } if (!pbShoot && (shotBullet != true)) { //Plays sounds when shooting mySpeaker.PlayNote(329, .04, 0.015); mySpeaker.PlayNote(370, .04, 0.015); shot.setPosX(ship.getPosX()); if ((shot.getPosY() > -3) || (shot.getPosY() < 105)) { shotBullet = true; // Shot has been fired and it's behavior is decided } // by condition codes below } for(int i = 0; i < 6; i++) { aliens[i]->update(); for (int j = (aliens[i]->getPosX()-2); j < (aliens[i]->getPosX()+10); j++) { if (j == shot.getPosX()) { for (int k = (aliens[i]->getPosY()+2); k < (aliens[i]->getPosY()+8); k++) { if (k == shot.getPosY()) { //Plays sound when an alien is hit mySpeaker.PlayNote(440, .015, 0.015); mySpeaker.PlayNote(494, .01, 0.015); shot.collide(); aliens[i]->collide(); shotBullet = false; // Cannot shoot bullet again until has left screen shot.setPosY(105); // Resets position of bullet numOfAliens--; break; } } } } if (shot.getPosY() < -3) //When bullet leaves screen then bullet actual resets { // original position and does not move forward until shotBullet = false; // fired again shot.setPosY(105); } else if (shotBullet) // If shot is fired and has not left screen then { // shot moves forward until it has left the screen shot.draw(); } } if (numOfAliens == 0) // Conditions where player wins { start = false; } } if (start == false) { int xtime = t.read(); uLCD.cls(); uLCD.rectangle(2, 2, 125, 125, GREEN); uLCD.printf(" "); uLCD.printf(" "); uLCD.printf(" "); uLCD.printf(" "); uLCD.printf(" CONGRATS YOU WON "); uLCD.printf(" SIR OR NOT SIR. "); uLCD.printf(" TIME:"); uLCD.printf("%d SEC", xtime); uLCD.printf(" "); uLCD.printf(" "); uLCD.printf(" GOOD DAY "); uLCD.printf(" AND "); uLCD.printf(" GOOD LIFE "); uLCD.rectangle(2, 2, 125, 125, GREEN); mySpeaker.PlayNote(658, .125, 0.015); mySpeaker.PlayNote(1320, .500, 0.015); mySpeaker.PlayNote(990, .250, 0.015); mySpeaker.PlayNote(1056, .250, 0.015); mySpeaker.PlayNote(1188, .250, 0.015); mySpeaker.PlayNote(1320, .125, 0.015); mySpeaker.PlayNote(1188, .125, 0.015); mySpeaker.PlayNote(1056, .250, 0.015); mySpeaker.PlayNote(990, .250, 0.015); mySpeaker.PlayNote(880, .500, 0.015); mySpeaker.PlayNote(880, .250, 0.015); mySpeaker.PlayNote(1056, .250, 0.015); mySpeaker.PlayNote(1320, .500, 0.015); mySpeaker.PlayNote(1188, .250, 0.015); mySpeaker.PlayNote(1056, .250, 0.015); mySpeaker.PlayNote(990, .750, 0.015); mySpeaker.PlayNote(1056, .250, 0.015); mySpeaker.PlayNote(1188, .5, 0.015); mySpeaker.PlayNote(1320, .500, 0.015); mySpeaker.PlayNote(1056, .500, 0.015); mySpeaker.PlayNote(880, .500, 0.015); mySpeaker.PlayNote(880, .750, 0.015); } }