![](/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@11:5a6b9dffd78a, 2019-04-14 (annotated)
- Committer:
- yfkwok
- Date:
- Sun Apr 14 15:56:58 2019 +0000
- Revision:
- 11:5a6b9dffd78a
- Parent:
- 5:bc0691d02fd5
- Child:
- 17:5d8ff39a0e49
14/4/2019 - Integer type of all sprites changed from "static int' to "int"
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 | 4:5bc9c4363d31 | 13 | //Enumerate instruction menu from page 1 - 3 |
yfkwok | 4:5bc9c4363d31 | 14 | enum Page { |
yfkwok | 4:5bc9c4363d31 | 15 | PAGE_1, PAGE_2, PAGE_3 |
yfkwok | 4:5bc9c4363d31 | 16 | }; |
yfkwok | 4:5bc9c4363d31 | 17 | |
yfkwok | 2:464c7e62d97d | 18 | void Game_one::init(int speed, int cha, int r) |
yfkwok | 2:464c7e62d97d | 19 | { |
yfkwok | 2:464c7e62d97d | 20 | // Set the speed of the objects |
yfkwok | 2:464c7e62d97d | 21 | _speed = speed; |
yfkwok | 2:464c7e62d97d | 22 | |
yfkwok | 2:464c7e62d97d | 23 | // Set the character being drawn |
yfkwok | 2:464c7e62d97d | 24 | _cha = cha; |
yfkwok | 2:464c7e62d97d | 25 | |
yfkwok | 2:464c7e62d97d | 26 | // Set the random parameter for coin/block ratio |
yfkwok | 2:464c7e62d97d | 27 | _rand = r; |
yfkwok | 2:464c7e62d97d | 28 | |
yfkwok | 2:464c7e62d97d | 29 | // x position on screen - WIDTH is defined in N5110.h |
yfkwok | 2:464c7e62d97d | 30 | _p1x = 61; |
yfkwok | 2:464c7e62d97d | 31 | |
yfkwok | 2:464c7e62d97d | 32 | // initialize round counter |
yfkwok | 2:464c7e62d97d | 33 | _count = 0; |
yfkwok | 2:464c7e62d97d | 34 | _alt = 0; |
yfkwok | 2:464c7e62d97d | 35 | |
yfkwok | 2:464c7e62d97d | 36 | // puts paddles and ball in middle |
yfkwok | 2:464c7e62d97d | 37 | _p1.init(_p1x); |
yfkwok | 2:464c7e62d97d | 38 | _coin.init(_speed); |
yfkwok | 2:464c7e62d97d | 39 | _block.init(_speed); |
yfkwok | 2:464c7e62d97d | 40 | |
yfkwok | 2:464c7e62d97d | 41 | // 4 possibilities. 3 for coin, 1 for block. |
yfkwok | 2:464c7e62d97d | 42 | srand(time(NULL)); |
yfkwok | 2:464c7e62d97d | 43 | _type = rand() % 4; // randomise type. |
yfkwok | 2:464c7e62d97d | 44 | } |
yfkwok | 2:464c7e62d97d | 45 | |
yfkwok | 2:464c7e62d97d | 46 | void Game_one::render(N5110 &lcd, int cha) |
yfkwok | 2:464c7e62d97d | 47 | { |
yfkwok | 2:464c7e62d97d | 48 | lcd.clear(); |
yfkwok | 2:464c7e62d97d | 49 | draw(lcd, cha); |
yfkwok | 2:464c7e62d97d | 50 | lcd.refresh(); |
yfkwok | 2:464c7e62d97d | 51 | } |
yfkwok | 2:464c7e62d97d | 52 | |
yfkwok | 2:464c7e62d97d | 53 | void Game_one::read_input(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 54 | { |
yfkwok | 2:464c7e62d97d | 55 | _d = pad.get_direction(); |
yfkwok | 2:464c7e62d97d | 56 | _mag = pad.get_mag(); |
yfkwok | 2:464c7e62d97d | 57 | } |
yfkwok | 2:464c7e62d97d | 58 | |
yfkwok | 2:464c7e62d97d | 59 | void Game_one::draw(N5110 &lcd, int cha) |
yfkwok | 2:464c7e62d97d | 60 | { |
yfkwok | 2:464c7e62d97d | 61 | int alt = update_alt(); |
yfkwok | 2:464c7e62d97d | 62 | // draw player on lcd |
yfkwok | 2:464c7e62d97d | 63 | if(alt <= 1){_p1.draw(lcd, cha);} |
yfkwok | 2:464c7e62d97d | 64 | else {_p1.draw_alt(lcd, cha);} |
yfkwok | 2:464c7e62d97d | 65 | print_scores(lcd); |
yfkwok | 2:464c7e62d97d | 66 | // spawn coin |
yfkwok | 2:464c7e62d97d | 67 | if(_type < _rand) {_coin.draw(lcd);} |
yfkwok | 2:464c7e62d97d | 68 | else {_block.draw(lcd);} |
yfkwok | 2:464c7e62d97d | 69 | } |
yfkwok | 2:464c7e62d97d | 70 | |
yfkwok | 2:464c7e62d97d | 71 | void Game_one::update(Gamepad &pad, N5110 &lcd) |
yfkwok | 2:464c7e62d97d | 72 | { |
yfkwok | 2:464c7e62d97d | 73 | if(_type < _rand) { |
yfkwok | 2:464c7e62d97d | 74 | check_miss_coin(pad); |
yfkwok | 2:464c7e62d97d | 75 | _p1.update(_d,_mag); |
yfkwok | 2:464c7e62d97d | 76 | _coin.update(); |
yfkwok | 2:464c7e62d97d | 77 | check_player_collect(pad); |
yfkwok | 2:464c7e62d97d | 78 | } |
yfkwok | 2:464c7e62d97d | 79 | else { |
yfkwok | 2:464c7e62d97d | 80 | check_miss_block(pad); |
yfkwok | 2:464c7e62d97d | 81 | _p1.update(_d,_mag); |
yfkwok | 2:464c7e62d97d | 82 | _block.update(); |
yfkwok | 2:464c7e62d97d | 83 | check_player_collide(pad, lcd); |
yfkwok | 2:464c7e62d97d | 84 | } |
yfkwok | 2:464c7e62d97d | 85 | } |
yfkwok | 2:464c7e62d97d | 86 | |
yfkwok | 2:464c7e62d97d | 87 | void Game_one::check_player_collect(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 88 | { |
yfkwok | 2:464c7e62d97d | 89 | // read current coin attributes |
yfkwok | 2:464c7e62d97d | 90 | Vector2D coin_pos = _coin.get_pos(); |
yfkwok | 2:464c7e62d97d | 91 | int coin_speed = _coin.get_velocity(); |
yfkwok | 2:464c7e62d97d | 92 | |
yfkwok | 2:464c7e62d97d | 93 | // check p1 first |
yfkwok | 2:464c7e62d97d | 94 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 2:464c7e62d97d | 95 | |
yfkwok | 2:464c7e62d97d | 96 | // see if coin has hit the player by checking for overlaps |
yfkwok | 2:464c7e62d97d | 97 | if ( |
yfkwok | 2:464c7e62d97d | 98 | (coin_pos.y >= p1_pos.y) && //top |
yfkwok | 2:464c7e62d97d | 99 | (coin_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 2:464c7e62d97d | 100 | (coin_pos.x >= _p1x) && //left |
yfkwok | 2:464c7e62d97d | 101 | (coin_pos.x <= _p1x + 17) //right |
yfkwok | 2:464c7e62d97d | 102 | ) |
yfkwok | 2:464c7e62d97d | 103 | { |
yfkwok | 2:464c7e62d97d | 104 | // write new attributes |
yfkwok | 2:464c7e62d97d | 105 | _p1.add_score(); |
yfkwok | 2:464c7e62d97d | 106 | _coin.init(_speed); |
yfkwok | 2:464c7e62d97d | 107 | music.coin(pad); |
yfkwok | 2:464c7e62d97d | 108 | _count++; |
yfkwok | 2:464c7e62d97d | 109 | _type = rand() % 8; // randomise type |
yfkwok | 2:464c7e62d97d | 110 | } |
yfkwok | 2:464c7e62d97d | 111 | } |
yfkwok | 2:464c7e62d97d | 112 | |
yfkwok | 2:464c7e62d97d | 113 | void Game_one::check_player_collide(Gamepad &pad, N5110 &lcd) |
yfkwok | 2:464c7e62d97d | 114 | { |
yfkwok | 2:464c7e62d97d | 115 | // read current block attributes |
yfkwok | 2:464c7e62d97d | 116 | Vector2D block_pos = _block.get_pos(); |
yfkwok | 2:464c7e62d97d | 117 | int block_speed = _block.get_velocity(); |
yfkwok | 2:464c7e62d97d | 118 | |
yfkwok | 2:464c7e62d97d | 119 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 2:464c7e62d97d | 120 | |
yfkwok | 2:464c7e62d97d | 121 | // see if block has hit the player by checking for overlaps |
yfkwok | 2:464c7e62d97d | 122 | if ( |
yfkwok | 5:bc0691d02fd5 | 123 | (block_pos.y >= p1_pos.y - 6) && //top |
yfkwok | 5:bc0691d02fd5 | 124 | (block_pos.y <= p1_pos.y + 14) && //bottom |
yfkwok | 2:464c7e62d97d | 125 | (block_pos.x >= _p1x) && //left |
yfkwok | 2:464c7e62d97d | 126 | (block_pos.x <= _p1x + 17) //right |
yfkwok | 2:464c7e62d97d | 127 | ) |
yfkwok | 2:464c7e62d97d | 128 | { |
yfkwok | 2:464c7e62d97d | 129 | // write new attributes |
yfkwok | 2:464c7e62d97d | 130 | _block.init(_speed); |
yfkwok | 2:464c7e62d97d | 131 | gameover(lcd, pad); |
yfkwok | 2:464c7e62d97d | 132 | } |
yfkwok | 2:464c7e62d97d | 133 | } |
yfkwok | 2:464c7e62d97d | 134 | |
yfkwok | 2:464c7e62d97d | 135 | void Game_one::check_miss_coin(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 136 | { |
yfkwok | 2:464c7e62d97d | 137 | Vector2D coin_pos = _coin.get_pos(); |
yfkwok | 2:464c7e62d97d | 138 | // player has missed |
yfkwok | 2:464c7e62d97d | 139 | if (coin_pos.x > WIDTH) { |
yfkwok | 2:464c7e62d97d | 140 | _coin.init(_speed); |
yfkwok | 2:464c7e62d97d | 141 | pad.tone(750.0,0.1); |
yfkwok | 2:464c7e62d97d | 142 | _count++; |
yfkwok | 2:464c7e62d97d | 143 | wait(0.1); |
yfkwok | 2:464c7e62d97d | 144 | _type = rand() % 8; // randomise type |
yfkwok | 2:464c7e62d97d | 145 | } |
yfkwok | 2:464c7e62d97d | 146 | |
yfkwok | 2:464c7e62d97d | 147 | } |
yfkwok | 2:464c7e62d97d | 148 | |
yfkwok | 2:464c7e62d97d | 149 | void Game_one::check_miss_block(Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 150 | { |
yfkwok | 2:464c7e62d97d | 151 | Vector2D block_pos = _block.get_pos(); |
yfkwok | 2:464c7e62d97d | 152 | // player has missed |
yfkwok | 2:464c7e62d97d | 153 | if (block_pos.x > WIDTH) { |
yfkwok | 2:464c7e62d97d | 154 | _block.init(_speed); |
yfkwok | 2:464c7e62d97d | 155 | pad.tone(750.0,0.1); |
yfkwok | 2:464c7e62d97d | 156 | wait(0.1); |
yfkwok | 2:464c7e62d97d | 157 | _type = rand() % 8; // randomise type |
yfkwok | 2:464c7e62d97d | 158 | } |
yfkwok | 2:464c7e62d97d | 159 | |
yfkwok | 2:464c7e62d97d | 160 | } |
yfkwok | 2:464c7e62d97d | 161 | |
yfkwok | 2:464c7e62d97d | 162 | int Game_one::print_scores(N5110 &lcd) |
yfkwok | 2:464c7e62d97d | 163 | { |
yfkwok | 2:464c7e62d97d | 164 | // get scores from paddles |
yfkwok | 2:464c7e62d97d | 165 | int p1_score = _p1.get_score(); |
yfkwok | 2:464c7e62d97d | 166 | int total = get_count(); |
yfkwok | 2:464c7e62d97d | 167 | |
yfkwok | 2:464c7e62d97d | 168 | // print to LCD i |
yfkwok | 2:464c7e62d97d | 169 | char buffer1[14]; |
yfkwok | 2:464c7e62d97d | 170 | sprintf(buffer1,"%2d / %2d",p1_score, total); |
yfkwok | 2:464c7e62d97d | 171 | lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
yfkwok | 2:464c7e62d97d | 172 | return p1_score; |
yfkwok | 2:464c7e62d97d | 173 | } |
yfkwok | 2:464c7e62d97d | 174 | |
yfkwok | 2:464c7e62d97d | 175 | void Game_one::set_count(int count) |
yfkwok | 2:464c7e62d97d | 176 | { |
yfkwok | 2:464c7e62d97d | 177 | _count = count; |
yfkwok | 2:464c7e62d97d | 178 | } |
yfkwok | 2:464c7e62d97d | 179 | |
yfkwok | 2:464c7e62d97d | 180 | int Game_one::get_count() |
yfkwok | 2:464c7e62d97d | 181 | { |
yfkwok | 2:464c7e62d97d | 182 | int count = _count; |
yfkwok | 2:464c7e62d97d | 183 | return count; |
yfkwok | 2:464c7e62d97d | 184 | } |
yfkwok | 2:464c7e62d97d | 185 | |
yfkwok | 2:464c7e62d97d | 186 | void Game_one::set_alt(int alt) |
yfkwok | 2:464c7e62d97d | 187 | { |
yfkwok | 2:464c7e62d97d | 188 | _alt = alt; |
yfkwok | 2:464c7e62d97d | 189 | } |
yfkwok | 2:464c7e62d97d | 190 | |
yfkwok | 2:464c7e62d97d | 191 | int Game_one::update_alt() |
yfkwok | 2:464c7e62d97d | 192 | { |
yfkwok | 2:464c7e62d97d | 193 | if(_alt < 4){_alt = _alt++;} |
yfkwok | 2:464c7e62d97d | 194 | else {_alt=0;} |
yfkwok | 2:464c7e62d97d | 195 | int alt = _alt; |
yfkwok | 2:464c7e62d97d | 196 | return alt; |
yfkwok | 2:464c7e62d97d | 197 | } |
yfkwok | 2:464c7e62d97d | 198 | |
yfkwok | 2:464c7e62d97d | 199 | void Game_one::gameover(N5110 &lcd, Gamepad &pad) |
yfkwok | 2:464c7e62d97d | 200 | { |
yfkwok | 2:464c7e62d97d | 201 | while(pad.check_event(Gamepad::B_PRESSED) == false) { |
yfkwok | 2:464c7e62d97d | 202 | lcd.clear(); |
yfkwok | 2:464c7e62d97d | 203 | lcd.printString("You rushed ",0,0); |
yfkwok | 2:464c7e62d97d | 204 | lcd.printString("into a block!",0,1); |
yfkwok | 2:464c7e62d97d | 205 | lcd.printString("You are late",0,2); |
yfkwok | 2:464c7e62d97d | 206 | lcd.printString("for class!",0,3); |
yfkwok | 2:464c7e62d97d | 207 | lcd.printString("Press B",0,5); |
yfkwok | 2:464c7e62d97d | 208 | lcd.refresh(); |
yfkwok | 2:464c7e62d97d | 209 | wait(0.1); |
yfkwok | 2:464c7e62d97d | 210 | } |
yfkwok | 2:464c7e62d97d | 211 | _count = 10; |
yfkwok | 4:5bc9c4363d31 | 212 | } |
yfkwok | 4:5bc9c4363d31 | 213 | |
yfkwok | 4:5bc9c4363d31 | 214 | void Game_one::intro(Gamepad &pad, N5110 &lcd) |
yfkwok | 4:5bc9c4363d31 | 215 | { |
yfkwok | 4:5bc9c4363d31 | 216 | Page currentPage = PAGE_1; |
yfkwok | 4:5bc9c4363d31 | 217 | int fps = 8.0; |
yfkwok | 11:5a6b9dffd78a | 218 | int instruct_data[4][7] = { |
yfkwok | 11:5a6b9dffd78a | 219 | {1,1,1,1,1,1,1}, |
yfkwok | 11:5a6b9dffd78a | 220 | {0,1,1,1,1,1,0}, |
yfkwok | 11:5a6b9dffd78a | 221 | {0,0,1,1,1,0,0}, |
yfkwok | 11:5a6b9dffd78a | 222 | {0,0,0,1,0,0,0}, |
yfkwok | 4:5bc9c4363d31 | 223 | }; |
yfkwok | 4:5bc9c4363d31 | 224 | |
yfkwok | 4:5bc9c4363d31 | 225 | while(pad.check_event(Gamepad::A_PRESSED) == false){ |
yfkwok | 4:5bc9c4363d31 | 226 | switch (currentPage) { |
yfkwok | 4:5bc9c4363d31 | 227 | case PAGE_1: |
yfkwok | 4:5bc9c4363d31 | 228 | lcd.clear(); |
yfkwok | 4:5bc9c4363d31 | 229 | lcd.printString("Squirtle",0,0); |
yfkwok | 4:5bc9c4363d31 | 230 | lcd.printString("is rushing",0,1); |
yfkwok | 4:5bc9c4363d31 | 231 | lcd.printString("to his lecture",0,2); |
yfkwok | 4:5bc9c4363d31 | 232 | lcd.printString("Collect coins",0,4); |
yfkwok | 11:5a6b9dffd78a | 233 | lcd.drawSprite(39, 44, 4, 7, (int *)instruct_data); |
yfkwok | 4:5bc9c4363d31 | 234 | lcd.refresh(); |
yfkwok | 4:5bc9c4363d31 | 235 | if(pad.get_direction() == S){currentPage = PAGE_2;} |
yfkwok | 4:5bc9c4363d31 | 236 | wait(1.0f/fps); |
yfkwok | 4:5bc9c4363d31 | 237 | break; |
yfkwok | 4:5bc9c4363d31 | 238 | case PAGE_2: |
yfkwok | 4:5bc9c4363d31 | 239 | lcd.clear(); |
yfkwok | 4:5bc9c4363d31 | 240 | lcd.printString("so he can have",0,0); |
yfkwok | 4:5bc9c4363d31 | 241 | lcd.printString("lunch and ",0,1); |
yfkwok | 4:5bc9c4363d31 | 242 | lcd.printString("reach the",0,2); |
yfkwok | 4:5bc9c4363d31 | 243 | lcd.printString("classroom in",0,3); |
yfkwok | 4:5bc9c4363d31 | 244 | lcd.printString("time by ",0,4); |
yfkwok | 11:5a6b9dffd78a | 245 | lcd.drawSprite(39, 44, 4, 7, (int *)instruct_data); |
yfkwok | 4:5bc9c4363d31 | 246 | lcd.refresh(); |
yfkwok | 4:5bc9c4363d31 | 247 | if(pad.get_direction() == S){currentPage = PAGE_3;} |
yfkwok | 4:5bc9c4363d31 | 248 | wait(1.0f/fps); |
yfkwok | 4:5bc9c4363d31 | 249 | break; |
yfkwok | 4:5bc9c4363d31 | 250 | case PAGE_3: |
yfkwok | 4:5bc9c4363d31 | 251 | lcd.clear(); |
yfkwok | 4:5bc9c4363d31 | 252 | lcd.printString("avoiding the",0,0); |
yfkwok | 4:5bc9c4363d31 | 253 | lcd.printString("blocks",0,1); |
yfkwok | 4:5bc9c4363d31 | 254 | lcd.printString("Press A",0,5); |
yfkwok | 4:5bc9c4363d31 | 255 | lcd.refresh(); |
yfkwok | 4:5bc9c4363d31 | 256 | wait(1.0f/fps); |
yfkwok | 4:5bc9c4363d31 | 257 | |
yfkwok | 4:5bc9c4363d31 | 258 | } |
yfkwok | 4:5bc9c4363d31 | 259 | } |
yfkwok | 5:bc0691d02fd5 | 260 | } |
yfkwok | 5:bc0691d02fd5 | 261 |