ELEC2645 (2018/19) / Mbed 2 deprecated 2645_Project_SiutingWong201186503

Dependencies:   mbed

main.cpp

Committer:
davidwst421
Date:
2019-05-09
Revision:
7:193c0fd7afdd
Parent:
6:a0f3dbbc8d33
Child:
8:97576c8761a8

File content as of revision 7:193c0fd7afdd:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
Name: Siuting Wong
Username: Siuting Wong
Student ID Number: 201186503
Date: 06/05/2019
*/
///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Engine.h"

#define WALL_WIDTH 2
#define WALL_GAP 10
#define AVENGER_SIZE 2
#define STONE_SIZE 2
#define SPEED 2

/////////////// structs /////////////////
struct UserInput {
    Direction d;
    float mag;
    Vector2D mapped_coord;
};
/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Engine eng;

///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
void welcome();
void endgame();
void introduction();

const int Avenger0[25][32] = {
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,1,1,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0 },
    { 0,0,0,1,0,0,0,0,0,1,1,1,0,0,0,1,1,0,1,1,1,0,1,1,0,0,0,0,1,0,0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,0,0,0,1,1,0,1,1,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
};
///////////// functions ////////////////
int main() {
    int fps = 8;
    
    init();     // initialise and then display welcome screen...
    welcome();
    introduction();
    render();
    wait(1.0f/fps);  // and wait for one frame period
    
    while (eng.get_score() >= 0) {
        eng.read_input(pad);
        eng.update(pad);
        render();
        wait(1.0f/fps);
    }
    endgame();
}

void init() {
    
    // need to initialise LCD and Gamepad 
    lcd.init();
    pad.init();
     
    // initialise the game with correct ball and paddle sizes
    eng.init(WALL_WIDTH,WALL_GAP,STONE_SIZE,SPEED);
}

void welcome() {
    
    while (pad.check_event(Gamepad::START_PRESSED) == true) {
        lcd.clear();
        lcd.printString("Eternity Stone",0,0);
        lcd.printString("Start: game",6,4);
        lcd.printString("Back: story",6,5);
        lcd.drawSprite(25,8,25,32,(int *)Avenger0);
        lcd.refresh();
        wait(0.5);
        lcd.clear();
        lcd.printString("Eternity Stone",0,0);
        lcd.printString("Start: game",6,4);
        lcd.printString("Back: story",6,5);
        lcd.drawSprite(25,8,25,32,(int *)Avenger1);
        lcd.refresh();
        wait(0.5);
    }
}

void introduction() {
    lcd.clear();
    lcd.printString("Five year ",12,0);
    lcd.refresh();
    wait(1.0);
    lcd.printString("Since Thanos",6,2);
    lcd.printString("destroyed half",6,3);
    lcd.printString("of all life",9,4);
    lcd.printString("on Earth",18,5);
    lcd.refresh();
    wait(3.0);
    lcd.printString("(Press start)",6,1);
    wait(1.0);
    while (pad.check_event(Gamepad::START_PRESSED) == false) {
        lcd.refresh();
        wait(0.5);
    }
    lcd.clear();
    lcd.printString("You are a",12,0);
    lcd.printString("member of",15,1);
    lcd.printString("Avengers and",6,2);
    lcd.printString("you need to",9,3);
    lcd.printString("time travel in",0,4);
    lcd.printString("Quantum realm",3,5);
    lcd.refresh();
    wait(3.0);
    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
        lcd.refresh();
        wait(0.5);
    }
    while (pad.check_event(Gamepad::START_PRESSED) == false) {
        lcd.clear();
        lcd.printString("Use your",12,0);
        lcd.printString("joystick to",15,1);
        lcd.printString("send yourself",3,2);
        lcd.printString("to the right",6,3);
        lcd.printString("Wormhole",6,4);
        lcd.drawSprite(0,40,8,84,(int *)intro0);
        lcd.refresh();
        wait(0.5);
        lcd.clear();
        lcd.printString("Use your",12,0);
        lcd.printString("joystick to",15,1);
        lcd.printString("send yourself",3,2);
        lcd.printString("to the right",6,3);
        lcd.printString("Wormhole",6,4);
        lcd.drawSprite(0,40,8,84,(int *)intro1);
        lcd.refresh();
        wait(0.5);
        lcd.clear();
        lcd.printString("Use your",12,0);
        lcd.printString("joystick to",15,1);
        lcd.printString("send yourself",3,2);
        lcd.printString("to the right",6,3);
        lcd.printString("Wormhole",6,4);
        lcd.drawSprite(0,40,8,84,(int *)intro2);
        lcd.refresh();
        wait(0.5);
    }
    
    
}

void render() {
    
    // clear screen, re-draw and refresh
    lcd.clear();  
    eng.draw(lcd);
    lcd.refresh();
}

void endgame() {
    lcd.clear();
        lcd.printString("Endgame",12,1);
        lcd.refresh();
    pad.tone(587.3,0.63);
    wait(0.63);
    pad.tone(0.0,0.06);
    wait(0.06);
    pad.tone(587.3,0.63);
    wait(0.63);
    pad.tone(0.0,0.06);
    wait(0.06);
    pad.tone(587.3,0.63);
    wait(0.63);
    pad.tone(0.0,0.06);
    wait(0.06);
    pad.tone(698.5,0.42);
    wait(0.42);
    pad.tone(659.3,0.21);
    wait(0.21);
    pad.tone(0.0,0.06);
    wait(0.06);
    pad.tone(659.3,0.42);
    wait(0.42);
    pad.tone(587.3,0.21);
    wait(0.21);
    pad.tone(0.0,0.06);
    wait(0.06);
    pad.tone(587.3,0.42);
    wait(0.42);
    pad.tone(554.4,0.21);
    wait(0.21);
    pad.tone(587.3,0.42);
    wait(0.42);
}