![](/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_two/Game_two.cpp
- Revision:
- 14:abe64fe0b6a5
- Parent:
- 13:02002658e718
- Child:
- 15:a65444eb1194
--- a/Game_two/Game_two.cpp Wed Apr 17 17:53:56 2019 +0000 +++ b/Game_two/Game_two.cpp Thu Apr 18 04:53:04 2019 +0000 @@ -29,20 +29,28 @@ // 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) +void Game_two::render(N5110 &lcd, int cha) { lcd.clear(); draw(lcd, cha); lcd.refresh(); } -void Game_two::read_input(Gamepad &pad) +void Game_two::read_input(FXOS8700CQ &device) { - _d = pad.get_direction(); - _mag = pad.get_mag(); + _d = device.get_direction(); + _mag = device.get_mag(); } void Game_two::draw(N5110 &lcd, int cha) @@ -51,91 +59,300 @@ // 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);} + 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) +void Game_two::update(Gamepad &pad, N5110 &lcd, int cha) { - if(_type < _rand) { - check_miss_coin(pad); - _p1.update(_d,_mag); - _coin.update(); - check_player_collect(pad); + 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_block(pad); + check_miss_yt(pad); _p1.update(_d,_mag); - _block.update(); - check_player_collide(pad, lcd); + _yt.update(); + check_yt_collide(pad, lcd, cha); } } -void Game_two::check_player_collect(Gamepad &pad) +void Game_two::check_insta_collide(Gamepad &pad, N5110 &lcd, int cha) { // read current coin attributes - Vector2D coin_pos = _coin.get_pos(); - int coin_speed = _coin.get_velocity(); + 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 ( - (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 + 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_player_collide(Gamepad &pad, N5110 &lcd) +void Game_two::check_miss_face(Gamepad &pad) { - // 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(); + Vector2D face_pos = _face.get_pos(); // player has missed - if (coin_pos.x > WIDTH) { - _coin.init(_speed); + 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_block(Gamepad &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 @@ -159,7 +376,7 @@ 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) { @@ -190,14 +407,14 @@ 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("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 = 10; + _count = 30; } void Game_two::intro(Gamepad &pad, N5110 &lcd) @@ -215,10 +432,10 @@ 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.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;} @@ -226,26 +443,13 @@ 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("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; } } -} - -*/ \ No newline at end of file +} \ No newline at end of file