Li Ruofan 201199450

Dependencies:   mbed

main.cpp

Committer:
DannyLee
Date:
2020-05-24
Revision:
1:bd7c99a5bd10
Parent:
0:80a59a49d504

File content as of revision 1:bd7c99a5bd10:

/*
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 sound_();
void begin();
void init(Gamepad &pad);
void game_rules();

int main()
{
    init(pad);
    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 rules
    game_rules();
}

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

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;
        } 
    pad.tone(659.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(659.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(587.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(523.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(523.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(659.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(1046.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(1046.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(1046.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(880.0f,0.5f);
    wait(0.5f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(880.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(784.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(659.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(523.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(587.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(587.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(659.0f,0.3f);
    wait(0.3f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }
    pad.tone(587.0f,0.2f);
    wait(0.2f);
    if(pad.check_event(Gamepad::X_PRESSED) == true){
        return 0;
        }   
        return 0;
    }//play Chinese song "East is Red" and Australian song "Sheep clippling"