ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp@10:ee781d18e0f6, 2020-05-28 (annotated)
- Committer:
- el17oc
- Date:
- Thu May 28 20:58:49 2020 +0000
- Revision:
- 10:ee781d18e0f6
- Parent:
- 9:a69a6a06dddf
- Child:
- 11:e7c56013acd9
- Child:
- 12:60c856354406
kkk
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17oc | 1:897160a1a3ae | 1 | #include "snake.h" |
el17oc | 1:897160a1a3ae | 2 | |
el17oc | 1:897160a1a3ae | 3 | Snake::Snake() |
el17oc | 1:897160a1a3ae | 4 | { |
el17oc | 1:897160a1a3ae | 5 | |
el17oc | 1:897160a1a3ae | 6 | } |
el17oc | 1:897160a1a3ae | 7 | |
el17oc | 1:897160a1a3ae | 8 | Snake::~Snake() |
el17oc | 1:897160a1a3ae | 9 | { |
el17oc | 6:bf90044188d0 | 10 | } |
el17oc | 6:bf90044188d0 | 11 | |
el17oc | 6:bf90044188d0 | 12 | |
el17oc | 6:bf90044188d0 | 13 | |
el17oc | 6:bf90044188d0 | 14 | void Snake::init() |
el17oc | 6:bf90044188d0 | 15 | { |
el17oc | 6:bf90044188d0 | 16 | _x0 = 48; |
el17oc | 6:bf90044188d0 | 17 | _x1 = 48; |
el17oc | 6:bf90044188d0 | 18 | _x2 = 48; |
el17oc | 6:bf90044188d0 | 19 | _x3 = 48; |
el17oc | 6:bf90044188d0 | 20 | |
el17oc | 6:bf90044188d0 | 21 | _y0 = 20; |
el17oc | 6:bf90044188d0 | 22 | _y1 = 19; |
el17oc | 6:bf90044188d0 | 23 | _y2 = 18; |
el17oc | 6:bf90044188d0 | 24 | _y3 = 17; |
el17oc | 6:bf90044188d0 | 25 | |
el17oc | 9:a69a6a06dddf | 26 | _apx = 48; |
el17oc | 9:a69a6a06dddf | 27 | _apy = 25; |
el17oc | 6:bf90044188d0 | 28 | _gameover = false; |
el17oc | 6:bf90044188d0 | 29 | _score = 0; |
el17oc | 6:bf90044188d0 | 30 | _direction = up; |
el17oc | 7:0ce806455ef1 | 31 | // Vector2D *_snakebody = new Vector2D [_length]; |
el17oc | 6:bf90044188d0 | 32 | } |
el17oc | 6:bf90044188d0 | 33 | |
el17oc | 9:a69a6a06dddf | 34 | Vector2D Snake::get_Snakehead() |
el17oc | 9:a69a6a06dddf | 35 | { |
el17oc | 9:a69a6a06dddf | 36 | Vector2D Snakehead; |
el17oc | 9:a69a6a06dddf | 37 | Snakehead.x = _x0; |
el17oc | 9:a69a6a06dddf | 38 | Snakehead.y = _y0; |
el17oc | 9:a69a6a06dddf | 39 | |
el17oc | 9:a69a6a06dddf | 40 | return Snakehead; |
el17oc | 9:a69a6a06dddf | 41 | |
el17oc | 9:a69a6a06dddf | 42 | } |
el17oc | 9:a69a6a06dddf | 43 | |
el17oc | 6:bf90044188d0 | 44 | |
el17oc | 6:bf90044188d0 | 45 | |
el17oc | 9:a69a6a06dddf | 46 | |
el17oc | 9:a69a6a06dddf | 47 | |
el17oc | 9:a69a6a06dddf | 48 | void Snake::apple_collected(N5110 &lcd, Gamepad &pad, Timer &timer) |
el17oc | 10:ee781d18e0f6 | 49 | { |
el17oc | 10:ee781d18e0f6 | 50 | |
el17oc | 9:a69a6a06dddf | 51 | //need to code clear apple and make sure apple isnt spawning every time |
el17oc | 8:997f90c88246 | 52 | if((_x0 == _apx) && (_y0 == _apy)) { |
el17oc | 9:a69a6a06dddf | 53 | |
el17oc | 8:997f90c88246 | 54 | _score++; //causes new apple position to be generated |
el17oc | 6:bf90044188d0 | 55 | timer.reset(); |
el17oc | 9:a69a6a06dddf | 56 | pad.tone(1500.0,0.5); |
el17oc | 8:997f90c88246 | 57 | pad.led(2, 1); |
el17oc | 8:997f90c88246 | 58 | pad.led(4, 1); |
el17oc | 6:bf90044188d0 | 59 | wait(0.5); |
el17oc | 8:997f90c88246 | 60 | pad.led(2, 0); |
el17oc | 8:997f90c88246 | 61 | pad.led(4, 0); |
el17oc | 6:bf90044188d0 | 62 | |
el17oc | 6:bf90044188d0 | 63 | } |
el17oc | 1:897160a1a3ae | 64 | |
el17oc | 1:897160a1a3ae | 65 | } |
el17oc | 1:897160a1a3ae | 66 | |
el17oc | 1:897160a1a3ae | 67 | |
el17oc | 6:bf90044188d0 | 68 | void Snake::check_collisions() //code where it hits itself |
el17oc | 1:897160a1a3ae | 69 | { |
el17oc | 6:bf90044188d0 | 70 | if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) { //how do i access snakehead.headx |
el17oc | 1:897160a1a3ae | 71 | |
el17oc | 6:bf90044188d0 | 72 | _gameover = true; |
el17oc | 8:997f90c88246 | 73 | } |
el17oc | 8:997f90c88246 | 74 | if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2)) { |
el17oc | 9:a69a6a06dddf | 75 | _gameover = true; |
el17oc | 9:a69a6a06dddf | 76 | } //|| (_x0 == _x4 && _y0 == _y4)) |
el17oc | 9:a69a6a06dddf | 77 | else { |
el17oc | 6:bf90044188d0 | 78 | _gameover = _gameover; |
el17oc | 1:897160a1a3ae | 79 | } |
el17oc | 1:897160a1a3ae | 80 | } |
el17oc | 1:897160a1a3ae | 81 | |
el17oc | 1:897160a1a3ae | 82 | |
el17oc | 1:897160a1a3ae | 83 | |
el17oc | 2:44e4a6ecdbef | 84 | |
el17oc | 8:997f90c88246 | 85 | void Snake::render(N5110 &lcd) |
el17oc | 1:897160a1a3ae | 86 | { |
el17oc | 1:897160a1a3ae | 87 | |
el17oc | 9:a69a6a06dddf | 88 | //apple |
el17oc | 10:ee781d18e0f6 | 89 | lcd.setPixel(_apx, _apy,1); //apple |
el17oc | 1:897160a1a3ae | 90 | |
el17oc | 8:997f90c88246 | 91 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); //game |
el17oc | 8:997f90c88246 | 92 | |
el17oc | 9:a69a6a06dddf | 93 | |
el17oc | 8:997f90c88246 | 94 | lcd.setPixel(_x0, _y0,1); //snake |
el17oc | 6:bf90044188d0 | 95 | lcd.setPixel(_x1, _y1,1); |
el17oc | 6:bf90044188d0 | 96 | lcd.setPixel(_x2, _y2,1); |
el17oc | 6:bf90044188d0 | 97 | lcd.setPixel(_x3, _y3,1); |
el17oc | 9:a69a6a06dddf | 98 | |
el17oc | 1:897160a1a3ae | 99 | |
el17oc | 8:997f90c88246 | 100 | lcd.refresh(); |
el17oc | 6:bf90044188d0 | 101 | |
el17oc | 5:d716013c6a18 | 102 | } |
el17oc | 5:d716013c6a18 | 103 | |
el17oc | 8:997f90c88246 | 104 | |
el17oc | 8:997f90c88246 | 105 | void Snake::get_direction(Gamepad &pad) |
el17oc | 6:bf90044188d0 | 106 | { |
el17oc | 6:bf90044188d0 | 107 | Directions direction = _direction; |
el17oc | 6:bf90044188d0 | 108 | if(direction != left) { |
el17oc | 6:bf90044188d0 | 109 | if (pad.A_pressed()) { |
el17oc | 6:bf90044188d0 | 110 | |
el17oc | 6:bf90044188d0 | 111 | _direction = right; |
el17oc | 6:bf90044188d0 | 112 | } |
el17oc | 6:bf90044188d0 | 113 | } |
el17oc | 6:bf90044188d0 | 114 | if(direction != right) { |
el17oc | 10:ee781d18e0f6 | 115 | if (pad.Y_pressed()) { |
el17oc | 6:bf90044188d0 | 116 | |
el17oc | 6:bf90044188d0 | 117 | _direction = left; |
el17oc | 6:bf90044188d0 | 118 | } //check these are orrecrt |
el17oc | 6:bf90044188d0 | 119 | } |
el17oc | 6:bf90044188d0 | 120 | if(direction != down) { |
el17oc | 6:bf90044188d0 | 121 | if (pad.X_pressed()) { |
el17oc | 6:bf90044188d0 | 122 | |
el17oc | 6:bf90044188d0 | 123 | _direction = up; |
el17oc | 6:bf90044188d0 | 124 | } |
el17oc | 6:bf90044188d0 | 125 | } |
el17oc | 6:bf90044188d0 | 126 | if(direction != up) { |
el17oc | 10:ee781d18e0f6 | 127 | if (pad.B_pressed()) { |
el17oc | 6:bf90044188d0 | 128 | |
el17oc | 6:bf90044188d0 | 129 | _direction = down; |
el17oc | 9:a69a6a06dddf | 130 | } |
el17oc | 9:a69a6a06dddf | 131 | } |
el17oc | 9:a69a6a06dddf | 132 | else { |
el17oc | 6:bf90044188d0 | 133 | _direction = direction; |
el17oc | 6:bf90044188d0 | 134 | } |
el17oc | 9:a69a6a06dddf | 135 | } |
el17oc | 9:a69a6a06dddf | 136 | |
el17oc | 9:a69a6a06dddf | 137 | |
el17oc | 9:a69a6a06dddf | 138 | void Snake::move_snake() { |
el17oc | 6:bf90044188d0 | 139 | if (_direction == up) { // /-/ |
el17oc | 6:bf90044188d0 | 140 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 141 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 142 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 143 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 144 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 145 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 146 | |
el17oc | 6:bf90044188d0 | 147 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 148 | _y0 = _y0 + 1; |
el17oc | 6:bf90044188d0 | 149 | } |
el17oc | 6:bf90044188d0 | 150 | if (_direction == down) { |
el17oc | 6:bf90044188d0 | 151 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 152 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 153 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 154 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 155 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 156 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 157 | |
el17oc | 6:bf90044188d0 | 158 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 159 | _y0 = _y0 - 1; |
el17oc | 6:bf90044188d0 | 160 | |
el17oc | 6:bf90044188d0 | 161 | } |
el17oc | 6:bf90044188d0 | 162 | if (_direction == left) { |
el17oc | 6:bf90044188d0 | 163 | |
el17oc | 6:bf90044188d0 | 164 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 165 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 166 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 167 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 168 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 169 | _y1 = _y0; |
el17oc | 6:bf90044188d0 | 170 | |
el17oc | 6:bf90044188d0 | 171 | _x0 = _x0 - 1; |
el17oc | 6:bf90044188d0 | 172 | _y0 = _y0; |
el17oc | 1:897160a1a3ae | 173 | |
el17oc | 5:d716013c6a18 | 174 | |
el17oc | 5:d716013c6a18 | 175 | |
el17oc | 5:d716013c6a18 | 176 | } |
el17oc | 5:d716013c6a18 | 177 | |
el17oc | 5:d716013c6a18 | 178 | if (_direction == right) { |
el17oc | 6:bf90044188d0 | 179 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 180 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 181 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 182 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 183 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 184 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 185 | |
el17oc | 6:bf90044188d0 | 186 | _x0 = _x0 + 1; |
el17oc | 6:bf90044188d0 | 187 | _y0 = _y0; |
el17oc | 6:bf90044188d0 | 188 | |
el17oc | 6:bf90044188d0 | 189 | |
el17oc | 5:d716013c6a18 | 190 | } |
el17oc | 5:d716013c6a18 | 191 | |
el17oc | 5:d716013c6a18 | 192 | } |
el17oc | 6:bf90044188d0 | 193 | |
el17oc | 9:a69a6a06dddf | 194 | |
el17oc | 6:bf90044188d0 | 195 | |
el17oc | 9:a69a6a06dddf | 196 | void Snake::render_clear_tail(N5110 &lcd) |
el17oc | 9:a69a6a06dddf | 197 | { |
el17oc | 10:ee781d18e0f6 | 198 | _oldscore = _score; |
el17oc | 9:a69a6a06dddf | 199 | lcd.clearPixel(_x3, _y3); |
el17oc | 9:a69a6a06dddf | 200 | } |
el17oc | 6:bf90044188d0 | 201 | |
el17oc | 8:997f90c88246 | 202 | bool Snake::get_gameover() |
el17oc | 8:997f90c88246 | 203 | { |
el17oc | 8:997f90c88246 | 204 | return _gameover; |
el17oc | 8:997f90c88246 | 205 | } |
el17oc | 8:997f90c88246 | 206 | int Snake::get_score() |
el17oc | 8:997f90c88246 | 207 | { |
el17oc | 8:997f90c88246 | 208 | return _score; |
el17oc | 8:997f90c88246 | 209 | } |
el17oc | 10:ee781d18e0f6 | 210 | int Snake::get_oldscore() |
el17oc | 10:ee781d18e0f6 | 211 | { |
el17oc | 10:ee781d18e0f6 | 212 | return _oldscore; |
el17oc | 10:ee781d18e0f6 | 213 | } |
el17oc | 6:bf90044188d0 | 214 | |
el17oc | 6:bf90044188d0 | 215 | |
el17oc | 8:997f90c88246 | 216 | void Snake::get_Apple_position(N5110 &lcd) |
el17oc | 9:a69a6a06dddf | 217 | { |
el17oc | 9:a69a6a06dddf | 218 | int appleposx; |
el17oc | 9:a69a6a06dddf | 219 | int appleposy; |
el17oc | 10:ee781d18e0f6 | 220 | if(_oldscore != _score) { |
el17oc | 9:a69a6a06dddf | 221 | appleposx = rand()%84; |
el17oc | 10:ee781d18e0f6 | 222 | appleposy = rand()%48; |
el17oc | 10:ee781d18e0f6 | 223 | |
el17oc | 10:ee781d18e0f6 | 224 | _apx = appleposx; |
el17oc | 10:ee781d18e0f6 | 225 | _apy = appleposy; |
el17oc | 9:a69a6a06dddf | 226 | } |
el17oc | 10:ee781d18e0f6 | 227 | else { |
el17oc | 10:ee781d18e0f6 | 228 | // if(lcd.getPixel(appleposx, appleposy)==1) { // this pixel is set -- 'if it is already filled on lcd, pick new position' |
el17oc | 10:ee781d18e0f6 | 229 | _apx = _apx; //i and j are fed into applepos.x/y -- those values are then fed into set_applepos which assigngs that value to _appleposx/y which then is fed into get_applepos where it is stored and returned |
el17oc | 10:ee781d18e0f6 | 230 | _apy = _apy; //alters value of private variable - this can then be accessed by get_Applepos |
el17oc | 6:bf90044188d0 | 231 | |
el17oc | 10:ee781d18e0f6 | 232 | } |
el17oc | 8:997f90c88246 | 233 | } |
el17oc | 8:997f90c88246 | 234 | |
el17oc | 8:997f90c88246 | 235 | void Snake::get_time(Timer &timer) |
el17oc | 6:bf90044188d0 | 236 | { |
el17oc | 9:a69a6a06dddf | 237 | |
el17oc | 6:bf90044188d0 | 238 | _realtime = timer.read(); |
el17oc | 6:bf90044188d0 | 239 | _display_time = (Reset_value - _realtime); |
el17oc | 1:897160a1a3ae | 240 | |
el17oc | 6:bf90044188d0 | 241 | if(_realtime == Reset_value) { |
el17oc | 1:897160a1a3ae | 242 | _gameover = true; |
el17oc | 6:bf90044188d0 | 243 | } |
el17oc | 6:bf90044188d0 | 244 | |
el17oc | 6:bf90044188d0 | 245 | if (0 <= _score && _score < 10) { |
el17oc | 6:bf90044188d0 | 246 | Reset_value = 12; |
el17oc | 6:bf90044188d0 | 247 | } |
el17oc | 6:bf90044188d0 | 248 | if (10 <= _score && _score < 15) { |
el17oc | 6:bf90044188d0 | 249 | Reset_value = 10; |
el17oc | 6:bf90044188d0 | 250 | } |
el17oc | 6:bf90044188d0 | 251 | if (15 <= _score && _score < 20) { |
el17oc | 6:bf90044188d0 | 252 | Reset_value = 8; |
el17oc | 1:897160a1a3ae | 253 | } else { |
el17oc | 6:bf90044188d0 | 254 | Reset_value = 6; |
el17oc | 6:bf90044188d0 | 255 | } |
el17oc | 10:ee781d18e0f6 | 256 | } |
el17oc | 10:ee781d18e0f6 | 257 | |
el17oc | 10:ee781d18e0f6 | 258 | void Snake::print_display_time(N5110 &lcd) |
el17oc | 10:ee781d18e0f6 | 259 | { |
el17oc | 10:ee781d18e0f6 | 260 | int countdown = _realtime; |
el17oc | 10:ee781d18e0f6 | 261 | |
el17oc | 10:ee781d18e0f6 | 262 | char buffer1[14]; |
el17oc | 10:ee781d18e0f6 | 263 | sprintf(buffer1,"%2d",countdown); |
el17oc | 10:ee781d18e0f6 | 264 | lcd.printString(buffer1,WIDTH/2,30); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
el17oc | 10:ee781d18e0f6 | 265 | char buffer2[14]; |
el17oc | 10:ee781d18e0f6 | 266 | |
el17oc | 6:bf90044188d0 | 267 | } |