RUOFAN LI / Mbed 2 deprecated el17rl

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 ELEC2645 Embedded Systems Project
00003 School of Electronic & Electrical Engineering
00004 University of Leeds
00005 Coded by Li Ruofan
00006 Username:el17rl
00007 StudentIDNumber:201199450
00008 Date:5/10/2020
00009 */
00010 #include <N5110.h>
00011 #include <Joystick.h>
00012 #include "shot.h"
00013 #include "UFO.h"
00014 #include "spaceship.h"
00015 #include "homepage.h"
00016 #include "bgm.h"
00017 
00018 /////////////// objects ///////////////
00019 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00020 Joystick joystick(PTB10,PTB11,PTC16);
00021 InterruptIn start(PTC5);
00022 InterruptIn buttonX(PTC17);
00023 InterruptIn buttonY(PTC12);
00024 InterruptIn buttonA(PTB9);
00025 InterruptIn buttonB(PTD0);
00026 BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3); //leds
00027 Bgm bgm;
00028 
00029 UFO *UFO = NULL;
00030 Shot *shots[5] = {NULL,NULL,NULL,NULL,NULL};
00031 Spaceship *spaceship = NULL;
00032 
00033 int score[3] = {0,0,0};
00034 int curScore = 0;
00035 int fail = 0;
00036 int count = 0;
00037 
00038 ///////////// prototypes ///////////////
00039 void attack();
00040 void move(int signal);
00041 void shoot();
00042 void welcome();
00043 void over();
00044 void updateScore();
00045 void over();
00046 void init();
00047 void updateGame();
00048 
00049 ///////////// functions ////////////////
00050 int main(){
00051         
00052         init();
00053         while (1) {
00054         //welcome to the star war
00055         homepage->welcome(lcd,output,bgm);
00056         //choose different function
00057         homepage->homepage(lcd,buttonA,start,score,3);          
00058         spaceship = new Spaceship();
00059         spaceship->init(37,38,10,10);   
00060         while (start == 0){
00061             } 
00062             lcd.clear();
00063 
00064         }         
00065 }
00066      
00067 //updating the game 
00068 void updateGame(){
00069     homepage->displayCurScore(lcd,curScore);
00070     Direction d = joystick.get_direction();
00071     spaceship->update(d);
00072     spaceship->draw(lcd);
00073     shoot();
00074     attack();
00075     lcd.clear();
00076 }
00077 
00078 // initialies buttons, lcd and joystick
00079 void init(){
00080     buttonX.mode(PullDown);
00081     start.mode(PullDown);
00082     buttonA.mode(PullDown);
00083     buttonY.mode(PullDown);
00084     buttonB.mode(PullDown);
00085     joystick.init();       
00086     lcd.init();
00087     homepage = new Homepage();
00088 }
00089 
00090 //game over,
00091 //clear data, bgm, etc.
00092 void over(){
00093     bgm->died(bgm);
00094     homepage->over(lcd,output); 
00095     UFO = NULL;
00096     for(int i = 0;i<5;i++)shots[i] = NULL;
00097     fail = 0;
00098     count = 0;
00099     updateScore();
00100     curScore = 0;
00101 }
00102 
00103 //show the movement of the shot
00104 //initialize the shot
00105 //shot moves upwards until the UFO has been slained.
00106 void shoot() {
00107     if (buttonX == 1) {
00108         for(int i = 0;i<5;i++){
00109             if(shots[i] == NULL){
00110                 shots[i] = new shot();
00111                 shots[i]->init(spaceship->getPos().x+3,35,5,3);
00112                 break;
00113             }
00114         }
00115     } 
00116     for(int i = 0;i<5;i++){
00117         if(shots[i]!=NULL){
00118             shots[i]->draw(lcd);
00119             lcd.refresh();
00120             if(shots[i]->getPos().x>=UFO->getPos().x && shots[i]->getPos().x<=(UFO->getPos().x+12) && shots[i]->getPos().y<=(UFO->getPos().y+5)){ //being shot
00121                 bgm.tone(750.0,0.1);
00122                 shots[i] = NULL;
00123                 UFO->setBlood(1);
00124                 if(UFO->getBlood() <= 0){
00125                     UFO = NULL; //destroy 
00126                     curScore+=1;   //get score
00127                 }             
00128             }else if(shots[i]->getPos().y<=0){
00129                 shots[i] = NULL;
00130             }else {
00131                 shots[i]->update();
00132             }
00133         }
00134     }
00135 }
00136 
00137 //display the movement of the UFO
00138 //initialize the UFO
00139 //UFO moves downwards until meets the spaceship
00140 void attack() { 
00141     if(UFO == NULL){
00142         UFO = new UFO;
00143         UFO->init(12,5);
00144         UFO->draw(lcd);
00145         lcd.refresh();
00146     } else {
00147         UFO->update();
00148         if(UFO->getPos().y>=33){
00149             fail = 1;
00150         }else{
00151             UFO->draw(lcd);
00152             lcd.refresh();
00153         }
00154     }
00155 }