![](/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
Game_one/Game_one.cpp@2:464c7e62d97d, 2019-04-03 (annotated)
- Committer:
- yfkwok
- Date:
- Wed Apr 03 03:06:00 2019 +0000
- Revision:
- 2:464c7e62d97d
- Child:
- 4:5bc9c4363d31
Date 3/4/2019 - Mini game one beta complete
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yfkwok | 2:464c7e62d97d | 1 | #include "Game_one.h" |
yfkwok | 2:464c7e62d97d | 2 | |
yfkwok | 2:464c7e62d97d | 3 | Game_one::Game_one() |
yfkwok | 2:464c7e62d97d | 4 | { |
yfkwok | 2:464c7e62d97d | 5 | |
yfkwok | 2:464c7e62d97d | 6 | } |
yfkwok | 2:464c7e62d97d | 7 | |
yfkwok | 2:464c7e62d97d | 8 | Game_one::~Game_one() |
yfkwok | 2:464c7e62d97d | 9 | { |
yfkwok | 2:464c7e62d97d | 10 | |
yfkwok | 2:464c7e62d97d | 11 | } |
yfkwok | 2:464c7e62d97d | 12 | |
yfkwok | 2:464c7e62d97d | 13 | void Game_one::init(int speed, int cha, int r) |
yfkwok | 2:464c7e62d97d | 14 | { |
yfkwok | 2:464c7e62d97d | 15 | // Set the speed of the objects |
yfkwok | 2:464c7e62d97d | 16 | _speed = speed; |
yfkwok | 2:464c7e62d97d | 17 | |
yfkwok | 2:464c7e62d97d | 18 | // Set the character being drawn |
yfkwok | 2:464c7e62d97d | 19 | _cha = cha; |
yfkwok | 2:464c7e62d97d | 20 | |
yfkwok | 2:464c7e62d97d | 21 | // Set the random parameter for coin/block ratio |
yfkwok | 2:464c7e62d97d | 22 | _rand = r; |
yfkwok | 2:464c7e62d97d | 23 | |
yfkwok | 2:464c7e62d97d | 24 | // x position on screen - WIDTH is defined in N5110.h |
yfkwok | 2:464c7e62d97d | 25 | _p1x = 61; |
yfkwok | 2:464c7e62d97d | 26 | |
yfkwok | 2:464c7e62d97d | 27 | // initialize round counter |
yfkwok | 2:464c7e62d97d | 28 | _count = 0; |
yfkwok | 2:464c7e62d97d | 29 | _alt = 0; |
yfkwok | 2:464c7e62d97d | 30 | |
yfkwok | 2:464c7e62d97d | 31 | // puts paddles and ball in middle |
yfkwok | 2:464c7e62d97d | 32 | _p1.init(_p1x); |
yfkwok | 2:464c7e62d97d | 33 | _coin.init(_speed); |
yfkwok | 2:464c7e62d97d | 34 | _block.init(_speed); |
yfkwok | 2:464c7e62d97d | 35 | |
yfkwok | 2:464c7e62d97d | 36 | // 4 possibilities. 3 for coin, 1 for block. |
yfkwok | 2:464c7e62d97d | 37 | srand(time(NULL)); |
yfkwok | 2:464c7e62d97d | 38 | _type = rand() % 4; // randomise type. |
yfkwok | 2:464c7e62d97d | 39 | } |
yfkwok | 2:464c7e62d97d | 40 | |
yfkwok | 2:464c7e62d97d | 41 | void Game_one::render(N5110 &lcd, int cha) |
yfkwok | 2:464c7e62d97d | 42 | { |
yfkwok | 2:464c7e62d97d | 43 | lcd.clear(); |
yfkwok | 2:464c7e62d97d | 44 | draw(lcd, cha); |
yfkwok | 2:464c7e62d97d | 45 | lcd.refresh(); |
yfkwok | 2:464c7e62d97d | 46 | } |
yfkwok | 2:464c7e62d97d | 47 | |
yfkwok | 2:464c7e62d97d | 48 | void Game_one::read_input(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 49 | { |
yfkwok | 2:464c7e62d97d | 50 | _d = pad.get_direction(); |
yfkwok | 2:464c7e62d97d | 51 | _mag = pad.get_mag(); |
yfkwok | 2:464c7e62d97d | 52 | } |
yfkwok | 2:464c7e62d97d | 53 | |
yfkwok | 2:464c7e62d97d | 54 | void Game_one::draw(N5110 &lcd, int cha) |
yfkwok | 2:464c7e62d97d | 55 | { |
yfkwok | 2:464c7e62d97d | 56 | int alt = update_alt(); |
yfkwok | 2:464c7e62d97d | 57 | // draw player on lcd |
yfkwok | 2:464c7e62d97d | 58 | if(alt <= 1){_p1.draw(lcd, cha);} |
yfkwok | 2:464c7e62d97d | 59 | else {_p1.draw_alt(lcd, cha);} |
yfkwok | 2:464c7e62d97d | 60 | print_scores(lcd); |
yfkwok | 2:464c7e62d97d | 61 | // spawn coin |
yfkwok | 2:464c7e62d97d | 62 | if(_type < _rand) {_coin.draw(lcd);} |
yfkwok | 2:464c7e62d97d | 63 | else {_block.draw(lcd);} |
yfkwok | 2:464c7e62d97d | 64 | } |
yfkwok | 2:464c7e62d97d | 65 | |
yfkwok | 2:464c7e62d97d | 66 | void Game_one::update(Gamepad &pad, N5110 &lcd) |
yfkwok | 2:464c7e62d97d | 67 | { |
yfkwok | 2:464c7e62d97d | 68 | if(_type < _rand) { |
yfkwok | 2:464c7e62d97d | 69 | check_miss_coin(pad); |
yfkwok | 2:464c7e62d97d | 70 | _p1.update(_d,_mag); |
yfkwok | 2:464c7e62d97d | 71 | _coin.update(); |
yfkwok | 2:464c7e62d97d | 72 | check_player_collect(pad); |
yfkwok | 2:464c7e62d97d | 73 | } |
yfkwok | 2:464c7e62d97d | 74 | else { |
yfkwok | 2:464c7e62d97d | 75 | check_miss_block(pad); |
yfkwok | 2:464c7e62d97d | 76 | _p1.update(_d,_mag); |
yfkwok | 2:464c7e62d97d | 77 | _block.update(); |
yfkwok | 2:464c7e62d97d | 78 | check_player_collide(pad, lcd); |
yfkwok | 2:464c7e62d97d | 79 | } |
yfkwok | 2:464c7e62d97d | 80 | } |
yfkwok | 2:464c7e62d97d | 81 | |
yfkwok | 2:464c7e62d97d | 82 | void Game_one::check_player_collect(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 83 | { |
yfkwok | 2:464c7e62d97d | 84 | // read current coin attributes |
yfkwok | 2:464c7e62d97d | 85 | Vector2D coin_pos = _coin.get_pos(); |
yfkwok | 2:464c7e62d97d | 86 | int coin_speed = _coin.get_velocity(); |
yfkwok | 2:464c7e62d97d | 87 | |
yfkwok | 2:464c7e62d97d | 88 | // check p1 first |
yfkwok | 2:464c7e62d97d | 89 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 2:464c7e62d97d | 90 | |
yfkwok | 2:464c7e62d97d | 91 | // see if coin has hit the player by checking for overlaps |
yfkwok | 2:464c7e62d97d | 92 | if ( |
yfkwok | 2:464c7e62d97d | 93 | (coin_pos.y >= p1_pos.y) && //top |
yfkwok | 2:464c7e62d97d | 94 | (coin_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 2:464c7e62d97d | 95 | (coin_pos.x >= _p1x) && //left |
yfkwok | 2:464c7e62d97d | 96 | (coin_pos.x <= _p1x + 17) //right |
yfkwok | 2:464c7e62d97d | 97 | ) |
yfkwok | 2:464c7e62d97d | 98 | { |
yfkwok | 2:464c7e62d97d | 99 | // write new attributes |
yfkwok | 2:464c7e62d97d | 100 | _p1.add_score(); |
yfkwok | 2:464c7e62d97d | 101 | _coin.init(_speed); |
yfkwok | 2:464c7e62d97d | 102 | music.coin(pad); |
yfkwok | 2:464c7e62d97d | 103 | _count++; |
yfkwok | 2:464c7e62d97d | 104 | _type = rand() % 8; // randomise type |
yfkwok | 2:464c7e62d97d | 105 | } |
yfkwok | 2:464c7e62d97d | 106 | } |
yfkwok | 2:464c7e62d97d | 107 | |
yfkwok | 2:464c7e62d97d | 108 | void Game_one::check_player_collide(Gamepad &pad, N5110 &lcd) |
yfkwok | 2:464c7e62d97d | 109 | { |
yfkwok | 2:464c7e62d97d | 110 | // read current block attributes |
yfkwok | 2:464c7e62d97d | 111 | Vector2D block_pos = _block.get_pos(); |
yfkwok | 2:464c7e62d97d | 112 | int block_speed = _block.get_velocity(); |
yfkwok | 2:464c7e62d97d | 113 | |
yfkwok | 2:464c7e62d97d | 114 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 2:464c7e62d97d | 115 | |
yfkwok | 2:464c7e62d97d | 116 | // see if block has hit the player by checking for overlaps |
yfkwok | 2:464c7e62d97d | 117 | if ( |
yfkwok | 2:464c7e62d97d | 118 | (block_pos.y >= p1_pos.y + 2) && //top |
yfkwok | 2:464c7e62d97d | 119 | (block_pos.y <= p1_pos.y + 19) && //bottom |
yfkwok | 2:464c7e62d97d | 120 | (block_pos.x >= _p1x) && //left |
yfkwok | 2:464c7e62d97d | 121 | (block_pos.x <= _p1x + 17) //right |
yfkwok | 2:464c7e62d97d | 122 | ) |
yfkwok | 2:464c7e62d97d | 123 | { |
yfkwok | 2:464c7e62d97d | 124 | // write new attributes |
yfkwok | 2:464c7e62d97d | 125 | _block.init(_speed); |
yfkwok | 2:464c7e62d97d | 126 | gameover(lcd, pad); |
yfkwok | 2:464c7e62d97d | 127 | } |
yfkwok | 2:464c7e62d97d | 128 | } |
yfkwok | 2:464c7e62d97d | 129 | |
yfkwok | 2:464c7e62d97d | 130 | void Game_one::check_miss_coin(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 131 | { |
yfkwok | 2:464c7e62d97d | 132 | Vector2D coin_pos = _coin.get_pos(); |
yfkwok | 2:464c7e62d97d | 133 | // player has missed |
yfkwok | 2:464c7e62d97d | 134 | if (coin_pos.x > WIDTH) { |
yfkwok | 2:464c7e62d97d | 135 | _coin.init(_speed); |
yfkwok | 2:464c7e62d97d | 136 | pad.tone(750.0,0.1); |
yfkwok | 2:464c7e62d97d | 137 | _count++; |
yfkwok | 2:464c7e62d97d | 138 | wait(0.1); |
yfkwok | 2:464c7e62d97d | 139 | _type = rand() % 8; // randomise type |
yfkwok | 2:464c7e62d97d | 140 | } |
yfkwok | 2:464c7e62d97d | 141 | |
yfkwok | 2:464c7e62d97d | 142 | } |
yfkwok | 2:464c7e62d97d | 143 | |
yfkwok | 2:464c7e62d97d | 144 | void Game_one::check_miss_block(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 145 | { |
yfkwok | 2:464c7e62d97d | 146 | Vector2D block_pos = _block.get_pos(); |
yfkwok | 2:464c7e62d97d | 147 | // player has missed |
yfkwok | 2:464c7e62d97d | 148 | if (block_pos.x > WIDTH) { |
yfkwok | 2:464c7e62d97d | 149 | _block.init(_speed); |
yfkwok | 2:464c7e62d97d | 150 | pad.tone(750.0,0.1); |
yfkwok | 2:464c7e62d97d | 151 | wait(0.1); |
yfkwok | 2:464c7e62d97d | 152 | _type = rand() % 8; // randomise type |
yfkwok | 2:464c7e62d97d | 153 | } |
yfkwok | 2:464c7e62d97d | 154 | |
yfkwok | 2:464c7e62d97d | 155 | } |
yfkwok | 2:464c7e62d97d | 156 | |
yfkwok | 2:464c7e62d97d | 157 | int Game_one::print_scores(N5110 &lcd) |
yfkwok | 2:464c7e62d97d | 158 | { |
yfkwok | 2:464c7e62d97d | 159 | // get scores from paddles |
yfkwok | 2:464c7e62d97d | 160 | int p1_score = _p1.get_score(); |
yfkwok | 2:464c7e62d97d | 161 | int total = get_count(); |
yfkwok | 2:464c7e62d97d | 162 | |
yfkwok | 2:464c7e62d97d | 163 | // print to LCD i |
yfkwok | 2:464c7e62d97d | 164 | char buffer1[14]; |
yfkwok | 2:464c7e62d97d | 165 | sprintf(buffer1,"%2d / %2d",p1_score, total); |
yfkwok | 2:464c7e62d97d | 166 | lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
yfkwok | 2:464c7e62d97d | 167 | return p1_score; |
yfkwok | 2:464c7e62d97d | 168 | } |
yfkwok | 2:464c7e62d97d | 169 | |
yfkwok | 2:464c7e62d97d | 170 | void Game_one::set_count(int count) |
yfkwok | 2:464c7e62d97d | 171 | { |
yfkwok | 2:464c7e62d97d | 172 | _count = count; |
yfkwok | 2:464c7e62d97d | 173 | } |
yfkwok | 2:464c7e62d97d | 174 | |
yfkwok | 2:464c7e62d97d | 175 | int Game_one::get_count() |
yfkwok | 2:464c7e62d97d | 176 | { |
yfkwok | 2:464c7e62d97d | 177 | int count = _count; |
yfkwok | 2:464c7e62d97d | 178 | return count; |
yfkwok | 2:464c7e62d97d | 179 | } |
yfkwok | 2:464c7e62d97d | 180 | |
yfkwok | 2:464c7e62d97d | 181 | void Game_one::set_alt(int alt) |
yfkwok | 2:464c7e62d97d | 182 | { |
yfkwok | 2:464c7e62d97d | 183 | _alt = alt; |
yfkwok | 2:464c7e62d97d | 184 | } |
yfkwok | 2:464c7e62d97d | 185 | |
yfkwok | 2:464c7e62d97d | 186 | int Game_one::update_alt() |
yfkwok | 2:464c7e62d97d | 187 | { |
yfkwok | 2:464c7e62d97d | 188 | if(_alt < 4){_alt = _alt++;} |
yfkwok | 2:464c7e62d97d | 189 | else {_alt=0;} |
yfkwok | 2:464c7e62d97d | 190 | int alt = _alt; |
yfkwok | 2:464c7e62d97d | 191 | return alt; |
yfkwok | 2:464c7e62d97d | 192 | } |
yfkwok | 2:464c7e62d97d | 193 | |
yfkwok | 2:464c7e62d97d | 194 | void Game_one::gameover(N5110 &lcd, Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 195 | { |
yfkwok | 2:464c7e62d97d | 196 | while(pad.check_event(Gamepad::B_PRESSED) == false) { |
yfkwok | 2:464c7e62d97d | 197 | lcd.clear(); |
yfkwok | 2:464c7e62d97d | 198 | lcd.printString("You rushed ",0,0); |
yfkwok | 2:464c7e62d97d | 199 | lcd.printString("into a block!",0,1); |
yfkwok | 2:464c7e62d97d | 200 | lcd.printString("You are late",0,2); |
yfkwok | 2:464c7e62d97d | 201 | lcd.printString("for class!",0,3); |
yfkwok | 2:464c7e62d97d | 202 | lcd.printString("Press B",0,5); |
yfkwok | 2:464c7e62d97d | 203 | lcd.refresh(); |
yfkwok | 2:464c7e62d97d | 204 | wait(0.1); |
yfkwok | 2:464c7e62d97d | 205 | } |
yfkwok | 2:464c7e62d97d | 206 | _count = 10; |
yfkwok | 2:464c7e62d97d | 207 | } |