Game codes for Pokemon Academy Yiu Fai Kwok - 201198802 I have read the University Regulations on Plagiarism and state that the work covered by this declaration is my own and does not contain any unacknowledged work from other sources.

Dependencies:   mbed FXOS8700CQ mbed-rtos

Game_two/Game_two.cpp

Committer:
yfkwok
Date:
2019-04-20
Revision:
15:a65444eb1194
Parent:
14:abe64fe0b6a5
Child:
19:903d67bb0dea

File content as of revision 15:a65444eb1194:

#include "Game_two.h"

Game_two::Game_two()
{
    
}

Game_two::~Game_two()
{
    
} 

//Enumerate instruction menu from page 1 - 3
enum Page {
    PAGE_1, PAGE_2, PAGE_3   
};

void Game_two::init(int speed, int cha, int r)
{
    // Set the speed of the objects
    _speed = speed;
    
    // Set the character being drawn
    _cha = cha;
    
    // Set the random parameter for coin/block ratio
    _rand = r; 
    
    // initialize round counter
    _count = 0;
    _alt = 0;
    
    // Game initiation
    _p1.init();
    
    // 4 possibilities. 3 for coin, 1 for block.
    srand(time(NULL));
    _type = rand() % 4; // randomise type.
    _so = rand() % 3;

}

void Game_two::render(N5110 &lcd, int cha)
{
    lcd.clear();
    draw(lcd, cha);
    lcd.refresh();    
}

void Game_two::read_input(FXOS8700CQ &device)
{
    _d = device.get_direction();
    _mag = device.get_mag();
}

void Game_two::draw(N5110 &lcd, int cha)
{
    int alt = update_alt();
    // draw player on lcd
    if(alt <= 1){_p1.draw(lcd, cha);}
    else {_p1.draw_alt(lcd, cha);}
    // spawn coin
    if(_type < _rand) {
        if (_so == 0) {_insta.draw(lcd);}
        else if (_so == 1) {_face.draw(lcd);}
        else if (_so == 2) {_twitt.draw(lcd);}
    }
    else {_yt.draw(lcd);}
}

void Game_two::update(Gamepad &pad, N5110 &lcd, int cha)
{
    if (_type < _rand) {
        if (_so == 0){
            check_miss_insta(pad);
            _p1.update(_d,_mag);
            _insta.update();
            check_insta_collide(pad, lcd, cha);
        } else if (_so == 1){
            check_miss_face(pad);
            _p1.update(_d,_mag);
            _face.update();
            check_face_collide(pad, lcd, cha);
        } else if (_so == 2){
            check_miss_twitt(pad);
            _p1.update(_d,_mag);
            _twitt.update();
            check_twitt_collide(pad, lcd, cha);
        }
    }
    else {
        check_miss_yt(pad);
        _p1.update(_d,_mag);
        _yt.update();
        check_yt_collide(pad, lcd, cha);
    }
}

void Game_two::check_insta_collide(Gamepad &pad, N5110 &lcd, int cha)
{
    // read current coin attributes
    Vector2D insta_pos = _insta.get_pos();
    Vector2D insta_speed = _insta.get_velocity();

    // check p1 first
    Vector2D p1_pos = _p1.get_pos();

    // see if coin has hit the player by checking for overlaps
    if (cha == 1){
        if (
            (insta_pos.y >= p1_pos.y - 8) && //top
            (insta_pos.y <= p1_pos.y + 18) && //bottom
            (insta_pos.x >= p1_pos.x - 6) && //left
            (insta_pos.x <= p1_pos.x + 20)  //right
        ) 
        {
            // write new attributes
            _insta.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 2){
        if (
            (insta_pos.y >= p1_pos.y - 8) && //top
            (insta_pos.y <= p1_pos.y + 21) && //bottom
            (insta_pos.x >= p1_pos.x - 6) && //left
            (insta_pos.x <= p1_pos.x + 21)  //right
        ) 
        {
            // write new attributes
            _insta.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 3){
        if (
            (insta_pos.y >= p1_pos.y - 8) && //top
            (insta_pos.y <= p1_pos.y + 21) && //bottom
            (insta_pos.x >= p1_pos.x - 6) && //left
            (insta_pos.x <= p1_pos.x + 29)  //right
        ) 
        {
            // write new attributes
            _insta.init(_speed);
            gameover(lcd, pad);
        }
    }
}

void Game_two::check_miss_insta(Gamepad &pad)
{
    Vector2D insta_pos = _insta.get_pos();
    // player has missed
    if ((insta_pos.x > 84) || (insta_pos.x < -15) || (insta_pos.y < -15) || (insta_pos.y > 50)) {
        _insta.init(_speed);
        pad.tone(750.0,0.1);
        _count++;
        wait(0.1);
        _type = rand() % 8; // randomise type
    } 
}

void Game_two::check_face_collide(Gamepad &pad, N5110 &lcd, int cha)
{
    // read current coin attributes
    Vector2D face_pos = _face.get_pos();
    Vector2D face_speed = _face.get_velocity();

    // check p1 first
    Vector2D p1_pos = _p1.get_pos();

    // see if coin has hit the player by checking for overlaps
    if (cha == 1){
        if (
            (face_pos.y >= p1_pos.y - 8) && //top
            (face_pos.y <= p1_pos.y + 18) && //bottom
            (face_pos.x >= p1_pos.x - 6) && //left
            (face_pos.x <= p1_pos.x + 20)  //right
        ) 
        {
            // write new attributes
            _face.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 2){
        if (
            (face_pos.y >= p1_pos.y - 8) && //top
            (face_pos.y <= p1_pos.y + 21) && //bottom
            (face_pos.x >= p1_pos.x - 6) && //left
            (face_pos.x <= p1_pos.x + 21)  //right
        ) 
        {
            // write new attributes
            _face.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 3){
        if (
            (face_pos.y >= p1_pos.y - 8) && //top
            (face_pos.y <= p1_pos.y + 21) && //bottom
            (face_pos.x >= p1_pos.x - 6) && //left
            (face_pos.x <= p1_pos.x + 29)  //right
        ) 
        {
            // write new attributes
            _face.init(_speed);
            gameover(lcd, pad);
        }
    }
}

void Game_two::check_miss_face(Gamepad &pad)
{
    Vector2D face_pos = _face.get_pos();
    // player has missed
    if ((face_pos.x > 84) || (face_pos.x < -15) || (face_pos.y < -15) || (face_pos.y > 50)) {
        _face.init(_speed);
        pad.tone(750.0,0.1);
        _count++;
        wait(0.1);
        _type = rand() % 8; // randomise type
    } 
}

void Game_two::check_twitt_collide(Gamepad &pad, N5110 &lcd, int cha)
{
    // read current coin attributes
    Vector2D twitt_pos = _twitt.get_pos();
    Vector2D twitt_speed = _twitt.get_velocity();

    // check p1 first
    Vector2D p1_pos = _p1.get_pos();

    // see if coin has hit the player by checking for overlaps
    if (cha == 1){
        if (
            (twitt_pos.y >= p1_pos.y - 8) && //top
            (twitt_pos.y <= p1_pos.y + 18) && //bottom
            (twitt_pos.x >= p1_pos.x - 6) && //left
            (twitt_pos.x <= p1_pos.x + 20)  //right
        ) 
        {
            // write new attributes
            _twitt.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 2){
        if (
            (twitt_pos.y >= p1_pos.y - 8) && //top
            (twitt_pos.y <= p1_pos.y + 21) && //bottom
            (twitt_pos.x >= p1_pos.x - 6) && //left
            (twitt_pos.x <= p1_pos.x + 21)  //right
        ) 
        {
            // write new attributes
            _twitt.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 3){
        if (
            (twitt_pos.y >= p1_pos.y - 8) && //top
            (twitt_pos.y <= p1_pos.y + 21) && //bottom
            (twitt_pos.x >= p1_pos.x - 6) && //left
            (twitt_pos.x <= p1_pos.x + 29)  //right
        ) 
        {
            // write new attributes
            _twitt.init(_speed);
            gameover(lcd, pad);
        }
    }
}

void Game_two::check_miss_twitt(Gamepad &pad)
{
    Vector2D twitt_pos = _twitt.get_pos();
    // player has missed
    if ((twitt_pos.x > 84) || (twitt_pos.x < -15) || (twitt_pos.y < -15) || (twitt_pos.y > 50)) {
        _twitt.init(_speed);
        pad.tone(750.0,0.1);
        _count++;
        wait(0.1);
        _type = rand() % 8; // randomise type
    } 
}

void Game_two::check_yt_collide(Gamepad &pad, N5110 &lcd, int cha)
{
    // read current coin attributes
    Vector2D yt_pos = _yt.get_pos();
    Vector2D yt_speed = _yt.get_velocity();

    // check p1 first
    Vector2D p1_pos = _p1.get_pos();

    // see if coin has hit the player by checking for overlaps
    if (cha == 1){
        if (
            (yt_pos.y >= p1_pos.y - 8) && //top
            (yt_pos.y <= p1_pos.y + 18) && //bottom
            (yt_pos.x >= p1_pos.x - 25) && //left
            (yt_pos.x <= p1_pos.x + 20)  //right
        ) 
        {
            // write new attributes
            _yt.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 2){
        if (
            (yt_pos.y >= p1_pos.y - 8) && //top
            (yt_pos.y <= p1_pos.y + 21) && //bottom
            (yt_pos.x >= p1_pos.x - 25) && //left
            (yt_pos.x <= p1_pos.x + 21)  //right
        ) 
        {
            // write new attributes
            _yt.init(_speed);
            gameover(lcd, pad);
        }
    }
    if (cha == 3){
        if (
            (yt_pos.y >= p1_pos.y - 8) && //top
            (yt_pos.y <= p1_pos.y + 21) && //bottom
            (yt_pos.x >= p1_pos.x - 25) && //left
            (yt_pos.x <= p1_pos.x + 29)  //right
        ) 
        {
            // write new attributes
            _yt.init(_speed);
            gameover(lcd, pad);
        }
    }
}

void Game_two::check_miss_yt(Gamepad &pad)
{
    Vector2D yt_pos = _yt.get_pos();
    // player has missed
    if ((yt_pos.x > 84) || (yt_pos.x < -15) || (yt_pos.y < -15) || (yt_pos.y > 50)) {
        _yt.init(_speed);
        pad.tone(750.0,0.1);
        _count++;
        wait(0.1);
        _type = rand() % 8; // randomise type
    } 
}

/*void Game_two::check_miss_block(Gamepad &pad)
{
    Vector2D block_pos = _block.get_pos();
    // player has missed
    if (block_pos.x > WIDTH) {
        _block.init(_speed);
        pad.tone(750.0,0.1);
        wait(0.1);
        _type = rand() % 8; // randomise type
    }
    
}*/

void Game_two::set_count(int count)
{
    _count = count;
}

int Game_two::get_count()
{
    int count = _count;
    return count;
}

void Game_two::set_alt(int alt)
{
    _alt = alt;
}

int Game_two::update_alt()
{
    if(_alt < 4){_alt = _alt++;}
    else {_alt=0;}
    int alt = _alt;
    return alt;
}

void Game_two::gameover(N5110 &lcd, Gamepad &pad)
{
    while(pad.check_event(Gamepad::B_PRESSED) == false) {
        lcd.clear();
        lcd.printString("You rushed ",0,0);
        lcd.printString("into social",0,1);
        lcd.printString("media!",0,2);
        lcd.printString("Game Over!",0,3);
        lcd.printString("Press B",0,5);
        lcd.refresh();
        wait(0.1);
    }
    _count = 30;
}

void Game_two::intro(Gamepad &pad, N5110 &lcd)
{
    Page currentPage = PAGE_1;
    int fps = 8.0;
    int instruct_data[4][7] = {
        {1,1,1,1,1,1,1},
        {0,1,1,1,1,1,0},
        {0,0,1,1,1,0,0},
        {0,0,0,1,0,0,0},
    };
    
    while(pad.check_event(Gamepad::A_PRESSED) == false){
        switch (currentPage) {
            case PAGE_1:
                lcd.clear();
                lcd.printString("Charmander",0,0);
                lcd.printString("is trying",0,1);
                lcd.printString("to study!",0,2);
                lcd.printString("Avoid all",0,4);
                lcd.drawSprite(39, 44, 4, 7, (int *)instruct_data);
                lcd.refresh();
                if(pad.get_direction() == S){currentPage = PAGE_2;}
                wait(1.0f/fps);
            break;
            case PAGE_2:
                lcd.clear();
                lcd.printString("distractions",0,0);
                lcd.printString("so he can pass",0,1);
                lcd.printString("the test!",0,2);
                lcd.printString("Press A",0,5);
                lcd.refresh();
                wait(1.0f/fps);
            break;
        }
    }
}