Sixiang Miao / Mbed 2 deprecated Miao_Sixiang

Dependencies:   mbed N5110

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* ELEC2645 Embedded Systems Project 
00002 School of Electronic & Electrical Engineering 
00003 University of Leeds 
00004  Name: Miao Sixiang
00005  Username: el16sm
00006  Student ID Number: 201089108
00007  Date: 
00008  */ 
00009  
00010  #include "mbed.h"
00011  #include "Gamepad.h"
00012  #include "N5110.h"
00013  #include "enemies.h"
00014  #include "math.h"
00015  #include "ship.h"
00016  #include "Bitmap.h"
00017  
00018  
00019 #ifdef WITH_TESTING
00020 #include "tests.h"
00021 #endif
00022 
00023 
00024 DigitalOut r_led(LED_RED);
00025 DigitalOut g_led(LED_GREEN);
00026 DigitalOut b_led(LED_BLUE);
00027 DigitalOut led1(PTA1);
00028 DigitalOut led2(PTA2);
00029 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); 
00030 
00031 Gamepad pad;
00032 enemies eni;
00033 ship shi;
00034 
00035 void init();
00036 void welcome();
00037 bool explosionjudge(float x1,float y1,float x2,float y2);
00038 void explosion(int x, int y);
00039 void game_over();
00040 
00041 float xp1,xp2,yp1,yp2,xn1,yn1,xn2,yn2,x,y,x1,y1,x2,y2,v;
00042 int p1,p2,score,high_score,count;
00043 Vector2D enic1 = {xn1, yn1};
00044 Vector2D enic2 = {xn2, yn2};
00045 const int explosion123[10][10] =      {
00046     { 0,0,0,0,1,0,0,0,0,0 },
00047     { 0,1,0,1,0,1,0,0,1,0 },
00048     { 0,1,0,1,0,1,1,1,1,0 },
00049     { 0,0,1,0,0,0,0,1,0,0 },
00050     { 0,1,0,0,0,0,0,0,1,0 },
00051     { 0,1,0,0,0,0,0,0,1,0 },
00052     { 0,0,1,0,0,0,0,1,0,0 },
00053     { 0,1,0,1,0,0,1,1,0,0 },
00054     { 1,0,0,0,1,1,0,0,1,0 },
00055     { 0,0,0,0,0,0,0,0,0,0 },
00056 };
00057 
00058 int main() {
00059     
00060     //initial all the components 
00061     init();//the lcd is initialed
00062     lcd.init();
00063     lcd.normalMode();
00064     lcd.setContrast(0.5);
00065     
00066     
00067     count = 0;
00068     score = 0;
00069     x = 5.0;
00070     y = 21.0;
00071     x1 = 0.0;
00072     y1 = 0.0;
00073     x2 = 0.0;
00074     y2 = 0.0;
00075     v = 4.0;
00076     p1 = p2 = 0;
00077     xp1 = xp2 = xn1 =xn2 =0.0;
00078     yp1 = yp2 = yn1 = yn2 =0.0;
00079     enic1.x = enic2.x = 0.0;
00080     
00081     high_score = 0;
00082     
00083     shi.set_ship(y,x,lcd);
00084     
00085     welcome();
00086         while (1) {
00087         lcd.clear();
00088         Vector2D coord = pad.get_mapped_coord();
00089         x = x + coord.x;
00090         y = y - coord.y;
00091         shi.set_ship(y,x,lcd);
00092         
00093         if (pad.check_event(Gamepad::A_PRESSED)){
00094             if (x1 == 0){
00095                     x1 = x;
00096                     y1 = y;
00097                     x1 = shi.open_fire(x1,y1,v,lcd);
00098                     if (!x2 ==0){
00099                     x2 = shi.open_fire(x2,y2,v,lcd);
00100                     }
00101                 }  else if (x1 > 0 and x2 == 0){
00102                     x2 = x;
00103                     y2 = y;
00104                     x2 = shi.open_fire(x2,y2,v,lcd);
00105                     x1 = shi.open_fire(x1,y1,v,lcd);
00106             } else if (x1 > 0 and x2 > 0){
00107                 x2 = shi.open_fire(x2,y2,v,lcd);
00108                 x1 = shi.open_fire(x1,y1,v,lcd);
00109                 }
00110         } else if (x1 > 0 or x2 > 0) {
00111             if (x1 > 0){
00112             x1 = shi.open_fire(x1,y1,v,lcd);
00113             } 
00114             if (x2 > 0){
00115                 x2 = shi.open_fire(x2,y2,v,lcd);
00116                 }
00117             }
00118         if (x1 >= 84){
00119              x1 = 0;
00120                 }
00121         if (x2 >= 84) {
00122             x2 = 0;
00123             }
00124         
00125         
00126         
00127         eninumber enin = eni.enemiesjudge(enic1.x, enic2.x);
00128         p1 = enin.p1;
00129         p2 = enin.p2;
00130         if (p1 == 1){
00131         enic1 = eni.set_coord1(p1);
00132         }
00133         if (p2 == 1){
00134         enic2 = eni.set_coord1(p2);
00135         }
00136         eni.drawenemies_small(enic1.x, enic1.y,lcd);
00137         eni.drawenemies_large(enic2.x, enic2.y,lcd);
00138         
00139         
00140         if (explosionjudge(enic1.x,enic1.y,x1,y1)) {
00141             explosion(x1, y1);
00142             x1 = 0;
00143             count = count + 1;
00144             enic1.x = 0;
00145             } 
00146         if (explosionjudge(enic2.x,enic2.y,x1,y1)) {
00147             explosion(x1, y1);
00148             x1= 0;
00149             count = count + 1;
00150             enic2.x = 0;
00151             }
00152             if (explosionjudge(enic1.x,enic1.y,x2,y2)) {
00153             explosion(x1, y1);
00154             x2 = 0;
00155             count = count + 1;
00156             enic1.x = 0;
00157             } 
00158         if (explosionjudge(enic2.x,enic2.y,x2,y2)) {
00159             explosion(x2, y2);
00160             count = count + 1;
00161             x2 = 0;
00162             enic2.x = 0;
00163             }
00164             
00165             
00166             score = count *50;
00167             
00168                     
00169         if (xp1 <= 4) {
00170             xp1 = enic1.x;
00171             yp1 = enic1.y;
00172             }else if (xp1 > 0) {
00173                 xp1 = eni.open_fire_en(xp1, yp1,lcd);
00174                 }
00175         if (xp2 <= 4){
00176             xp2 = enic2.x;
00177             yp2 = enic2.y;
00178             }else if (xp2 > 0) {
00179              xp2 = eni.open_fire_en(xp2, yp2,lcd);
00180             }
00181             
00182             if (score > high_score){
00183             high_score = score;
00184             }
00185                   
00186         if (explosionjudge(x,y,xp1,yp1)) {
00187             explosion(xp1, yp1);
00188             xp1 = 0;
00189             game_over();
00190             }
00191           if (explosionjudge(x,y,xp2,yp2)) {
00192             explosion(xp2, yp2);
00193             xp2 = 0;
00194             game_over();
00195             }
00196             
00197         
00198     
00199         
00200         wait(0.1);
00201        lcd.refresh();
00202     }
00203 }
00204 
00205 
00206 
00207 
00208 
00209 
00210 void init()
00211 {
00212     // need to initialise LCD and Gamepad 
00213     pad.init();
00214     
00215     count = 0;
00216     score = 0;
00217     x = 5.0;
00218     y = 21.0;
00219     x1 = 0.0;
00220     y1 = 0.0;
00221     x2 = 0.0;
00222     y2 = 0.0;
00223     v = 4.0;
00224     p1 = p2 = 0;
00225     xp1 = xp2 = xn1 =xn2 =0.0;
00226     yp1 = yp2 = yn1 = yn2 =0.0;
00227     enic1.x = enic2.x = 0.0;
00228      
00229     // initialise the game with correct ball and paddle sizes
00230 
00231 }
00232 
00233 
00234 
00235 void welcome() {
00236     
00237     lcd.printString("   HALO WAR    ",0,1);  
00238     lcd.printString("  Press Start ",0,4);
00239     lcd.refresh();
00240      
00241     // wait flashing LEDs until start button is pressed 
00242     while ( pad.check_event(Gamepad::START_PRESSED) == false) {
00243         pad.leds_on();
00244         wait(0.1);
00245         pad.leds_off();
00246         wait(0.1);
00247         
00248     }
00249 }
00250 
00251 bool explosionjudge(float x1,float y1,float x2,float y2){
00252         float d;
00253         d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
00254         if (d <= 9){     
00255           return true;
00256         }else {
00257             return false;
00258             }
00259         }
00260         
00261 void explosion(int x, int y){
00262     
00263     lcd.drawSprite(x, y, 10, 10, (int *)explosion123);
00264     }
00265     
00266     
00267 void game_over(){
00268     while(!pad.check_event(Gamepad::START_PRESSED)){
00269         char buffer1[20];
00270         sprintf(buffer1, "High Score=%d", high_score);
00271         lcd.printString(buffer1,0,0);
00272         char buffer[14];
00273         sprintf(buffer, "  Score = %d ", score);
00274         lcd.printString(buffer,0,1);
00275     lcd.printString("   Wasted    ",0,2);  
00276     lcd.printString("Press start to",0,3);
00277     lcd.printString(" new compaign",0,4);
00278     lcd.refresh();
00279     lcd.clear();
00280     }
00281     init();
00282     }