![](/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_two/Game_two.cpp@14:abe64fe0b6a5, 2019-04-18 (annotated)
- Committer:
- yfkwok
- Date:
- Thu Apr 18 04:53:04 2019 +0000
- Revision:
- 14:abe64fe0b6a5
- Parent:
- 13:02002658e718
- Child:
- 15:a65444eb1194
18/04/2019 - Mini Game 2 Implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yfkwok | 13:02002658e718 | 1 | #include "Game_two.h" |
yfkwok | 13:02002658e718 | 2 | |
yfkwok | 13:02002658e718 | 3 | Game_two::Game_two() |
yfkwok | 13:02002658e718 | 4 | { |
yfkwok | 13:02002658e718 | 5 | |
yfkwok | 13:02002658e718 | 6 | } |
yfkwok | 13:02002658e718 | 7 | |
yfkwok | 13:02002658e718 | 8 | Game_two::~Game_two() |
yfkwok | 13:02002658e718 | 9 | { |
yfkwok | 13:02002658e718 | 10 | |
yfkwok | 13:02002658e718 | 11 | } |
yfkwok | 13:02002658e718 | 12 | |
yfkwok | 13:02002658e718 | 13 | //Enumerate instruction menu from page 1 - 3 |
yfkwok | 13:02002658e718 | 14 | enum Page { |
yfkwok | 13:02002658e718 | 15 | PAGE_1, PAGE_2, PAGE_3 |
yfkwok | 13:02002658e718 | 16 | }; |
yfkwok | 13:02002658e718 | 17 | |
yfkwok | 13:02002658e718 | 18 | void Game_two::init(int speed, int cha, int r) |
yfkwok | 13:02002658e718 | 19 | { |
yfkwok | 13:02002658e718 | 20 | // Set the speed of the objects |
yfkwok | 13:02002658e718 | 21 | _speed = speed; |
yfkwok | 13:02002658e718 | 22 | |
yfkwok | 13:02002658e718 | 23 | // Set the character being drawn |
yfkwok | 13:02002658e718 | 24 | _cha = cha; |
yfkwok | 13:02002658e718 | 25 | |
yfkwok | 13:02002658e718 | 26 | // Set the random parameter for coin/block ratio |
yfkwok | 13:02002658e718 | 27 | _rand = r; |
yfkwok | 13:02002658e718 | 28 | |
yfkwok | 13:02002658e718 | 29 | // initialize round counter |
yfkwok | 13:02002658e718 | 30 | _count = 0; |
yfkwok | 13:02002658e718 | 31 | _alt = 0; |
yfkwok | 14:abe64fe0b6a5 | 32 | |
yfkwok | 14:abe64fe0b6a5 | 33 | // Game initiation |
yfkwok | 14:abe64fe0b6a5 | 34 | _p1.init(); |
yfkwok | 14:abe64fe0b6a5 | 35 | |
yfkwok | 14:abe64fe0b6a5 | 36 | // 4 possibilities. 3 for coin, 1 for block. |
yfkwok | 14:abe64fe0b6a5 | 37 | srand(time(NULL)); |
yfkwok | 14:abe64fe0b6a5 | 38 | _type = rand() % 4; // randomise type. |
yfkwok | 14:abe64fe0b6a5 | 39 | _so = rand() % 3; |
yfkwok | 13:02002658e718 | 40 | |
yfkwok | 13:02002658e718 | 41 | } |
yfkwok | 13:02002658e718 | 42 | |
yfkwok | 14:abe64fe0b6a5 | 43 | void Game_two::render(N5110 &lcd, int cha) |
yfkwok | 13:02002658e718 | 44 | { |
yfkwok | 13:02002658e718 | 45 | lcd.clear(); |
yfkwok | 13:02002658e718 | 46 | draw(lcd, cha); |
yfkwok | 13:02002658e718 | 47 | lcd.refresh(); |
yfkwok | 13:02002658e718 | 48 | } |
yfkwok | 13:02002658e718 | 49 | |
yfkwok | 14:abe64fe0b6a5 | 50 | void Game_two::read_input(FXOS8700CQ &device) |
yfkwok | 13:02002658e718 | 51 | { |
yfkwok | 14:abe64fe0b6a5 | 52 | _d = device.get_direction(); |
yfkwok | 14:abe64fe0b6a5 | 53 | _mag = device.get_mag(); |
yfkwok | 13:02002658e718 | 54 | } |
yfkwok | 13:02002658e718 | 55 | |
yfkwok | 13:02002658e718 | 56 | void Game_two::draw(N5110 &lcd, int cha) |
yfkwok | 13:02002658e718 | 57 | { |
yfkwok | 13:02002658e718 | 58 | int alt = update_alt(); |
yfkwok | 13:02002658e718 | 59 | // draw player on lcd |
yfkwok | 13:02002658e718 | 60 | if(alt <= 1){_p1.draw(lcd, cha);} |
yfkwok | 13:02002658e718 | 61 | else {_p1.draw_alt(lcd, cha);} |
yfkwok | 13:02002658e718 | 62 | // spawn coin |
yfkwok | 14:abe64fe0b6a5 | 63 | if(_type < _rand) { |
yfkwok | 14:abe64fe0b6a5 | 64 | if (_so == 0) {_insta.draw(lcd);} |
yfkwok | 14:abe64fe0b6a5 | 65 | else if (_so == 1) {_face.draw(lcd);} |
yfkwok | 14:abe64fe0b6a5 | 66 | else if (_so == 2) {_twitt.draw(lcd);} |
yfkwok | 14:abe64fe0b6a5 | 67 | } |
yfkwok | 14:abe64fe0b6a5 | 68 | else {_yt.draw(lcd);} |
yfkwok | 13:02002658e718 | 69 | } |
yfkwok | 13:02002658e718 | 70 | |
yfkwok | 14:abe64fe0b6a5 | 71 | void Game_two::update(Gamepad &pad, N5110 &lcd, int cha) |
yfkwok | 13:02002658e718 | 72 | { |
yfkwok | 14:abe64fe0b6a5 | 73 | if (_type < _rand) { |
yfkwok | 14:abe64fe0b6a5 | 74 | if (_so == 0){ |
yfkwok | 14:abe64fe0b6a5 | 75 | check_miss_insta(pad); |
yfkwok | 14:abe64fe0b6a5 | 76 | _p1.update(_d,_mag); |
yfkwok | 14:abe64fe0b6a5 | 77 | _insta.update(); |
yfkwok | 14:abe64fe0b6a5 | 78 | check_insta_collide(pad, lcd, cha); |
yfkwok | 14:abe64fe0b6a5 | 79 | } else if (_so == 1){ |
yfkwok | 14:abe64fe0b6a5 | 80 | check_miss_face(pad); |
yfkwok | 14:abe64fe0b6a5 | 81 | _p1.update(_d,_mag); |
yfkwok | 14:abe64fe0b6a5 | 82 | _face.update(); |
yfkwok | 14:abe64fe0b6a5 | 83 | check_face_collide(pad, lcd, cha); |
yfkwok | 14:abe64fe0b6a5 | 84 | } else if (_so == 2){ |
yfkwok | 14:abe64fe0b6a5 | 85 | check_miss_twitt(pad); |
yfkwok | 14:abe64fe0b6a5 | 86 | _p1.update(_d,_mag); |
yfkwok | 14:abe64fe0b6a5 | 87 | _twitt.update(); |
yfkwok | 14:abe64fe0b6a5 | 88 | check_twitt_collide(pad, lcd, cha); |
yfkwok | 14:abe64fe0b6a5 | 89 | } |
yfkwok | 13:02002658e718 | 90 | } |
yfkwok | 13:02002658e718 | 91 | else { |
yfkwok | 14:abe64fe0b6a5 | 92 | check_miss_yt(pad); |
yfkwok | 13:02002658e718 | 93 | _p1.update(_d,_mag); |
yfkwok | 14:abe64fe0b6a5 | 94 | _yt.update(); |
yfkwok | 14:abe64fe0b6a5 | 95 | check_yt_collide(pad, lcd, cha); |
yfkwok | 13:02002658e718 | 96 | } |
yfkwok | 13:02002658e718 | 97 | } |
yfkwok | 13:02002658e718 | 98 | |
yfkwok | 14:abe64fe0b6a5 | 99 | void Game_two::check_insta_collide(Gamepad &pad, N5110 &lcd, int cha) |
yfkwok | 13:02002658e718 | 100 | { |
yfkwok | 13:02002658e718 | 101 | // read current coin attributes |
yfkwok | 14:abe64fe0b6a5 | 102 | Vector2D insta_pos = _insta.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 103 | Vector2D insta_speed = _insta.get_velocity(); |
yfkwok | 14:abe64fe0b6a5 | 104 | |
yfkwok | 14:abe64fe0b6a5 | 105 | // check p1 first |
yfkwok | 14:abe64fe0b6a5 | 106 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 107 | |
yfkwok | 14:abe64fe0b6a5 | 108 | // see if coin has hit the player by checking for overlaps |
yfkwok | 14:abe64fe0b6a5 | 109 | if (cha == 1){ |
yfkwok | 14:abe64fe0b6a5 | 110 | if ( |
yfkwok | 14:abe64fe0b6a5 | 111 | (insta_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 112 | (insta_pos.y <= p1_pos.y + 18) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 113 | (insta_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 114 | (insta_pos.x <= p1_pos.x + 20) //right |
yfkwok | 14:abe64fe0b6a5 | 115 | ) |
yfkwok | 14:abe64fe0b6a5 | 116 | { |
yfkwok | 14:abe64fe0b6a5 | 117 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 118 | _insta.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 119 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 120 | } |
yfkwok | 14:abe64fe0b6a5 | 121 | } |
yfkwok | 14:abe64fe0b6a5 | 122 | if (cha == 2){ |
yfkwok | 14:abe64fe0b6a5 | 123 | if ( |
yfkwok | 14:abe64fe0b6a5 | 124 | (insta_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 125 | (insta_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 126 | (insta_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 127 | (insta_pos.x <= p1_pos.x + 21) //right |
yfkwok | 14:abe64fe0b6a5 | 128 | ) |
yfkwok | 14:abe64fe0b6a5 | 129 | { |
yfkwok | 14:abe64fe0b6a5 | 130 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 131 | _insta.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 132 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 133 | } |
yfkwok | 14:abe64fe0b6a5 | 134 | } |
yfkwok | 14:abe64fe0b6a5 | 135 | if (cha == 3){ |
yfkwok | 14:abe64fe0b6a5 | 136 | if ( |
yfkwok | 14:abe64fe0b6a5 | 137 | (insta_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 138 | (insta_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 139 | (insta_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 140 | (insta_pos.x <= p1_pos.x + 29) //right |
yfkwok | 14:abe64fe0b6a5 | 141 | ) |
yfkwok | 14:abe64fe0b6a5 | 142 | { |
yfkwok | 14:abe64fe0b6a5 | 143 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 144 | _insta.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 145 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 146 | } |
yfkwok | 14:abe64fe0b6a5 | 147 | } |
yfkwok | 14:abe64fe0b6a5 | 148 | } |
yfkwok | 14:abe64fe0b6a5 | 149 | |
yfkwok | 14:abe64fe0b6a5 | 150 | void Game_two::check_miss_insta(Gamepad &pad) |
yfkwok | 14:abe64fe0b6a5 | 151 | { |
yfkwok | 14:abe64fe0b6a5 | 152 | Vector2D insta_pos = _insta.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 153 | // player has missed |
yfkwok | 14:abe64fe0b6a5 | 154 | if ((insta_pos.x > 84) || (insta_pos.x < -15) || (insta_pos.y < -15) || (insta_pos.y > 50)) { |
yfkwok | 14:abe64fe0b6a5 | 155 | _insta.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 156 | pad.tone(750.0,0.1); |
yfkwok | 14:abe64fe0b6a5 | 157 | _count++; |
yfkwok | 14:abe64fe0b6a5 | 158 | wait(0.1); |
yfkwok | 14:abe64fe0b6a5 | 159 | _type = rand() % 8; // randomise type |
yfkwok | 14:abe64fe0b6a5 | 160 | } |
yfkwok | 14:abe64fe0b6a5 | 161 | } |
yfkwok | 14:abe64fe0b6a5 | 162 | |
yfkwok | 14:abe64fe0b6a5 | 163 | void Game_two::check_face_collide(Gamepad &pad, N5110 &lcd, int cha) |
yfkwok | 14:abe64fe0b6a5 | 164 | { |
yfkwok | 14:abe64fe0b6a5 | 165 | // read current coin attributes |
yfkwok | 14:abe64fe0b6a5 | 166 | Vector2D face_pos = _face.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 167 | Vector2D face_speed = _face.get_velocity(); |
yfkwok | 13:02002658e718 | 168 | |
yfkwok | 13:02002658e718 | 169 | // check p1 first |
yfkwok | 13:02002658e718 | 170 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 13:02002658e718 | 171 | |
yfkwok | 13:02002658e718 | 172 | // see if coin has hit the player by checking for overlaps |
yfkwok | 14:abe64fe0b6a5 | 173 | if (cha == 1){ |
yfkwok | 14:abe64fe0b6a5 | 174 | if ( |
yfkwok | 14:abe64fe0b6a5 | 175 | (face_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 176 | (face_pos.y <= p1_pos.y + 18) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 177 | (face_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 178 | (face_pos.x <= p1_pos.x + 20) //right |
yfkwok | 14:abe64fe0b6a5 | 179 | ) |
yfkwok | 14:abe64fe0b6a5 | 180 | { |
yfkwok | 14:abe64fe0b6a5 | 181 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 182 | _face.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 183 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 184 | } |
yfkwok | 14:abe64fe0b6a5 | 185 | } |
yfkwok | 14:abe64fe0b6a5 | 186 | if (cha == 2){ |
yfkwok | 14:abe64fe0b6a5 | 187 | if ( |
yfkwok | 14:abe64fe0b6a5 | 188 | (face_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 189 | (face_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 190 | (face_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 191 | (face_pos.x <= p1_pos.x + 21) //right |
yfkwok | 14:abe64fe0b6a5 | 192 | ) |
yfkwok | 14:abe64fe0b6a5 | 193 | { |
yfkwok | 14:abe64fe0b6a5 | 194 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 195 | _face.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 196 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 197 | } |
yfkwok | 14:abe64fe0b6a5 | 198 | } |
yfkwok | 14:abe64fe0b6a5 | 199 | if (cha == 3){ |
yfkwok | 14:abe64fe0b6a5 | 200 | if ( |
yfkwok | 14:abe64fe0b6a5 | 201 | (face_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 202 | (face_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 203 | (face_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 204 | (face_pos.x <= p1_pos.x + 29) //right |
yfkwok | 14:abe64fe0b6a5 | 205 | ) |
yfkwok | 14:abe64fe0b6a5 | 206 | { |
yfkwok | 14:abe64fe0b6a5 | 207 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 208 | _face.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 209 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 210 | } |
yfkwok | 13:02002658e718 | 211 | } |
yfkwok | 13:02002658e718 | 212 | } |
yfkwok | 13:02002658e718 | 213 | |
yfkwok | 14:abe64fe0b6a5 | 214 | void Game_two::check_miss_face(Gamepad &pad) |
yfkwok | 13:02002658e718 | 215 | { |
yfkwok | 14:abe64fe0b6a5 | 216 | Vector2D face_pos = _face.get_pos(); |
yfkwok | 13:02002658e718 | 217 | // player has missed |
yfkwok | 14:abe64fe0b6a5 | 218 | if ((face_pos.x > 84) || (face_pos.x < -15) || (face_pos.y < -15) || (face_pos.y > 50)) { |
yfkwok | 14:abe64fe0b6a5 | 219 | _face.init(_speed); |
yfkwok | 13:02002658e718 | 220 | pad.tone(750.0,0.1); |
yfkwok | 13:02002658e718 | 221 | _count++; |
yfkwok | 13:02002658e718 | 222 | wait(0.1); |
yfkwok | 13:02002658e718 | 223 | _type = rand() % 8; // randomise type |
yfkwok | 14:abe64fe0b6a5 | 224 | } |
yfkwok | 14:abe64fe0b6a5 | 225 | } |
yfkwok | 14:abe64fe0b6a5 | 226 | |
yfkwok | 14:abe64fe0b6a5 | 227 | void Game_two::check_twitt_collide(Gamepad &pad, N5110 &lcd, int cha) |
yfkwok | 14:abe64fe0b6a5 | 228 | { |
yfkwok | 14:abe64fe0b6a5 | 229 | // read current coin attributes |
yfkwok | 14:abe64fe0b6a5 | 230 | Vector2D twitt_pos = _twitt.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 231 | Vector2D twitt_speed = _twitt.get_velocity(); |
yfkwok | 14:abe64fe0b6a5 | 232 | |
yfkwok | 14:abe64fe0b6a5 | 233 | // check p1 first |
yfkwok | 14:abe64fe0b6a5 | 234 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 235 | |
yfkwok | 14:abe64fe0b6a5 | 236 | // see if coin has hit the player by checking for overlaps |
yfkwok | 14:abe64fe0b6a5 | 237 | if (cha == 1){ |
yfkwok | 14:abe64fe0b6a5 | 238 | if ( |
yfkwok | 14:abe64fe0b6a5 | 239 | (twitt_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 240 | (twitt_pos.y <= p1_pos.y + 18) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 241 | (twitt_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 242 | (twitt_pos.x <= p1_pos.x + 20) //right |
yfkwok | 14:abe64fe0b6a5 | 243 | ) |
yfkwok | 14:abe64fe0b6a5 | 244 | { |
yfkwok | 14:abe64fe0b6a5 | 245 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 246 | _twitt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 247 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 248 | } |
yfkwok | 13:02002658e718 | 249 | } |
yfkwok | 14:abe64fe0b6a5 | 250 | if (cha == 2){ |
yfkwok | 14:abe64fe0b6a5 | 251 | if ( |
yfkwok | 14:abe64fe0b6a5 | 252 | (twitt_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 253 | (twitt_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 254 | (twitt_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 255 | (twitt_pos.x <= p1_pos.x + 21) //right |
yfkwok | 14:abe64fe0b6a5 | 256 | ) |
yfkwok | 14:abe64fe0b6a5 | 257 | { |
yfkwok | 14:abe64fe0b6a5 | 258 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 259 | _twitt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 260 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 261 | } |
yfkwok | 14:abe64fe0b6a5 | 262 | } |
yfkwok | 14:abe64fe0b6a5 | 263 | if (cha == 3){ |
yfkwok | 14:abe64fe0b6a5 | 264 | if ( |
yfkwok | 14:abe64fe0b6a5 | 265 | (twitt_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 266 | (twitt_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 267 | (twitt_pos.x >= p1_pos.x - 6) && //left |
yfkwok | 14:abe64fe0b6a5 | 268 | (twitt_pos.x <= p1_pos.x + 29) //right |
yfkwok | 14:abe64fe0b6a5 | 269 | ) |
yfkwok | 14:abe64fe0b6a5 | 270 | { |
yfkwok | 14:abe64fe0b6a5 | 271 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 272 | _twitt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 273 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 274 | } |
yfkwok | 14:abe64fe0b6a5 | 275 | } |
yfkwok | 13:02002658e718 | 276 | } |
yfkwok | 13:02002658e718 | 277 | |
yfkwok | 14:abe64fe0b6a5 | 278 | void Game_two::check_miss_twitt(Gamepad &pad) |
yfkwok | 14:abe64fe0b6a5 | 279 | { |
yfkwok | 14:abe64fe0b6a5 | 280 | Vector2D twitt_pos = _twitt.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 281 | // player has missed |
yfkwok | 14:abe64fe0b6a5 | 282 | if ((twitt_pos.x > 84) || (twitt_pos.x < -15) || (twitt_pos.y < -15) || (twitt_pos.y > 50)) { |
yfkwok | 14:abe64fe0b6a5 | 283 | _twitt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 284 | pad.tone(750.0,0.1); |
yfkwok | 14:abe64fe0b6a5 | 285 | _count++; |
yfkwok | 14:abe64fe0b6a5 | 286 | wait(0.1); |
yfkwok | 14:abe64fe0b6a5 | 287 | _type = rand() % 8; // randomise type |
yfkwok | 14:abe64fe0b6a5 | 288 | } |
yfkwok | 14:abe64fe0b6a5 | 289 | } |
yfkwok | 14:abe64fe0b6a5 | 290 | |
yfkwok | 14:abe64fe0b6a5 | 291 | void Game_two::check_yt_collide(Gamepad &pad, N5110 &lcd, int cha) |
yfkwok | 14:abe64fe0b6a5 | 292 | { |
yfkwok | 14:abe64fe0b6a5 | 293 | // read current coin attributes |
yfkwok | 14:abe64fe0b6a5 | 294 | Vector2D yt_pos = _yt.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 295 | Vector2D yt_speed = _yt.get_velocity(); |
yfkwok | 14:abe64fe0b6a5 | 296 | |
yfkwok | 14:abe64fe0b6a5 | 297 | // check p1 first |
yfkwok | 14:abe64fe0b6a5 | 298 | Vector2D p1_pos = _p1.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 299 | |
yfkwok | 14:abe64fe0b6a5 | 300 | // see if coin has hit the player by checking for overlaps |
yfkwok | 14:abe64fe0b6a5 | 301 | if (cha == 1){ |
yfkwok | 14:abe64fe0b6a5 | 302 | if ( |
yfkwok | 14:abe64fe0b6a5 | 303 | (yt_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 304 | (yt_pos.y <= p1_pos.y + 18) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 305 | (yt_pos.x >= p1_pos.x - 25) && //left |
yfkwok | 14:abe64fe0b6a5 | 306 | (yt_pos.x <= p1_pos.x + 20) //right |
yfkwok | 14:abe64fe0b6a5 | 307 | ) |
yfkwok | 14:abe64fe0b6a5 | 308 | { |
yfkwok | 14:abe64fe0b6a5 | 309 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 310 | _yt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 311 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 312 | } |
yfkwok | 14:abe64fe0b6a5 | 313 | } |
yfkwok | 14:abe64fe0b6a5 | 314 | if (cha == 2){ |
yfkwok | 14:abe64fe0b6a5 | 315 | if ( |
yfkwok | 14:abe64fe0b6a5 | 316 | (yt_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 317 | (yt_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 318 | (yt_pos.x >= p1_pos.x - 25) && //left |
yfkwok | 14:abe64fe0b6a5 | 319 | (yt_pos.x <= p1_pos.x + 21) //right |
yfkwok | 14:abe64fe0b6a5 | 320 | ) |
yfkwok | 14:abe64fe0b6a5 | 321 | { |
yfkwok | 14:abe64fe0b6a5 | 322 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 323 | _yt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 324 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 325 | } |
yfkwok | 14:abe64fe0b6a5 | 326 | } |
yfkwok | 14:abe64fe0b6a5 | 327 | if (cha == 3){ |
yfkwok | 14:abe64fe0b6a5 | 328 | if ( |
yfkwok | 14:abe64fe0b6a5 | 329 | (yt_pos.y >= p1_pos.y - 8) && //top |
yfkwok | 14:abe64fe0b6a5 | 330 | (yt_pos.y <= p1_pos.y + 21) && //bottom |
yfkwok | 14:abe64fe0b6a5 | 331 | (yt_pos.x >= p1_pos.x - 25) && //left |
yfkwok | 14:abe64fe0b6a5 | 332 | (yt_pos.x <= p1_pos.x + 29) //right |
yfkwok | 14:abe64fe0b6a5 | 333 | ) |
yfkwok | 14:abe64fe0b6a5 | 334 | { |
yfkwok | 14:abe64fe0b6a5 | 335 | // write new attributes |
yfkwok | 14:abe64fe0b6a5 | 336 | _yt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 337 | gameover(lcd, pad); |
yfkwok | 14:abe64fe0b6a5 | 338 | } |
yfkwok | 14:abe64fe0b6a5 | 339 | } |
yfkwok | 14:abe64fe0b6a5 | 340 | } |
yfkwok | 14:abe64fe0b6a5 | 341 | |
yfkwok | 14:abe64fe0b6a5 | 342 | void Game_two::check_miss_yt(Gamepad &pad) |
yfkwok | 14:abe64fe0b6a5 | 343 | { |
yfkwok | 14:abe64fe0b6a5 | 344 | Vector2D yt_pos = _yt.get_pos(); |
yfkwok | 14:abe64fe0b6a5 | 345 | // player has missed |
yfkwok | 14:abe64fe0b6a5 | 346 | if ((yt_pos.x > 84) || (yt_pos.x < -15) || (yt_pos.y < -15) || (yt_pos.y > 50)) { |
yfkwok | 14:abe64fe0b6a5 | 347 | _yt.init(_speed); |
yfkwok | 14:abe64fe0b6a5 | 348 | pad.tone(750.0,0.1); |
yfkwok | 14:abe64fe0b6a5 | 349 | _count++; |
yfkwok | 14:abe64fe0b6a5 | 350 | wait(0.1); |
yfkwok | 14:abe64fe0b6a5 | 351 | _type = rand() % 8; // randomise type |
yfkwok | 14:abe64fe0b6a5 | 352 | } |
yfkwok | 14:abe64fe0b6a5 | 353 | } |
yfkwok | 14:abe64fe0b6a5 | 354 | |
yfkwok | 14:abe64fe0b6a5 | 355 | /*void Game_two::check_miss_block(Gamepad &pad) |
yfkwok | 13:02002658e718 | 356 | { |
yfkwok | 13:02002658e718 | 357 | Vector2D block_pos = _block.get_pos(); |
yfkwok | 13:02002658e718 | 358 | // player has missed |
yfkwok | 13:02002658e718 | 359 | if (block_pos.x > WIDTH) { |
yfkwok | 13:02002658e718 | 360 | _block.init(_speed); |
yfkwok | 13:02002658e718 | 361 | pad.tone(750.0,0.1); |
yfkwok | 13:02002658e718 | 362 | wait(0.1); |
yfkwok | 13:02002658e718 | 363 | _type = rand() % 8; // randomise type |
yfkwok | 13:02002658e718 | 364 | } |
yfkwok | 13:02002658e718 | 365 | |
yfkwok | 13:02002658e718 | 366 | } |
yfkwok | 13:02002658e718 | 367 | |
yfkwok | 13:02002658e718 | 368 | int Game_two::print_scores(N5110 &lcd) |
yfkwok | 13:02002658e718 | 369 | { |
yfkwok | 13:02002658e718 | 370 | // get scores from paddles |
yfkwok | 13:02002658e718 | 371 | int p1_score = _p1.get_score(); |
yfkwok | 13:02002658e718 | 372 | int total = get_count(); |
yfkwok | 13:02002658e718 | 373 | |
yfkwok | 13:02002658e718 | 374 | // print to LCD i |
yfkwok | 13:02002658e718 | 375 | char buffer1[14]; |
yfkwok | 13:02002658e718 | 376 | sprintf(buffer1,"%2d / %2d",p1_score, total); |
yfkwok | 13:02002658e718 | 377 | lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
yfkwok | 13:02002658e718 | 378 | return p1_score; |
yfkwok | 14:abe64fe0b6a5 | 379 | }*/ |
yfkwok | 13:02002658e718 | 380 | |
yfkwok | 13:02002658e718 | 381 | void Game_two::set_count(int count) |
yfkwok | 13:02002658e718 | 382 | { |
yfkwok | 13:02002658e718 | 383 | _count = count; |
yfkwok | 13:02002658e718 | 384 | } |
yfkwok | 13:02002658e718 | 385 | |
yfkwok | 13:02002658e718 | 386 | int Game_two::get_count() |
yfkwok | 13:02002658e718 | 387 | { |
yfkwok | 13:02002658e718 | 388 | int count = _count; |
yfkwok | 13:02002658e718 | 389 | return count; |
yfkwok | 13:02002658e718 | 390 | } |
yfkwok | 13:02002658e718 | 391 | |
yfkwok | 13:02002658e718 | 392 | void Game_two::set_alt(int alt) |
yfkwok | 13:02002658e718 | 393 | { |
yfkwok | 13:02002658e718 | 394 | _alt = alt; |
yfkwok | 13:02002658e718 | 395 | } |
yfkwok | 13:02002658e718 | 396 | |
yfkwok | 13:02002658e718 | 397 | int Game_two::update_alt() |
yfkwok | 13:02002658e718 | 398 | { |
yfkwok | 13:02002658e718 | 399 | if(_alt < 4){_alt = _alt++;} |
yfkwok | 13:02002658e718 | 400 | else {_alt=0;} |
yfkwok | 13:02002658e718 | 401 | int alt = _alt; |
yfkwok | 13:02002658e718 | 402 | return alt; |
yfkwok | 13:02002658e718 | 403 | } |
yfkwok | 13:02002658e718 | 404 | |
yfkwok | 13:02002658e718 | 405 | void Game_two::gameover(N5110 &lcd, Gamepad &pad) |
yfkwok | 13:02002658e718 | 406 | { |
yfkwok | 13:02002658e718 | 407 | while(pad.check_event(Gamepad::B_PRESSED) == false) { |
yfkwok | 13:02002658e718 | 408 | lcd.clear(); |
yfkwok | 13:02002658e718 | 409 | lcd.printString("You rushed ",0,0); |
yfkwok | 14:abe64fe0b6a5 | 410 | lcd.printString("into social",0,1); |
yfkwok | 14:abe64fe0b6a5 | 411 | lcd.printString("media!",0,2); |
yfkwok | 14:abe64fe0b6a5 | 412 | lcd.printString("Game Over!",0,3); |
yfkwok | 13:02002658e718 | 413 | lcd.printString("Press B",0,5); |
yfkwok | 13:02002658e718 | 414 | lcd.refresh(); |
yfkwok | 13:02002658e718 | 415 | wait(0.1); |
yfkwok | 13:02002658e718 | 416 | } |
yfkwok | 14:abe64fe0b6a5 | 417 | _count = 30; |
yfkwok | 13:02002658e718 | 418 | } |
yfkwok | 13:02002658e718 | 419 | |
yfkwok | 13:02002658e718 | 420 | void Game_two::intro(Gamepad &pad, N5110 &lcd) |
yfkwok | 13:02002658e718 | 421 | { |
yfkwok | 13:02002658e718 | 422 | Page currentPage = PAGE_1; |
yfkwok | 13:02002658e718 | 423 | int fps = 8.0; |
yfkwok | 13:02002658e718 | 424 | int instruct_data[4][7] = { |
yfkwok | 13:02002658e718 | 425 | {1,1,1,1,1,1,1}, |
yfkwok | 13:02002658e718 | 426 | {0,1,1,1,1,1,0}, |
yfkwok | 13:02002658e718 | 427 | {0,0,1,1,1,0,0}, |
yfkwok | 13:02002658e718 | 428 | {0,0,0,1,0,0,0}, |
yfkwok | 13:02002658e718 | 429 | }; |
yfkwok | 13:02002658e718 | 430 | |
yfkwok | 13:02002658e718 | 431 | while(pad.check_event(Gamepad::A_PRESSED) == false){ |
yfkwok | 13:02002658e718 | 432 | switch (currentPage) { |
yfkwok | 13:02002658e718 | 433 | case PAGE_1: |
yfkwok | 13:02002658e718 | 434 | lcd.clear(); |
yfkwok | 14:abe64fe0b6a5 | 435 | lcd.printString("Charmander",0,0); |
yfkwok | 14:abe64fe0b6a5 | 436 | lcd.printString("is trying",0,1); |
yfkwok | 14:abe64fe0b6a5 | 437 | lcd.printString("to study!",0,2); |
yfkwok | 14:abe64fe0b6a5 | 438 | lcd.printString("Avoid all",0,4); |
yfkwok | 13:02002658e718 | 439 | lcd.drawSprite(39, 44, 4, 7, (int *)instruct_data); |
yfkwok | 13:02002658e718 | 440 | lcd.refresh(); |
yfkwok | 13:02002658e718 | 441 | if(pad.get_direction() == S){currentPage = PAGE_2;} |
yfkwok | 13:02002658e718 | 442 | wait(1.0f/fps); |
yfkwok | 13:02002658e718 | 443 | break; |
yfkwok | 13:02002658e718 | 444 | case PAGE_2: |
yfkwok | 13:02002658e718 | 445 | lcd.clear(); |
yfkwok | 14:abe64fe0b6a5 | 446 | lcd.printString("distractions",0,0); |
yfkwok | 14:abe64fe0b6a5 | 447 | lcd.printString("so he can pass",0,1); |
yfkwok | 14:abe64fe0b6a5 | 448 | lcd.printString("the test!",0,2); |
yfkwok | 13:02002658e718 | 449 | lcd.printString("Press A",0,5); |
yfkwok | 13:02002658e718 | 450 | lcd.refresh(); |
yfkwok | 13:02002658e718 | 451 | wait(1.0f/fps); |
yfkwok | 14:abe64fe0b6a5 | 452 | break; |
yfkwok | 13:02002658e718 | 453 | } |
yfkwok | 13:02002658e718 | 454 | } |
yfkwok | 14:abe64fe0b6a5 | 455 | } |