Wang Lin 201090174

Dependencies:   mbed Gamepad N5110 FXOS8700Q

main.cpp

Committer:
a1115921303
Date:
2019-05-06
Revision:
13:febf9fbb502f
Parent:
12:121ba031343a

File content as of revision 13:febf9fbb502f:

#include "main.h"
#define SNAKE_LENGTH 100
#define SNAKE_SIZE 2
void init();
void welcome();
void render();
void initsnake();
void speed();
int  dif = 1;

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

Name:Wang Lin
Username:wang lin
Student ID Number:201090174
Date:May 2019
*/
 
int main()
{
   // the main loop of the whole project
    while(1){
    init();     // initialise and then display welcome screen...
    welcome();  // waiting for the user to start
    speed(); // the page of choose difficulty
    initsnake();//intilize the variables
    defineSnake();//define the body of snake 
    SpwanFood();//generate the firstfood
    
   // the loop of game, if snake is dead jump to the gameover page
  while(live == 1 ) {
   lcd.clear();
    render();
    DrawSnake();
    drawFood();
    MoveSnake();
    EatFood();
    Break();
    lcd.refresh();
    wait(waittime/2);
    }
     //game over page , if peass back, jump this loop and restart
     while(pad.check_event(Gamepad::BACK_PRESSED) == false){
        DeadSnake();
        lcd.refresh();
        }
    
   }
}
void init()
{
    // need to initialise LCD and Gamepad 
    lcd.init();
    pad.init();
     
}
 
void welcome() {
        
    lcd.clear();    
    lcd.printString("WELCOME!",19,0);
    lcd.printString(" SNAKE!  ",20,1);  
    lcd.printString(" Press A  ",16,2);
    lcd.printString("to choose ",18,3);
    lcd.printString("difficulty",17,4);
    lcd.refresh();
     
    // wait flashing LEDs until A button is pressed 
    while ( pad.check_event(Gamepad::A_PRESSED) == false) {
       lcd.refresh();
    }
}
 
void render() {
   
    lcd.drawRect(0,0,83,47,FILL_TRANSPARENT); //daaw the map

}
//initialise the varaiables of snake
void initsnake() {
    SNAKE.snakezb[0].x = 30;
    SNAKE.snakezb[0].y = 20;
    SNAKE.n = 3;  //inital length of snake
    Dir = 3;
    live = 1;
    Score = 0;
   
    
}
// the difficulty choose module throuth joystick or direction button
void speed(){
    while(pad.check_event(Gamepad::START_PRESSED) == false){
    lcd.clear();
    lcd.printString("CHOOSE",25,0);
    lcd.printString("difficulty",15,1);
    lcd.printString("Press START",13,4);
    lcd.printString("to Start",18,5);//user interface guide to choose difficulty
    if(dif == 1){
        waittime = 1;
        }
    else if (dif == 2){
        waittime = 0.3;
        }
    else if (dif == 3){
        waittime = 0.2;
        }
    else if (dif == 4){
        waittime = 0.1;
        }
    else if (dif == 5){
        waittime = 0.05;
        }
    if(dif > 5){
        dif = 5;
        }
    else if(dif < 1){
        dif = 1;
        }
    d = pad.get_direction();
    //if difficulty is larger or less than the limitation , make back to the limitation 
    if(pad.check_event(Gamepad::Y_PRESSED) || d == N){
        dif = dif + 1;
        }
     if(pad.check_event(Gamepad::A_PRESSED) || d == S){
        dif = dif - 1;
        }
    char buffer[1];
    sprintf(buffer,"%d",dif);
    lcd.printString(buffer,40,2);
    lcd.refresh();
        }
    
    }