Yunting Zou 201199716

Dependencies:   mbed MotionSensor

main.cpp

Committer:
zhouyun123
Date:
2020-05-14
Revision:
3:4bd22344278d
Parent:
2:a2f88b2d5da4

File content as of revision 3:4bd22344278d:

/*
ELEC 2645 Embedded System Project
School of Electronic & Electrical Engineering
University of Leeds

Name:Zou yunting
Username:el17y2z
Student ID Number:201199716
Date:15th May 2020
*/


#include "viper.h"
#include "story.h"

void init();          // initialise and show the welcome screen
void begin();         // the welcome screen
void render();        // Draw the ouline of the area
void showviper();     //Draw the snake
void speed();         //select the speed of the snake's movment
void showtarget();      //draw the target food
void Dead();          //set the failure condition
void music();         //set the starting music
void lose();          //set the losing music
int sp = 0 ;          //variable of speed level


 
int main()
{
   // the main function of the whole project
    while(1){
    init();     // initialise and show the welcome screen
    begin();
    music();  // show the starting screen
    firststory();
    graph();
    speed(); // set the speed of snake's movement
    showviper();//@intilize the snake's location
    viper();//set the parameter of snake 
    goal();//generate the first target food
    
   //@ the loop of game, if snake is dead jump to the gameover page
  while(HP == 1 ) {
    lcd.clear();
    render();
    viper();
    showtarget();
    Movement();
    gettarget();
    Dead();
    lcd.refresh();
    wait(wtime/2);
    }
     //turn to the game over page , press the back button to restart.
     while(pad.check_event(Gamepad::BACK_PRESSED) == false){
        gameover();
        lcd.refresh();
        } 
   }
}


void init()                                                        //1
{
    //  initialise LCD and Gamepad 
    lcd.init();
    pad.init();
     
}


 //the welcome screen
void begin() {                                                    //2
        
    lcd.clear();   
    lcd.printString("Gluttonous!",0,0);
    lcd.printString("snake!!!!",0,1);
    lcd.printString(" Zou Yunting",0,2);  
    lcd.printString(" ID:201199716 ",0,3);
    lcd.printString(" welcome! ",0,4);
    lcd.printString("press A button",0,5);
    lcd.refresh();
     
    // Press the A button to turn to the next screen 
    while ( pad.check_event(Gamepad::A_PRESSED) == false) {
       lcd.refresh();
    }
}


//Outline the entire interface
void render() {                                                        //3
   
    lcd.drawRect(0,0,80,40,FILL_TRANSPARENT);

}


//initialise the parameters of snake
void showviper() {                                                    //4
    SER.head[0].x = 20;
    SER.head[0].y = 30;
    SER.n = 5;  //inital length of snake
    HP = 1;
    score = 0;
}


// select the speed of snale's movement by the A and B buttoms
void speed(){                                                              //5
    while(pad.check_event(Gamepad::START_PRESSED) == false){
    lcd.clear();
    lcd.printString("CHOOSE",25,0);
    lcd.printString("difficulty",15,1);
    lcd.printString("press A or B",0,2);
    lcd.printString("Press START",13,4);
    lcd.printString("to Start",18,5);
    if(sp == 1){
        wtime = 0.3;
        }
    else if (sp == 2){
        wtime = 0.5;
        }
    //@press A or B button to select the speed
    if(pad.check_event(Gamepad::A_PRESSED) ){
        sp =  1;
        }
    else if(pad.check_event(Gamepad::B_PRESSED) ){
        sp =  2;
        }
    char buffer[1];
    sprintf(buffer,"%d",sp);
    lcd.printString(buffer,40,3);
    lcd.refresh();
        }
    }
    
    
    //print the food                           5             
void showtarget(){                                                              //6
    lcd.drawRect(target.var.x,target.var.y,1,1,FILL_TRANSPARENT);
    }
    
       
    //if the viper rush out of the border of the screen, game over.             7
void Dead(){
    //judge if snake rush out of the border
    if(SER.head[0].x<=0 || SER.head[0].x >= 80 || SER.head[0].y <= 0  || SER.head[0].y >= 40)
    {
        HP = 0;
        }
    }
    
    
    //if snake dead,print gameover screen and prepare to resart                8  
void gameover(){
    lcd.clear();
    lcd.printString("failure!",0,1 );
    lcd.printString("last score:",0,2);
    lcd.printString("Press Back",1,4);
    lcd.printString("to restart",1,5);
    char buffer[1];
    sprintf(buffer,"%d",score);
    lcd.printString(buffer,65,2);
    lcd.refresh();
    lose();                                            
    }