Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

Revision:
5:e3a9f0548922
Parent:
4:1ebf8b8842e0
Child:
6:cbd9e1f26a10
--- a/main.cpp	Fri May 15 01:25:16 2020 +0000
+++ b/main.cpp	Fri May 15 19:57:40 2020 +0000
@@ -2,46 +2,33 @@
 ELEC2645 Embedded Systems Project
 School of Electronic & Electrical Engineering
 University of Leeds
-Name:Li Ruofan
+Coded by Li Ruofan
 Username:el17rl
-Student ID Number:201199450
-Date:12 May 2020
+StudentIDNumber:201199450
+Date:5/10/2020
 */
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
+#include <N5110.h>
+#include <Joystick.h>
 #include "shot.h"
-#include "Ufo.h"
+#include "UFO.h"
 #include "Spaceship.h"
-#include "bgm.h"
+#include "Menu.h"
+#include "Bgm.h"
 
-#define SPACESHIP_WIDTH 5
-#define SPACESHIP_HEIGHT 6
-#define UFO_SIZE 4
-#define UFO_SPEED 1
-#define SPACESHIP_LIFE 2
-
-struct UserInput {
-    Direction d;
-    float mag;
-};
-
+/////////////// objects ///////////////
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
-Gamepad pad;
-Spaceship Spaceship;
-
+Joystick joystick(PTB10,PTB11,PTC16);
 InterruptIn start(PTC5);
 InterruptIn buttonX(PTC17);
 InterruptIn buttonY(PTC12);
 InterruptIn buttonA(PTB9);
 InterruptIn buttonB(PTD0);
 BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3); //leds
-bgm bgm;
+Bgm bgm;
 
-Ufo *Ufo = NULL;
-shot *shot[5] = {NULL,NULL,NULL,NULL,NULL};
-Spaceship *Spaceship = NULL;
-Homepage *Homepage = NULL;
+UFO *UFO = NULL;
+Shot *shots[5] = {NULL,NULL,NULL,NULL,NULL};
+Spaceship *spaceship = NULL;
 
 int score[3] = {0,0,0};
 int curScore = 0;
@@ -64,52 +51,26 @@
 int main(){
         
         init();
-        
         while (1) {
-           //welcome to the star war
-           Homepage->welcome(lcd,output,bgm);
-           //choose different function
-           Homepage->Homepage(lcd,buttonA,buttonY,buttonX,buttonB,start,score,3);          
-           Spaceship = new Spaceship();
-           Spaceship->init(37,38,10,10);
-           //choose the mode: easy, normal, defficult
-           mode = Homepage->getMode();
-            while(1){  
-                 //playing...
-                  updateGame();
-                  //failing, check weather playing again
-                  if (fail == 1) {
-                    int tmp = Homepage->again(lcd,curScore,buttonX,buttonY);
-                    if(tmp == 0)
-                        //do not play again
-                        break;
-                    else{
-                            //play again
-                            curScore+=tmp;
-                            fail = 0;
-                            lcd.clear();
-                            Ufo = NULL;
-                        }
-                  }
-                  count++;
-                  //change Ufo's speed
-                  if(count %8 == 0)Ufo->setSpeed(Ufo->getSpeed()+1);
-                  wait(1.0f/3);                   
-            } 
-            over();           
-            while (start == 0){
+        //welcome to the star war
+        homepage->welcome(lcd,output,bgm);
+        //choose different function
+        homepage->homepage(lcd,buttonA,start,score,3);          
+        spaceship = new Spaceship();
+        spaceship->init(37,38,10,10);   
+        while (start == 0){
             } 
             lcd.clear();
 
         }         
 }
-//this function is used to update a game, such as 
-//display the current score, draw Spaceship, shot ect.  
+     
+//updating the game 
 void updateGame(){
-    Homepage->displayCurScore(lcd,curScore);
+    homepage->displayCurScore(lcd,curScore);
     Direction d = joystick.get_direction();
-    Spaceship->update(d);
-    Spaceship->draw(lcd);
+    spaceship->update(d);
+    spaceship->draw(lcd);
     shoot();
     attack();
     lcd.clear();
@@ -124,85 +85,71 @@
     buttonB.mode(PullDown);
     joystick.init();       
     lcd.init();
-    Homepage = new Homepage();
+    homepage = new Homepage();
 }
 
-//do something about game over,
-//clear data, Bgm, etc.
+//game over,
+//clear data, bgm, etc.
 void over(){
-    bgm.tone(1500.0,0.5);
-    Homepage->over(lcd,output); 
-    Ufo = NULL;
-    for(int i = 0;i<5;i++)shot[i] = NULL;
+    bgm->died(bgm);
+    homepage->over(lcd,output); 
+    UFO = NULL;
+    for(int i = 0;i<5;i++)shots[i] = NULL;
     fail = 0;
     count = 0;
     updateScore();
     curScore = 0;
 }
 
-//update top 3 history scores when game over
-void updateScore(){
-    for(int i = 0;i<3;i++){
-        if(score[i]<curScore){
-            //score[i] = curScore;
-            for(int j = 2;j>i;j--){
-                score[j] = score[j-1];
-            }
-            score[i] = curScore;
-            break;
-        }
-    }
-}
-
-//display the movement of the shot
+//show the movement of the shot
 //initialize the shot
-//shot moves upwards until the Ufo's blood ran out.
+//shot moves upwards until the UFO has been slained.
 void shoot() {
     if (buttonX == 1) {
         for(int i = 0;i<5;i++){
-            if(shot[i] == NULL){
-                shot[i] = new shot();
-                shot[i]->init(Spaceship->getPos().x+3,35,5,3);
+            if(shots[i] == NULL){
+                shots[i] = new shot();
+                shots[i]->init(spaceship->getPos().x+3,35,5,3);
                 break;
             }
         }
     } 
     for(int i = 0;i<5;i++){
-        if(shot[i]!=NULL){
-            shot[i]->draw(lcd);
+        if(shots[i]!=NULL){
+            shots[i]->draw(lcd);
             lcd.refresh();
-            if(shot[i]->getPos().x>=Ufo->getPos().x && shot[i]->getPos().x<=(Ufo->getPos().x+12) && shot[i]->getPos().y<=(Ufo->getPos().y+5)){ //being shot
+            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
                 bgm.tone(750.0,0.1);
-                shot[i] = NULL;
-                Ufo->setBlood(1);
-                if(Ufo->getBlood() <= 0){
-                    Ufo = NULL; //destroy 
-                    curScore+=((mode+1)*2);   //get points according to the mode
+                shots[i] = NULL;
+                UFO->setBlood(1);
+                if(UFO->getBlood() <= 0){
+                    UFO = NULL; //destroy 
+                    curScore+=1;   //get score
                 }             
-            }else if(shot[i]->getPos().y<=0){
-                shot[i] = NULL;
+            }else if(shots[i]->getPos().y<=0){
+                shots[i] = NULL;
             }else {
-                shot[i]->update();
+                shots[i]->update();
             }
         }
     }
 }
 
-//display the movement of the Ufo
-//initialize the Ufo
-//Ufo moves downwards until meets the Spaceship
+//display the movement of the UFO
+//initialize the UFO
+//UFO moves downwards until meets the spaceship
 void attack() { 
-    if(Ufo == NULL){
-        Ufo = new Ufo(mode);
-        Ufo->init(12,5);
-        Ufo->draw(lcd,mode);
+    if(UFO == NULL){
+        UFO = new UFO(mode);
+        UFO->init(12,5);
+        UFO->draw(lcd,mode);
         lcd.refresh();
     } else {
-        Ufo->update();
-        if(Ufo->getPos().y>=33){
+        UFO->update();
+        if(UFO->getPos().y>=33){
             fail = 1;
         }else{
-            Ufo->draw(lcd,mode);
+            UFO->draw(lcd,mode);
             lcd.refresh();
         }
     }