World In the Balance

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WITB.cpp Source File

WITB.cpp

00001 #include "mbed.h"
00002 #include "WITB.h"
00003 
00004 uLCD_4DGL myLCD(p9, p10, p11);
00005 Speaker mySpeaker(p21);
00006 PinDetect pb1(p19);
00007 PinDetect pb2(p18);
00008 PinDetect pb3(p17);
00009 Serial pc(USBTX,USBRX);
00010 
00011 
00012 
00013 #define ASTEROID_HEIGHT 12
00014 #define ASTEROID_WIDTH 15
00015 #define SPRITE_MAX 15
00016 #define EARTH_WIDTH 10
00017 #define EARTH_HEIGHT 10
00018 #define EXPLOSION1_WIDTH 20
00019 #define SCREEN_MAX 125
00020 #define SCREEN_MIN 1
00021 #define NUM_ASTEROIDS 4
00022 #define Q 0x808000 //OLIVE 
00023 #define I 0x008000 //GREEN 
00024 #define S 0xC0C0C0 //SILVER 
00025 #define C 0x17202A //UFO GLASS 
00026 #define D 0x797D7F //DARK GREY 
00027 #define L 0x00FF00 //LIME 
00028 #define P 0xFF00FF //PINK 
00029 #define R 0xF1C40F //YELLOW 
00030 #define O 0xF39C12 //ORANGE 
00031 #define G 0xAAB7B8 //GREY 
00032 #define _ 0x000000 //BLACK 
00033 #define X 0xFFFFFF //WHITE 
00034 #define B 0x0000FF //BLUE 
00035 #define r 0xFF0000 //RED
00036 
00037 void clear(){
00038     myLCD.cls();   
00039 }
00040 
00041 int asteroid_sprite_1[ASTEROID_HEIGHT * ASTEROID_WIDTH] = {
00042 _,_,_,_,X,X,X,X,X,X,X,X,_,_,_, 
00043 _,_,_,X,_,_,_,_,_,_,_,_,X,_,_, 
00044 _,_,X,_,_,_,_,_,_,_,_,_,X,_,_, 
00045 _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00046 X,X,X,X,_,_,_,_,_,_,_,_,_,X,_, 
00047 _,_,_,X,_,_,_,_,_,_,_,_,_,X,_, 
00048 _,_,X,_,_,_,_,_,_,_,_,_,_,X,_, 
00049 _,X,_,_,_,_,_,X,_,_,_,_,_,X,_, 
00050 X,_,_,_,_,_,X,X,_,_,_,_,X,_,_, 
00051 _,X,_,_,_,X,_,X,_,_,_,_,X,_,_, 
00052 _,_,X,_,X,_,_,X,_,_,_,X,_,_,_, 
00053 _,_,_,X,_,_,_,X,X,X,X,_,_,_,_ 
00054 };
00055 
00056 int asteroid_sprite_2[ASTEROID_HEIGHT * ASTEROID_WIDTH] = {
00057 _,_,_,_,X,X,X,_,_,_,X,X,_,_,_, 
00058 _,_,_,X,_,_,_,X,_,X,_,_,X,_,_, 
00059 _,_,X,_,_,_,_,_,X,_,_,_,_,X,_, 
00060 _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00061 X,_,_,_,_,_,_,_,_,_,_,X,_,_,_, 
00062 X,_,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00063 X,_,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00064 X,_,_,_,_,_,_,_,_,_,_,_,_,X,_, 
00065 X,_,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00066 _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00067 _,_,X,_,_,_,_,_,_,_,_,X,_,_,_, 
00068 _,_,_,X,X,X,X,X,X,X,X,_,_,_,_ 
00069 };
00070 
00071 int asteroid_sprite_3[ASTEROID_HEIGHT * ASTEROID_WIDTH] = {
00072 _,_,_,_,X,X,_,_,_,_,X,X,_,_,_, 
00073 _,_,_,X,_,_,X,X,_,X,_,_,X,_,_, 
00074 _,_,X,_,_,_,_,_,X,_,_,_,X,_,_, 
00075 _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00076 X,X,_,_,_,_,_,_,_,_,_,_,_,X,_, 
00077 _,_,X,_,_,_,_,_,_,_,_,_,_,X,_, 
00078 _,_,X,_,_,_,_,_,_,_,_,_,_,X,_, 
00079 _,X,_,_,_,_,_,_,_,_,_,_,_,X,_, 
00080 X,_,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00081 _,X,_,_,_,X,X,X,_,_,_,_,X,_,_, 
00082 _,_,X,_,X,_,_,_,X,_,_,X,_,_,_, 
00083 _,_,_,X,_,_,_,_,_,X,X,_,_,_,_ 
00084 };
00085 
00086 int asteroid_sprite_4[ASTEROID_HEIGHT * ASTEROID_WIDTH] = {
00087 _,_,_,_,X,X,X,X,X,X,X,X,_,_,_, 
00088 _,_,_,X,_,_,_,_,_,_,_,_,X,_,_, 
00089 _,_,X,_,_,_,_,_,_,_,_,_,X,_,_, 
00090 _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00091 X,X,X,X,_,_,_,_,_,_,_,_,X,_,_, 
00092 _,_,_,X,_,_,_,_,_,_,_,X,_,_,_, 
00093 _,_,X,_,_,_,_,_,_,_,X,_,_,_,_, 
00094 _,X,_,_,_,_,_,_,_,_,_,X,_,_,_, 
00095 X,_,_,_,_,_,_,_,_,_,_,_,X,_,_, 
00096 _,X,_,_,_,_,X,X,_,_,_,_,X,_,_, 
00097 _,_,X,_,X,X,_,X,_,_,_,X,_,_,_, 
00098 _,_,_,X,_,_,_,_,X,X,X,_,_,_,_ 
00099 };
00100 
00101 int spaceship_earth1[EARTH_WIDTH *EARTH_HEIGHT] = {
00102 _,_,S,S,S,S,S,S,_,_, 
00103 _,S,I,I,I,I,I,I,S,_, 
00104 S,I,I,I,I,I,I,I,I,S, 
00105 S,I,I,I,I,I,I,I,I,S, 
00106 S,I,I,I,I,I,I,I,I,S, 
00107 S,I,I,I,I,I,I,I,I,S, 
00108 S,I,I,I,I,I,I,I,I,S, 
00109 S,I,I,I,I,I,I,I,I,S, 
00110 S,I,I,I,I,I,I,I,S,_, 
00111 _,S,S,S,S,S,S,S,_,_, 
00112 };
00113 
00114 int explosion[EARTH_WIDTH *EARTH_HEIGHT] = {
00115 r,_,_,_,_,_,_,_,_,r, 
00116 _,r,O,_,_,_,_,O,r,_, 
00117 _,O,r,O,O,O,O,r,O,_, 
00118 _,_,O,r,r,r,r,O,_,_, 
00119 _,_,O,r,R,R,r,O,_,_, 
00120 _,_,O,r,R,R,r,O,_,_, 
00121 _,_,O,r,r,r,r,O,_,_, 
00122 _,O,r,O,O,O,O,r,O,_, 
00123 _,r,O,_,_,_,_,O,r,_, 
00124 r,_,_,_,_,_,_,_,_,r, 
00125 };
00126 
00127 void ScreenObject::setXPos(int x) {xPos = x;}
00128 void ScreenObject::setYPos(int y) {yPos = y;}
00129 int ScreenObject::getXPos() {return xPos;}
00130 int ScreenObject::getYPos() {return yPos;}
00131 void ScreenObject::setBaud() {myLCD.baudrate(600000);}
00132 
00133 bool overlap(ScreenObject & objectA, ScreenObject & objectB)
00134 {
00135     if ((objectA.getXPos() < (objectB.getXPos() + 10)) and (objectA.getXPos() > objectB.getXPos()))
00136     {
00137         if ((objectA.getYPos() > objectB.getYPos()) and (objectA.getYPos() < (objectB.getYPos() + 10))){
00138             return true;
00139         }else if (((objectA.getYPos()+10) > objectB.getYPos()) and ((objectA.getYPos()+10) < (objectB.getYPos() + 10))){
00140             return true;
00141         }else{
00142             return false;   
00143         }
00144     }else if (((objectA.getXPos()+ 10) < (objectB.getXPos() + 10)) and ((objectA.getXPos() + 10) > objectB.getXPos())){
00145         if ((objectA.getYPos() > objectB.getYPos()) and (objectA.getYPos() < (objectB.getYPos() + 10))){
00146             return true;
00147         }else if (((objectA.getYPos()+10) > objectB.getYPos()) and ((objectA.getYPos()+10) < (objectB.getYPos() + 10))){
00148             return true;
00149         }else{
00150             return false;   
00151         }
00152     }else{
00153         return false;   
00154     }
00155 }
00156 
00157 void loseGame(ScreenObject & A)
00158 {
00159     clear();
00160     myLCD.BLIT(A.getXPos(), A.getYPos(), 10, 10, explosion);
00161     mySpeaker.PlayNote(600, 0.15, 0.05);
00162     mySpeaker.PlayNote(700, 0.15, 0.05);
00163     mySpeaker.PlayNote(800, 0.15, 0.05);
00164     mySpeaker.PlayNote(600, 0.15, 0.05);
00165     wait(3);
00166     clear();
00167     myLCD.color(WHITE);
00168     myLCD.text_width(2);
00169     myLCD.text_height(2);
00170     myLCD.locate(0,12);
00171     myLCD.printf("LOSER!");
00172     while(1){
00173         myLCD.printf("LOSER!");
00174     }
00175 }
00176 
00177 
00178 void EarthSS::setdx(int x) {dx = x;}
00179 void EarthSS::setdy(int y) {dy = y;}
00180 int EarthSS::getdx(){return dx;}
00181 int EarthSS::getdy(){return dy;}
00182 
00183 void EarthSS::draw()
00184 {
00185    this->setXPos(64);
00186    this->setYPos(64);
00187    myLCD.BLIT(this->getXPos(), this->getYPos(), 10, 10, spaceship_earth1);
00188 }
00189 
00190 void EarthSS::update()
00191 {
00192     this->setXPos(this->getXPos() - this->getdx());
00193     myLCD.BLIT(this->getXPos(), this->getYPos(), 10, 10, spaceship_earth1);
00194 }
00195 
00196 void AbstractAsteroid::setdx(int x) {dx = x;}
00197 void AbstractAsteroid::setdy(int y) {dy = y;}
00198 int AbstractAsteroid::getdx() {return dx;}
00199 int AbstractAsteroid::getdy() {return dy;}
00200 
00201 
00202 
00203 void ConcreteAsteroid1::draw()
00204 {
00205     this->setXPos(rand() % 110);
00206     this->setYPos(10);
00207 }
00208 
00209 void ConcreteAsteroid1::update()
00210 {
00211     this->setYPos(this->getYPos() + this->getdx());
00212     this->setXPos(this->getXPos() + this->getdy());
00213     if ( this->getXPos() < 0)
00214     {
00215         this->setXPos(110);   
00216     }
00217     if ( this->getXPos() > 110 )
00218     {
00219         this->setXPos(0);   
00220     }
00221     myLCD.BLIT(this->getXPos(), this->getYPos(), 15, 12, asteroid_sprite_1);
00222 }
00223 
00224 void ConcreteAsteroid2::draw()
00225 {
00226     this->setXPos(rand() % 110);
00227     this->setYPos(0);
00228 }
00229 
00230 void ConcreteAsteroid2::update()
00231 {
00232     this->setYPos(this->getYPos() + this->getdx());
00233     this->setXPos(this->getXPos() + this->getdy());
00234     if ( this->getXPos() < 0)
00235     {
00236         this->setXPos(110);   
00237     }
00238     if ( this->getXPos() > 110 )
00239     {
00240         this->setXPos(0);   
00241     }
00242     myLCD.BLIT(this->getXPos(), this->getYPos(), 15, 12, asteroid_sprite_2);
00243 }
00244 
00245 void ConcreteAsteroid3::draw()
00246 {
00247     this->setXPos(rand() % 110);
00248     this->setYPos(0);
00249 }
00250 
00251 void ConcreteAsteroid3::update()
00252 {
00253     this->setYPos(this->getYPos() + this->getdx());
00254     this->setXPos(this->getXPos() + this->getdy());
00255     if ( this->getXPos() < 0)
00256     {
00257         this->setXPos(110);   
00258     }
00259     if ( this->getXPos() > 110 )
00260     {
00261         this->setXPos(0);   
00262     }
00263     myLCD.BLIT(this->getXPos(), this->getYPos(), 15, 12, asteroid_sprite_3);
00264 }
00265 
00266 void ConcreteAsteroid4::draw()
00267 {
00268     this->setXPos(rand() % 110);
00269     this->setYPos(0);
00270 }
00271 
00272 void ConcreteAsteroid4::update()
00273 {
00274     this->setYPos(this->getYPos() + this->getdx());
00275     this->setXPos(this->getXPos() + this->getdy());
00276     if ( this->getXPos() < 0)
00277     {
00278         this->setXPos(110);   
00279     }
00280     if ( this->getXPos() > 110 )
00281     {
00282         this->setXPos(0);   
00283     }
00284     myLCD.BLIT(this->getXPos(), this->getYPos(), 15, 12, asteroid_sprite_4);
00285 }
00286 
00287 
00288 void GameTimer::setY(int Y) {this->y = Y;}
00289 int GameTimer::getX(){return x;}
00290 int GameTimer::getY(){return y;}
00291 void GameTimer::setdx(int x) {this->dx = x;}
00292 void GameTimer::setdy(int y) {this->dy = y;}
00293 int GameTimer::getdx() {return dx;}
00294 int GameTimer::getdy() {return dy;}
00295 
00296 void GameTimer::draw()
00297 {
00298     StartTime = time(0); 
00299     ElapsedTime = time(0) - StartTime;  
00300     this->setX(128);
00301     this->setY(10);
00302     myLCD.filled_rectangle(this->getXPos(), this->getYPos(), this->getX(), this->getY(), B);   
00303 }
00304 
00305 void GameTimer::update()
00306 {
00307     float t = 4.26;
00308     ElapsedTime = time(0) - StartTime;
00309     this->setX(128 - floor(t * ElapsedTime));
00310     myLCD.filled_rectangle(this->getXPos(), this->getYPos(), this->getX(), this->getY(), B);  
00311     if (this->getX() < 3)
00312     {
00313         winGame();
00314     }     
00315 }
00316 
00317 void winGame(){
00318     clear();
00319     myLCD.color(WHITE);
00320     myLCD.text_width(2);
00321     myLCD.text_height(2);
00322     myLCD.locate(0,12);
00323     myLCD.printf("WINNER!");
00324     while(1)
00325     {
00326         myLCD.printf("WINNER!");
00327     } 
00328 }
00329 
00330 void endGame(){
00331     while(1){}   
00332 }