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-17
Revision:
13:02002658e718
Child:
14:abe64fe0b6a5

File content as of revision 13:02002658e718:

#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;

}

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

void Game_two::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.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);}
    print_scores(lcd);
    // spawn coin
    if(_type < _rand) {_coin.draw(lcd);}
    else {_block.draw(lcd);}
}

void Game_two::update(Gamepad &pad, N5110 &lcd)
{
    if(_type < _rand) {
        check_miss_coin(pad);
        _p1.update(_d,_mag);
        _coin.update();
        check_player_collect(pad);
    }
    else {
        check_miss_block(pad);
        _p1.update(_d,_mag);
        _block.update();
        check_player_collide(pad, lcd);
    }
}

void Game_two::check_player_collect(Gamepad &pad)
{
    // read current coin attributes
    Vector2D coin_pos = _coin.get_pos();
    int coin_speed = _coin.get_velocity();

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

    // see if coin has hit the player by checking for overlaps
    if (
        (coin_pos.y >= p1_pos.y) && //top
        (coin_pos.y <= p1_pos.y + 21) && //bottom
        (coin_pos.x >= _p1x) && //left
        (coin_pos.x <= _p1x + 17)  //right
    ) 
    {
        // write new attributes
        _p1.add_score();
        _coin.init(_speed);
        music.coin(pad);
        _count++;
        _type = rand() % 8; // randomise type
    }
}

void Game_two::check_player_collide(Gamepad &pad, N5110 &lcd)
{
    // read current block attributes
    Vector2D block_pos = _block.get_pos();
    int block_speed = _block.get_velocity();

    Vector2D p1_pos = _p1.get_pos();

    // see if block has hit the player by checking for overlaps
    if (
        (block_pos.y >= p1_pos.y - 6) && //top
        (block_pos.y <= p1_pos.y + 14) && //bottom
        (block_pos.x >= _p1x) && //left
        (block_pos.x <= _p1x + 17)  //right
    ) 
    {
        // write new attributes
        _block.init(_speed);
        gameover(lcd, pad);
    }
}

void Game_two::check_miss_coin(Gamepad &pad)
{
    Vector2D coin_pos = _coin.get_pos();
    // player has missed
    if (coin_pos.x > WIDTH) {
        _coin.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
    }
    
}

int Game_two::print_scores(N5110 &lcd)
{
    // get scores from paddles
    int p1_score = _p1.get_score();
    int total = get_count();

    // print to LCD i
    char buffer1[14];
    sprintf(buffer1,"%2d / %2d",p1_score, total);
    lcd.printString(buffer1,WIDTH/2 - 20,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
    return p1_score;
}

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 a block!",0,1);
        lcd.printString("You are late",0,2);
        lcd.printString("for class!",0,3);
        lcd.printString("Press B",0,5);
        lcd.refresh();
        wait(0.1);
    }
    _count = 10;
}

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("Squirtle",0,0);
                lcd.printString("is rushing",0,1);
                lcd.printString("to his lecture",0,2);
                lcd.printString("Collect coins",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("so he can have",0,0);
                lcd.printString("lunch and ",0,1);
                lcd.printString("reach the",0,2);
                lcd.printString("classroom in",0,3);
                lcd.printString("time by ",0,4);
                lcd.drawSprite(39, 44, 4, 7, (int *)instruct_data);
                lcd.refresh();
                if(pad.get_direction() == S){currentPage = PAGE_3;}
                wait(1.0f/fps);
            break;
            case PAGE_3:
                lcd.clear();
                lcd.printString("avoiding the",0,0);
                lcd.printString("blocks",0,1);
                lcd.printString("Press A",0,5);
                lcd.refresh();
                wait(1.0f/fps);

        }
    }
}

*/