This is test version of Pokemongo game. ELEC 2645 final project.

Dependencies:   Tone

Committer:
shalwego
Date:
Thu Apr 15 15:35:12 2021 +0000
Revision:
0:819c2d6a69ac
Issue about music playing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shalwego 0:819c2d6a69ac 1
shalwego 0:819c2d6a69ac 2 #include "PokeEngine.h"
shalwego 0:819c2d6a69ac 3
shalwego 0:819c2d6a69ac 4 PokeEngine::PokeEngine(){ _lives = 4; }
shalwego 0:819c2d6a69ac 5
shalwego 0:819c2d6a69ac 6 void PokeEngine::init(int ball_x, int ball_y, int ball_radius, int pokemon_x, int pokemon_y, int pokemon_size, int speed){
shalwego 0:819c2d6a69ac 7 _pokeball.init(ball_x, ball_y, ball_radius);
shalwego 0:819c2d6a69ac 8 _pokemon.init(pokemon_x, pokemon_y, pokemon_size, speed);
shalwego 0:819c2d6a69ac 9 _score = 0;
shalwego 0:819c2d6a69ac 10 }
shalwego 0:819c2d6a69ac 11
shalwego 0:819c2d6a69ac 12 void PokeEngine::init_rocket(int x, int y, int size, int speed){
shalwego 0:819c2d6a69ac 13 _rocket.init(x,y,size, speed);
shalwego 0:819c2d6a69ac 14 }
shalwego 0:819c2d6a69ac 15
shalwego 0:819c2d6a69ac 16 int PokeEngine::update(UserInput input){
shalwego 0:819c2d6a69ac 17 _pokeball.update(input);
shalwego 0:819c2d6a69ac 18 Position2D ball_pos = _pokeball.get_pos();
shalwego 0:819c2d6a69ac 19 _pokemon.update();
shalwego 0:819c2d6a69ac 20 _rocket.update(ball_pos);
shalwego 0:819c2d6a69ac 21
shalwego 0:819c2d6a69ac 22 check_wall_collision();
shalwego 0:819c2d6a69ac 23 check_catching_collision();
shalwego 0:819c2d6a69ac 24 check_crash();
shalwego 0:819c2d6a69ac 25 return _lives;
shalwego 0:819c2d6a69ac 26 }
shalwego 0:819c2d6a69ac 27
shalwego 0:819c2d6a69ac 28 void PokeEngine::draw(N5110 &lcd) {
shalwego 0:819c2d6a69ac 29 //printf("Pong Engine: Draw\n");
shalwego 0:819c2d6a69ac 30 // draw the elements in the LCD buffer
shalwego 0:819c2d6a69ac 31 // pitch
shalwego 0:819c2d6a69ac 32 lcd.drawLine(0,0,WIDTH-1,0,1); // top
shalwego 0:819c2d6a69ac 33 lcd.drawLine(WIDTH-1,0,WIDTH-1,HEIGHT-1,1); // back wall
shalwego 0:819c2d6a69ac 34 lcd.drawLine(0,HEIGHT-1,WIDTH-1,HEIGHT-1,1); // bottom
shalwego 0:819c2d6a69ac 35 lcd.drawLine(0,0,0,HEIGHT-1,1);
shalwego 0:819c2d6a69ac 36 _pokeball.draw(lcd);
shalwego 0:819c2d6a69ac 37 _pokemon.draw(lcd);
shalwego 0:819c2d6a69ac 38 _rocket.draw(lcd);
shalwego 0:819c2d6a69ac 39 }
shalwego 0:819c2d6a69ac 40
shalwego 0:819c2d6a69ac 41 void PokeEngine::check_wall_collision() {
shalwego 0:819c2d6a69ac 42 //printf("Pong Engine: Check Wall Collision\n");
shalwego 0:819c2d6a69ac 43 // read current ball attributes
shalwego 0:819c2d6a69ac 44 Position2D pokemon_pos = _pokemon.get_pos();
shalwego 0:819c2d6a69ac 45 Position2D pokemon_velocity = _pokemon.get_velocity();
shalwego 0:819c2d6a69ac 46 int size = _pokemon.get_size();
shalwego 0:819c2d6a69ac 47
shalwego 0:819c2d6a69ac 48 // check if hit top wall
shalwego 0:819c2d6a69ac 49 if (pokemon_pos.y <= 1) { // 1 due to 1 pixel boundary
shalwego 0:819c2d6a69ac 50 pokemon_pos.y = 1; // bounce off ceiling without going off screen
shalwego 0:819c2d6a69ac 51 pokemon_velocity.y = -pokemon_velocity.y; // flip velocity
shalwego 0:819c2d6a69ac 52 } else if (pokemon_pos.y + size >= (HEIGHT-1) ) {
shalwego 0:819c2d6a69ac 53 // hit bottom
shalwego 0:819c2d6a69ac 54 pokemon_pos.y = (HEIGHT-1) - size; // stops ball going off screen
shalwego 0:819c2d6a69ac 55 pokemon_velocity.y = -pokemon_velocity.y; // flip velcoity
shalwego 0:819c2d6a69ac 56 } else if (pokemon_pos.x + size >= (WIDTH-1) ) {
shalwego 0:819c2d6a69ac 57 // hit right wall
shalwego 0:819c2d6a69ac 58 pokemon_pos.x = (WIDTH-1) - size; // stops ball going off screen
shalwego 0:819c2d6a69ac 59 pokemon_velocity.x = -pokemon_velocity.x; // flip velcoity
shalwego 0:819c2d6a69ac 60 } else if (pokemon_pos.x <= 1) { // 1 due to 1 pixel boundary
shalwego 0:819c2d6a69ac 61 pokemon_pos.x = 1; // bounce off ceiling without going off screen
shalwego 0:819c2d6a69ac 62 pokemon_velocity.x = -pokemon_velocity.x; // flip velocity
shalwego 0:819c2d6a69ac 63 }
shalwego 0:819c2d6a69ac 64
shalwego 0:819c2d6a69ac 65 // update ball parameters
shalwego 0:819c2d6a69ac 66 _pokemon.set_velocity(pokemon_velocity);
shalwego 0:819c2d6a69ac 67 _pokemon.set_pos(pokemon_pos);
shalwego 0:819c2d6a69ac 68 }
shalwego 0:819c2d6a69ac 69
shalwego 0:819c2d6a69ac 70 void PokeEngine::check_catching_collision() {
shalwego 0:819c2d6a69ac 71 Position2D pokemon_pos = _pokemon.get_pos();
shalwego 0:819c2d6a69ac 72 Position2D pokeball_pos = _pokeball.get_pos(); // paddle
shalwego 0:819c2d6a69ac 73
shalwego 0:819c2d6a69ac 74 // see if ball has hit the paddle by checking for overlaps
shalwego 0:819c2d6a69ac 75 if (
shalwego 0:819c2d6a69ac 76 (pokemon_pos.y >= pokeball_pos.y - _pokeball.get_radius()) && //top
shalwego 0:819c2d6a69ac 77 (pokemon_pos.y <= pokeball_pos.y + _pokeball.get_radius() ) && //bottom
shalwego 0:819c2d6a69ac 78 (pokemon_pos.x >= pokeball_pos.x - _pokeball.get_radius()) && //left
shalwego 0:819c2d6a69ac 79 (pokemon_pos.x <= pokeball_pos.x + _pokeball.get_radius() ) //right
shalwego 0:819c2d6a69ac 80 ) {
shalwego 0:819c2d6a69ac 81 // if it has,the pokemon is caught.
shalwego 0:819c2d6a69ac 82 _pokemon.pokemon_caught();
shalwego 0:819c2d6a69ac 83 _score++;
shalwego 0:819c2d6a69ac 84 }
shalwego 0:819c2d6a69ac 85 }
shalwego 0:819c2d6a69ac 86
shalwego 0:819c2d6a69ac 87
shalwego 0:819c2d6a69ac 88 void PokeEngine::check_crash() {
shalwego 0:819c2d6a69ac 89 int left_lives = _lives;
shalwego 0:819c2d6a69ac 90 Position2D rocket_pos = _rocket.get_pos();
shalwego 0:819c2d6a69ac 91 int rocket_size = _rocket.get_size();
shalwego 0:819c2d6a69ac 92 Position2D pokeball_pos = _pokeball.get_pos(); // paddle
shalwego 0:819c2d6a69ac 93
shalwego 0:819c2d6a69ac 94 // see if ball has hit the paddle by checking for overlaps
shalwego 0:819c2d6a69ac 95 if (
shalwego 0:819c2d6a69ac 96 (rocket_pos.y >= pokeball_pos.y - _pokeball.get_radius()) && //top
shalwego 0:819c2d6a69ac 97 (rocket_pos.y <= pokeball_pos.y + _pokeball.get_radius() ) && //bottom
shalwego 0:819c2d6a69ac 98 (rocket_pos.x >= pokeball_pos.x - _pokeball.get_radius()) && //left
shalwego 0:819c2d6a69ac 99 (rocket_pos.x <= pokeball_pos.x + _pokeball.get_radius() ) //right
shalwego 0:819c2d6a69ac 100 ) {
shalwego 0:819c2d6a69ac 101 // if it has,the pokemon is caught.
shalwego 0:819c2d6a69ac 102 _rocket.rocket_crash();
shalwego 0:819c2d6a69ac 103 _lives--;
shalwego 0:819c2d6a69ac 104 }
shalwego 0:819c2d6a69ac 105 }
shalwego 0:819c2d6a69ac 106
shalwego 0:819c2d6a69ac 107
shalwego 0:819c2d6a69ac 108 int PokeEngine::get_lives() { return _lives;}
shalwego 0:819c2d6a69ac 109
shalwego 0:819c2d6a69ac 110 int PokeEngine::get_score() { return _score; }
shalwego 0:819c2d6a69ac 111
shalwego 0:819c2d6a69ac 112 void PokeEngine::check_lives(int lives) { _lives = lives ;}
shalwego 0:819c2d6a69ac 113