![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
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
Diff: Game_one/Game_one.cpp
- Revision:
- 2:464c7e62d97d
- Child:
- 4:5bc9c4363d31
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Game_one/Game_one.cpp Wed Apr 03 03:06:00 2019 +0000 @@ -0,0 +1,207 @@ +#include "Game_one.h" + +Game_one::Game_one() +{ + +} + +Game_one::~Game_one() +{ + +} + +void Game_one::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; + + // x position on screen - WIDTH is defined in N5110.h + _p1x = 61; + + // initialize round counter + _count = 0; + _alt = 0; + + // puts paddles and ball in middle + _p1.init(_p1x); + _coin.init(_speed); + _block.init(_speed); + + // 4 possibilities. 3 for coin, 1 for block. + srand(time(NULL)); + _type = rand() % 4; // randomise type. +} + +void Game_one::render(N5110 &lcd, int cha) +{ + lcd.clear(); + draw(lcd, cha); + lcd.refresh(); +} + +void Game_one::read_input(Gamepad &pad) +{ + _d = pad.get_direction(); + _mag = pad.get_mag(); +} + +void Game_one::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_one::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_one::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_one::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 + 2) && //top + (block_pos.y <= p1_pos.y + 19) && //bottom + (block_pos.x >= _p1x) && //left + (block_pos.x <= _p1x + 17) //right + ) + { + // write new attributes + _block.init(_speed); + gameover(lcd, pad); + } +} + +void Game_one::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_one::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_one::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_one::set_count(int count) +{ + _count = count; +} + +int Game_one::get_count() +{ + int count = _count; + return count; +} + +void Game_one::set_alt(int alt) +{ + _alt = alt; +} + +int Game_one::update_alt() +{ + if(_alt < 4){_alt = _alt++;} + else {_alt=0;} + int alt = _alt; + return alt; +} + +void Game_one::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; +} \ No newline at end of file