WITB2
Dependencies: mbed 4DGL-uLCD-SE PinDetect
Revision 5:ed52ac77cdf9, committed 2018-11-29
- Comitter:
- Nydrel
- Date:
- Thu Nov 29 18:37:00 2018 +0000
- Parent:
- 4:d128df681d05
- Commit message:
- witb 2;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r d128df681d05 -r ed52ac77cdf9 main.cpp --- a/main.cpp Tue Nov 14 17:33:09 2017 +0000 +++ b/main.cpp Thu Nov 29 18:37:00 2018 +0000 @@ -1,7 +1,7 @@ #include "mbed.h" #include "MMA8452.h" #include "uLCD_4DGL.h" -//#include "PinDetect.h" +#include "PinDetect.h" #include "Speaker.h" #include <ctime> @@ -62,6 +62,21 @@ _,_,_,_,_,_,_,_,_,_ }; + +int earth_destroy[EARTH_WIDTH * EARTH_HEIGHT] = +{ + R,R,R,R,R,R,R,R,R,R, + R,R,r,_,_,r,_,_,r,R, + R,R,_,r,_,r,_,r,_,R, + R,R,_,_,r,r,r,_,_,R, + R,R,r,r,r,r,r,r,r,R, + R,R,_,_,r,r,r,_,_,R, + R,R,_,r,_,r,_,r,_,R, + R,R,r,_,_,r,_,_,r,R, + R,R,R,R,R,R,R,R,R,R, + R,R,R,R,R,R,R,R,R,R +}; + int asteroid_sprite_1[ASTEROID_HEIGHT * ASTEROID_WIDTH] = { _,_,_,_,X,X,X,X,X,X,X,X,_,_,_, _,_,_,X,_,_,_,_,_,_,_,_,X,_,_, @@ -143,6 +158,7 @@ }; + //Instantiate Accelerometer Object Serial pc(USBTX,USBRX); //not used in this program @@ -153,12 +169,25 @@ DigitalOut myled(LED1); uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object -//Speaker mySpeaker(p21); +Speaker mySpeaker(p21); + +//Setup pushbutton for bomb +PinDetect pb1(p16); int factor = 50; int offset = 63; +bool BombUsed = false; +bool ExplodeAllAsteroids = false; +void pb1_hit_callback (void) +{ + if (!BombUsed ) + { + ExplodeAllAsteroids = true; + BombUsed = true; + } +} /*-----ScreenObject Class------*/ @@ -186,6 +215,16 @@ virtual void draw() = 0; virtual void update() = 0; virtual bool boundaryCheck() = 0; + virtual void reset() = 0; + + friend bool overlap(ScreenObject & objectA, ScreenObject & objectB) + { + bool rangeX = (objectA.x_pos <= objectB.x_pos + 7) && (objectA.x_pos + 8 >= objectB.x_pos); + bool rangeY = (objectA.y_pos <= objectB.y_pos + 7) && (objectA.y_pos + 7 >= objectB.y_pos); + return (rangeX && rangeY); + } +//you put stuff in here! + protected: int x_pos; int y_pos; @@ -222,6 +261,11 @@ } //end else }//end if + void explode() { + uLCD.BLIT(x_pos, y_pos, EARTH_WIDTH, EARTH_HEIGHT,earth_blank); + uLCD.BLIT(x_pos, y_pos, EARTH_WIDTH, EARTH_HEIGHT,earth_destroy); + } + bool boundaryCheck() { if(((x_pos == 0) || (x_pos == 126)) || ((y_pos == 0) || (y_pos == 126))) @@ -230,8 +274,18 @@ return false; } + void reset() + { + x_pos = 63; + y_pos = 63; + } + /* - + bool boundaryCheck(* ScreenObject asteroid) + { + //compare the four corners of the Earth and asteroid sprites + bool c1 = (asteroid->x_pos) <= + } */ }; @@ -249,71 +303,84 @@ switch (corner) { case 1: //top left - x_pos = 1; - y_pos = 1; + x_pos = 2; + y_pos = 2; deltaX = rand() % 3 + 1; deltaY = rand() % 3 + 1; break; case 2: //top right - x_pos = 125; - y_pos = 1; + x_pos = 124 - ASTEROID_WIDTH; + y_pos = 2; deltaX = -1*(rand() % 3 + 1); deltaY = rand() % 3 + 1; break; case 3: //bottom left - x_pos = 1; - y_pos = 125; + x_pos = 2; + y_pos = 124 - ASTEROID_HEIGHT; deltaX = rand() % 3 + 1; deltaY = -1*(rand() % 3 + 1); break; case 4: //bottom right - x_pos = 125; - y_pos = 125; + x_pos = 124 - ASTEROID_WIDTH; + y_pos = 124 - ASTEROID_HEIGHT; deltaX = -1*(rand() % 3 + 1); deltaY = -1*(rand() % 3 + 1); break; } } - void pickCorner() + void pickEdge() { - int corner = rand() % 4 + 1; + int edge = rand() % 4 + 1; - switch (corner) + switch (edge) { - case 1: //top left - x_pos = 1; - y_pos = 1; - deltaX = rand() % 3 + 1; + case 1: //top + x_pos = rand() % 2 + (120 - ASTEROID_WIDTH); + y_pos = 2; + if((rand() % 1)) + deltaX = rand() % 3 + 1; + else + deltaX = -1*(rand() % 3 + 1); deltaY = rand() % 3 + 1; break; - case 2: //top right - x_pos = 125; - y_pos = 1; - deltaX = -1*(rand() % 3 + 1); - deltaY = rand() % 3 + 1; - break; - case 3: //bottom left - x_pos = 1; - y_pos = 125; - deltaX = rand() % 3 + 1; + case 2: //bottom + x_pos = rand() % 2 + (120 - ASTEROID_WIDTH); + y_pos = 120 - ASTEROID_HEIGHT; + if((rand() % 1)) + deltaX = rand() % 3 + 1; + else + deltaX = -1*(rand() % 3 + 1); deltaY = -1*(rand() % 3 + 1); break; - case 4: //bottom right - x_pos = 125; - y_pos = 125; + case 3: //left + x_pos = 2; + y_pos = rand() % 1 + (120 - ASTEROID_HEIGHT); + deltaX = rand() % 3 + 1; + if((rand() % 1)) + deltaY = rand() % 3 + 1; + else + deltaY = -1*(rand() % 3 + 1); + break; + case 4: //right + x_pos = 120 - ASTEROID_WIDTH; + y_pos = rand() % 1 + (120 - ASTEROID_HEIGHT); deltaX = -1*(rand() % 3 + 1); - deltaY = -1*(rand() % 3 + 1); + if((rand() % 1)) + deltaY = rand() % 3 + 1; + else + deltaY = -1*(rand() % 3 + 1); break; } } + bool boundaryCheck() { - bool c1 = (deltaX > 1) && (x_pos >= 126); //if moving towards right side of screen - bool c2 = (deltaX < 1) && (x_pos + ASTEROID_WIDTH <= 0); //if moving towards left side of screen - bool c3 = (deltaY > 1) && (y_pos >= 126); //if moving towards bottom of screen - bool c4 = (deltaY < 1) && (y_pos + ASTEROID_HEIGHT <= 0); //if moving towards top of screen + bool c1 = x_pos + ASTEROID_WIDTH > 124; //if moving towards right side of screen + bool c2 = x_pos <= 0; //if moving towards left side of screen + bool c3 = y_pos + ASTEROID_HEIGHT > 124; //if moving towards bottom of screen + bool c4 = y_pos <= 0; //if moving towards top of screen if(c1 || c2 || c3 || c4) return true; @@ -340,18 +407,14 @@ startTime = time(0); } - Asteroid1() + void reset() { - pickCorner(); - startTime = time(0); - } - - ~Asteroid1(void) - { + pickEdge(); + startTime = time(0); } void draw() - { + { uLCD.BLIT(x_pos, y_pos, ASTEROID_WIDTH, ASTEROID_HEIGHT,asteroid_sprite_1); } @@ -364,6 +427,7 @@ y_pos = y_pos + (deltaY * timeElapsed); } + protected: time_t startTime; }; @@ -379,10 +443,10 @@ startTime = time(0); } - Asteroid2() + void reset() { - pickCorner(); - startTime = time(0); + pickEdge(); + startTime = time(0); } void draw() @@ -416,10 +480,10 @@ startTime = time(0); } - Asteroid3() + void reset() { - pickCorner(); - startTime = time(0); + pickEdge(); + startTime = time(0); } void draw() @@ -436,6 +500,7 @@ y_pos = y_pos + (deltaY * timeElapsed); } + protected: time_t startTime; }; @@ -452,10 +517,10 @@ startTime = time(0); } - Asteroid4() + void reset() { - pickCorner(); - startTime = time(0); + pickEdge(); + startTime = time(0); } void draw() @@ -472,6 +537,7 @@ y_pos = y_pos + (deltaY * timeElapsed); } + protected: time_t startTime; }; @@ -479,7 +545,6 @@ - //Program Start int main() @@ -489,25 +554,53 @@ srand(time(0)); // do this srand call here ONLY... no where else in the code! ScreenObject * ActiveAsteroids[NUM_ASTEROIDS]; SpaceShipEarth ship; //instantiate a SpaceShipEarth object + + pb1.mode(PullUp); + wait(0.01); + pb1.attach_deasserted(&pb1_hit_callback); + pb1.setSampleFrequency(); + + double bar = 127; - Asteroid1 rock1(1); - //Asteroid2 rock2(2); - //Asteroid3 rock3(3); - //Asteroid4 rock4(4); + Asteroid1 rock1(1); + Asteroid2 rock2(2); + Asteroid3 rock3(3); + Asteroid4 rock4(4); - ActiveAsteroids[0] = &rock1; - //ActiveAsteroids[1] = &rock2; - //ActiveAsteroids[2] = &rock3; - //ActiveAsteroids[3] = &rock4; + ActiveAsteroids[0] = &rock1; + ActiveAsteroids[1] = &rock2; + ActiveAsteroids[2] = &rock3; + ActiveAsteroids[3] = &rock4; //set parameters -- use these and don't change acc.setBitDepth(MMA8452::BIT_DEPTH_12); acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G); acc.setDataRate(MMA8452::RATE_100); - + + + time_t startTime; + time_t timeElapsed; + startTime = time(0); + + int dur = 30; while(1) { + //check winning condition + if((time(0)-startTime) > dur) + { + uLCD.printf("YOU WIN!"); + return 1; + } + + else + { + timeElapsed = time(0)-startTime; + bar = 127 - (timeElapsed*(127/dur)); + uLCD.filled_rectangle(1, 1,127,1,R); + uLCD.filled_rectangle(1, 1,127-bar,1,_); + } + ship.update(); ship.draw(); @@ -516,62 +609,94 @@ ActiveAsteroids[0]->update(); if(ActiveAsteroids[0]->boundaryCheck()) - { - wait(0.2); - delete ActiveAsteroids[0]; - ActiveAsteroids[0] = NULL; - Asteroid1 rock1; - ActiveAsteroids[0] = &rock1; - } + ActiveAsteroids[0]->reset(); ActiveAsteroids[0]->draw(); - - /* ActiveAsteroids[1]->update(); if(ActiveAsteroids[1]->boundaryCheck()) - { - wait(0.2); - delete ActiveAsteroids[1]; - ActiveAsteroids[1] = NULL; - Asteroid1 rock2; - ActiveAsteroids[1] = &rock2; - } + ActiveAsteroids[1]->reset(); ActiveAsteroids[1]->draw(); - - + ActiveAsteroids[2]->update(); - if(ActiveAsteroids[2->boundaryCheck()) - { - wait(0.2); - delete ActiveAsteroids[2]; - ActiveAsteroids[2] = NULL; - Asteroid1 rock3; - ActiveAsteroids[2] = &rock3; - } + if(ActiveAsteroids[2]->boundaryCheck()) + ActiveAsteroids[2]->reset(); ActiveAsteroids[2]->draw(); - - - ActiveAsteroids[3]->update(); + + ActiveAsteroids[3]->update(); if(ActiveAsteroids[3]->boundaryCheck()) + ActiveAsteroids[3]->reset(); + + ActiveAsteroids[3]->draw(); + + + //check to see if the pushbutton has activated the bomb + if(ExplodeAllAsteroids) { - wait(0.2); - delete ActiveAsteroids[3]; - ActiveAsteroids[3] = NULL; - Asteroid1 rock4; - ActiveAsteroids[3] = &rock4; + for(int i = 0; i < 4; i++) + { + ActiveAsteroids[i]->update(); + wait(0.25); + ActiveAsteroids[i]->reset(); + } + ExplodeAllAsteroids = false; } - ActiveAsteroids[3]->draw(); - */ + //check for overlap between Asteroids and Earth + //return 1 and end game + if(overlap(ship,rock1)) { + ship.explode(); + mySpeaker.PlayNote(500.0,0.25,0.1); + mySpeaker.PlayNote(450.0,0.25,0.1); + mySpeaker.PlayNote(400.0,0.25,0.1); + mySpeaker.PlayNote(300.0,0.25,0.1); + uLCD.printf("GAME OVER"); + wait(3); + return 1; + } + + else if(overlap(ship,rock2)) { + ship.explode(); + mySpeaker.PlayNote(500.0,0.25,0.1); + mySpeaker.PlayNote(450.0,0.25,0.1); + mySpeaker.PlayNote(400.0,0.25,0.1); + mySpeaker.PlayNote(300.0,0.25,0.1); + uLCD.printf("GAME OVER"); + wait(3); + return 1; + } + else if(overlap(ship,rock3)) { + ship.explode(); + mySpeaker.PlayNote(500.0,0.25,0.1); + mySpeaker.PlayNote(450.0,0.25,0.1); + mySpeaker.PlayNote(400.0,0.25,0.1); + mySpeaker.PlayNote(300.0,0.25,0.1); + uLCD.printf("GAME OVER"); + wait(3); + return 1; + } + + else if(overlap(ship,rock4)) { + ship.explode(); + mySpeaker.PlayNote(500.0,0.25,0.1); + mySpeaker.PlayNote(450.0,0.25,0.1); + mySpeaker.PlayNote(400.0,0.25,0.1); + mySpeaker.PlayNote(300.0,0.25,0.1); + uLCD.printf("GAME OVER"); + wait(3); + return 1; + } + + + //check to see if the game is over + } //end infinite while loop } //end main -