Li Boyuan / Mbed 2 deprecated PlaneWar

Dependencies:   mbed Gamepad N5110

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "Bitmap.h"
00002 #include "N5110.h"
00003 #include "Gamepad.h"
00004 
00005 #include "plane.h"
00006 #include "life.h"
00007 #include "engine.h"
00008 #include "N5110.h"
00009 #include "Gamepad.h"
00010 #include<iostream>
00011 
00012 using namespace std;
00013 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00014 Gamepad pad;
00015 plane p;
00016 life lf;
00017 engine eg;
00018 bool n = true;
00019 bomb bo[20];
00020 bullet bu[20];
00021 int bunum = 0;
00022 int bonum = 0;
00023 int score = 0;
00024 int m = 0;
00025 
00026 void init();
00027 void welcome();
00028 void screen();
00029 void update();
00030 void gameover();
00031 void drawBitmap(int x, int y, int width, int height, int *data);
00032 
00033 int main()
00034 {
00035     while(1){
00036         init();
00037         welcome();
00038         while(n){
00039             if(m%8 ==0){
00040                 bo[bonum%20].y = eg.randombomb();
00041                 bo[bonum%20].x = 82;
00042                 bonum = bonum + 1;
00043             }
00044             lcd.clear();
00045             screen();
00046             update();
00047             lcd.refresh();
00048             wait(0.2);
00049             m = m + 1;
00050         }
00051         wait(1);
00052     }
00053 }
00054 
00055 void init(){
00056     lcd.init();
00057     pad.init();
00058     p.init();
00059     lf.init();
00060     eg.init();
00061     lcd.setContrast(0.4);
00062     n = true;
00063     bunum = 0;
00064     bonum = 0;
00065     score = 0;
00066     m = 0;
00067     for(int i = 0; i < 20; i++){
00068         bu[i].x = -1;
00069         bu[i].y = -1;
00070         bo[i].x = -1;
00071         bo[i].y = -1;
00072         bo[i].width = 8;
00073         bo[i].height = 8;
00074         int data[64]= {
00075                 0,0,0,1,1,0,0,0,
00076                 0,1,1,0,0,1,1,0,
00077                 0,1,1,1,1,1,1,0,
00078                 1,0,1,1,1,1,0,1,
00079                 1,0,1,1,1,1,0,1,
00080                 0,1,1,1,1,1,1,0,
00081                 0,1,1,0,0,1,1,0,
00082                 0,0,0,1,1,0,0,0                 
00083         };
00084         bo[i].data = data;
00085     }
00086 }
00087 
00088 void welcome(){
00089     lcd.clear();
00090     lcd.printString("The plane war!",0,1);  
00091     lcd.printString("  Press Start ",0,4);
00092     lcd.refresh();
00093     while( pad.check_event(Gamepad::START_PRESSED) == false){
00094         pad.leds_on();
00095         wait(0.1);
00096         pad.leds_off();
00097         wait(0.1);
00098     }
00099 }
00100 
00101 void screen(){
00102     lcd.clear();
00103     p.display(lcd);
00104     lf.display(lcd);
00105     for(int i = 0; i <20; i++){
00106         if(bo[i].x >=0){
00107             drawBitmap(bo[i].x, bo[i].y, bo[i].width, bo[i].height, bo[i].data);}
00108         if(bu[i].x >=0){
00109             lcd.drawRect(bu[i].x, bu[i].y, 2, 2, FILL_BLACK);}
00110     }
00111     lcd.refresh();
00112 }
00113 
00114 void update(){
00115     p.update(pad);
00116     for(int i = 0; i<20; i++){
00117         for(int j = 0; j < 20; j++){
00118             events e = eg.evet_check(p, bo[i],pad,bu[j]);
00119             if(e == DEAD){
00120                 lf.update();
00121                 bo[i].x = -1;
00122                 pad.tone(1000,0.2);
00123                 if(lf.liferest()==0){
00124                     gameover();
00125                     i = 20;
00126                     j = 20;
00127                     pad.tone(1000,0.5);
00128                     break;
00129                 }
00130             }else if(e == FIRE){
00131                 eg.fire(p.getxy().x+7, p.getxy().y+3, bu[bunum%20]);
00132                 bunum = bunum + 1;
00133             }else if(e == ELMININATE){
00134                 bo[i].x = -1;
00135                 bu[j].x = -1;
00136                 score = score + 5;
00137                 cout <<score<<endl;
00138             }
00139         }
00140     }
00141     for(int i = 0; i<20; i++){
00142         if((bo[i].x >=0) && (bo[i].x<=84)){
00143             bo[i].x = bo[i].x - 1;
00144         }
00145         if((bu[i].x >=0) && (bu[i].x<=84)){
00146             bu[i].x = bu[i].x + 1;
00147         }
00148     }
00149     
00150 }
00151 
00152 void gameover(){
00153     cout <<"game over"<< endl;
00154     lcd.clear();
00155     lcd.printString("GAMEOVER    ",0,1);  
00156     lcd.printString("SCORE    ",0,2);  
00157     char buffer1[14];
00158     sprintf(buffer1,"%2d",score);
00159     lcd.printString(buffer1,0,3);
00160     lcd.printString("  Press A ",0,4);
00161     lcd.refresh();
00162     while ( pad.check_event(Gamepad::A_PRESSED) == false) {
00163         pad.leds_on();
00164         wait(0.1);
00165         pad.leds_off();
00166         wait(0.1);
00167     }
00168     n = false;
00169 }
00170 
00171 void drawBitmap(int x, int y, int width, int height, int *data){
00172     Bitmap p(data, height, width);
00173     p.render(lcd, x, y);
00174 }