Li Ruofan 201199450

Dependencies:   mbed

main.cpp

Committer:
DannyLee
Date:
2020-05-23
Revision:
0:80a59a49d504
Child:
1:bd7c99a5bd10

File content as of revision 0:80a59a49d504:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Ruofan Li
Username: el17rl
Student ID Number: 201199450
Date: May, 2020
*/

#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
#include "Snake.h"

struct UserInput {
    Direction d;
};

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);//set LCD pins
Gamepad pad;
Snake snake;

int bgm_();
int sound_();
void begin();
void init(Gamepad &pad);
void game_tips();

int main()
{
    init(pad);
    // game loop
    while (1) {
            snake.draw(lcd);
            snake.update(pad);
    }
}

void init(Gamepad &pad)
{
    // initialise LCD and Gamepad
    lcd.init();
    pad.init();
    begin();
    snake.init(25,25,6,1);
    // initialise parameters of the project
    // display tips
    game_tips();
}

void begin(){
    lcd.printString(" Greedy Snake  ",0,1);
    lcd.printString("  Li Ruofan  ",0,2);  
    lcd.printString("  201199450  ",0,3);
    lcd.printString("    Quickly  ",0,4);
    lcd.printString(" Push X twice ",0,5); 
    lcd.refresh();

 while (pad.check_event(Gamepad::X_PRESSED) == false){
        pad.leds_on();
        sound_();    
        wait(0.3);   
        pad.leds_off();
        wait(0.3);
    }  
    
    lcd.clear();
    
}

void game_tips(){
    lcd.clear(); 
    lcd.printString(" Greedy Snake! ",0,0);
    lcd.printString(" Move the snake ",0,1);  
    lcd.printString(" To eat apples ",0,2);  
    lcd.printString(" Far from wall ",0,3);
    lcd.printString(" Press start! ",0,5);
    lcd.refresh();
    
while (pad.check_event(Gamepad::START_PRESSED) == false) {
        pad.leds_on();
        wait(0.2);
        pad.leds_off();
        wait(0.2);
}
}

int sound_(){
    pad.tone(784.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(880.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(587.0,0.6);
    wait(0.5);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(523.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(523.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(440.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(587.0,0.6);
    wait(0.4);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0,0.4);
    wait(0.4);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0,0.4);
    wait(0.4);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(880.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(1046.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(880.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(523.0,0.4);
    wait(0.4);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(523.0,0.4);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    
    pad.tone(440.0,0.3);
    wait(0.3);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(587.0,0.6);
    wait(0.6);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        } 
        return 0;
    }//play the Chinese song "East is Red"