RUOFAN LI / Mbed 2 deprecated el17rl

Dependencies:   mbed

main.cpp

Committer:
DannyLee
Date:
2020-05-15
Revision:
4:1ebf8b8842e0
Parent:
3:cf9fead9c3f4
Child:
5:e3a9f0548922

File content as of revision 4:1ebf8b8842e0:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name:Li Ruofan
Username:el17rl
Student ID Number:201199450
Date:12 May 2020
*/
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
#include "shot.h"
#include "Ufo.h"
#include "Spaceship.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;
};

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Spaceship Spaceship;

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;

Ufo *Ufo = NULL;
shot *shot[5] = {NULL,NULL,NULL,NULL,NULL};
Spaceship *Spaceship = NULL;
Homepage *Homepage = NULL;

int score[3] = {0,0,0};
int curScore = 0;
int fail = 0;
int count = 0;
int mode = 0;

///////////// prototypes ///////////////
void attack();
void move(int signal);
void shoot();
void welcome();
void over();
void updateScore();
void over();
void init();
void updateGame();

///////////// functions ////////////////
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){
            } 
            lcd.clear();

        }         
}
//this function is used to update a game, such as 
//display the current score, draw Spaceship, shot ect.  
void updateGame(){
    Homepage->displayCurScore(lcd,curScore);
    Direction d = joystick.get_direction();
    Spaceship->update(d);
    Spaceship->draw(lcd);
    shoot();
    attack();
    lcd.clear();
}

// initialies buttons, lcd and joystick
void init(){
    buttonX.mode(PullDown);
    start.mode(PullDown);
    buttonA.mode(PullDown);
    buttonY.mode(PullDown);
    buttonB.mode(PullDown);
    joystick.init();       
    lcd.init();
    Homepage = new Homepage();
}

//do something about 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;
    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
//initialize the shot
//shot moves upwards until the Ufo's blood ran out.
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);
                break;
            }
        }
    } 
    for(int i = 0;i<5;i++){
        if(shot[i]!=NULL){
            shot[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
                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
                }             
            }else if(shot[i]->getPos().y<=0){
                shot[i] = NULL;
            }else {
                shot[i]->update();
            }
        }
    }
}

//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);
        lcd.refresh();
    } else {
        Ufo->update();
        if(Ufo->getPos().y>=33){
            fail = 1;
        }else{
            Ufo->draw(lcd,mode);
            lcd.refresh();
        }
    }
}