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

Committer:
yfkwok
Date:
Thu May 09 00:41:05 2019 +0000
Revision:
34:3ddfaa217eca
Parent:
33:f7ec806e14b6
09/05/2019 - Last commit before submission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yfkwok 14:abe64fe0b6a5 1 #include "Game_two_cha.h"
yfkwok 14:abe64fe0b6a5 2
yfkwok 14:abe64fe0b6a5 3 Game_two_cha::Game_two_cha()
yfkwok 14:abe64fe0b6a5 4 {
yfkwok 14:abe64fe0b6a5 5
yfkwok 14:abe64fe0b6a5 6 }
yfkwok 14:abe64fe0b6a5 7
yfkwok 14:abe64fe0b6a5 8 Game_two_cha::~Game_two_cha()
yfkwok 14:abe64fe0b6a5 9 {
yfkwok 14:abe64fe0b6a5 10
yfkwok 14:abe64fe0b6a5 11 }
yfkwok 14:abe64fe0b6a5 12
yfkwok 14:abe64fe0b6a5 13 void Game_two_cha::init()
yfkwok 14:abe64fe0b6a5 14 {
yfkwok 33:f7ec806e14b6 15 _x = WIDTH/2 - 9; //x position fixed
yfkwok 33:f7ec806e14b6 16 _y = HEIGHT/2 - 9; //y initial position at mid-point of screen
yfkwok 33:f7ec806e14b6 17 _speed = 1; // default speed
yfkwok 33:f7ec806e14b6 18 _score = 0; // start score from zero
yfkwok 14:abe64fe0b6a5 19 }
yfkwok 14:abe64fe0b6a5 20
yfkwok 33:f7ec806e14b6 21 // Function for drawing in game sprite
yfkwok 14:abe64fe0b6a5 22 void Game_two_cha::draw(N5110 &lcd, int cha)
yfkwok 14:abe64fe0b6a5 23 {
yfkwok 33:f7ec806e14b6 24 if(cha == 1){_c1.draw_cha1_sprite(_x, _y, lcd);} // Draw sprite for year 1
yfkwok 33:f7ec806e14b6 25 else if(cha == 2){_c1.draw_cha2_sprite(_x, _y, lcd);} // Draw sprite for year 2
yfkwok 33:f7ec806e14b6 26 else if(cha == 3){_c1.draw_cha3_sprite(_x, _y, lcd);} // Draw sprite for year 3
yfkwok 14:abe64fe0b6a5 27 }
yfkwok 14:abe64fe0b6a5 28
yfkwok 33:f7ec806e14b6 29 // Function for drawing in game alternative sprite to create running animation
yfkwok 14:abe64fe0b6a5 30 void Game_two_cha::draw_alt(N5110 &lcd, int cha)
yfkwok 14:abe64fe0b6a5 31 {
yfkwok 33:f7ec806e14b6 32 if(cha == 1){_c1.draw_cha1_alt_sprite(_x, _y, lcd);} // Draw sprite for year 1
yfkwok 33:f7ec806e14b6 33 else if(cha == 2){_c1.draw_cha2_alt_sprite(_x, _y, lcd);} // Draw sprite for year 2
yfkwok 33:f7ec806e14b6 34 else if(cha == 3){_c1.draw_cha3_alt_sprite(_x, _y, lcd);} // Draw sprite for year 3
yfkwok 14:abe64fe0b6a5 35 }
yfkwok 14:abe64fe0b6a5 36
yfkwok 33:f7ec806e14b6 37 // Update the character postion depending on the Gamepad input
yfkwok 14:abe64fe0b6a5 38 void Game_two_cha::update(Direction d,float mag)
yfkwok 14:abe64fe0b6a5 39 {
yfkwok 14:abe64fe0b6a5 40 _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
yfkwok 14:abe64fe0b6a5 41
yfkwok 14:abe64fe0b6a5 42 // update y value depending on direction of movement
yfkwok 14:abe64fe0b6a5 43 // North is decrement as origin is at the top-left so decreasing moves up
yfkwok 21:704d938acf5d 44 if (d == N) {_y-=-_speed*0.7;}
yfkwok 21:704d938acf5d 45 else if (d == NE) {_y-=-_speed*0.7; _x+=_speed;}
yfkwok 14:abe64fe0b6a5 46 else if (d == E) {_x+=_speed;}
yfkwok 21:704d938acf5d 47 else if (d == SE) {_y+=-_speed*0.7; _x+=_speed;}
yfkwok 21:704d938acf5d 48 else if (d == S) {_y+=-_speed*0.7;}
yfkwok 21:704d938acf5d 49 else if (d == SW) {_y+=-_speed*0.7; _x-=_speed;}
yfkwok 14:abe64fe0b6a5 50 else if (d == W) {_x-=_speed;}
yfkwok 21:704d938acf5d 51 else if (d == NW) {_y-=-_speed*0.7; _x-=_speed;}
yfkwok 14:abe64fe0b6a5 52
yfkwok 14:abe64fe0b6a5 53 // check the y origin to ensure that the character doesn't go off screen
yfkwok 14:abe64fe0b6a5 54 if (_y < 1) {_y = 1;}
yfkwok 14:abe64fe0b6a5 55 if (_y > HEIGHT - 18) {_y = HEIGHT - 18;}
yfkwok 21:704d938acf5d 56 if (_x < 1) {_x = 1;}
yfkwok 21:704d938acf5d 57 if (_x > WIDTH - 18) {_x = WIDTH - 18;}
yfkwok 14:abe64fe0b6a5 58 }
yfkwok 14:abe64fe0b6a5 59
yfkwok 33:f7ec806e14b6 60 // Function to increase the score in the game
yfkwok 14:abe64fe0b6a5 61 void Game_two_cha::add_score()
yfkwok 14:abe64fe0b6a5 62 {
yfkwok 14:abe64fe0b6a5 63 _score++;
yfkwok 14:abe64fe0b6a5 64 }
yfkwok 14:abe64fe0b6a5 65
yfkwok 33:f7ec806e14b6 66 // Function to fetch the current score
yfkwok 14:abe64fe0b6a5 67 int Game_two_cha::get_score()
yfkwok 14:abe64fe0b6a5 68 {
yfkwok 14:abe64fe0b6a5 69 return _score;
yfkwok 14:abe64fe0b6a5 70 }
yfkwok 14:abe64fe0b6a5 71
yfkwok 33:f7ec806e14b6 72 // Function to fetch the current position of the character
yfkwok 14:abe64fe0b6a5 73 Vector2D Game_two_cha::get_pos()
yfkwok 14:abe64fe0b6a5 74 {
yfkwok 14:abe64fe0b6a5 75 Vector2D p = {_x,_y};
yfkwok 14:abe64fe0b6a5 76 return p;
yfkwok 14:abe64fe0b6a5 77 }