ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp@12:60c856354406, 2020-05-29 (annotated)
- Committer:
- el17oc
- Date:
- Fri May 29 16:00:56 2020 +0000
- Revision:
- 12:60c856354406
- Parent:
- 10:ee781d18e0f6
- Child:
- 13:b37dde18bfdc
HH
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 | 12:60c856354406 | 20 | _x4 = 48; |
el17oc | 12:60c856354406 | 21 | _x5 = 48; |
el17oc | 6:bf90044188d0 | 22 | |
el17oc | 6:bf90044188d0 | 23 | _y0 = 20; |
el17oc | 6:bf90044188d0 | 24 | _y1 = 19; |
el17oc | 6:bf90044188d0 | 25 | _y2 = 18; |
el17oc | 6:bf90044188d0 | 26 | _y3 = 17; |
el17oc | 12:60c856354406 | 27 | _y4 = 16; |
el17oc | 12:60c856354406 | 28 | _y5 = 15; |
el17oc | 12:60c856354406 | 29 | |
el17oc | 9:a69a6a06dddf | 30 | _apx = 48; |
el17oc | 9:a69a6a06dddf | 31 | _apy = 25; |
el17oc | 6:bf90044188d0 | 32 | _gameover = false; |
el17oc | 12:60c856354406 | 33 | _reset_apple = false; |
el17oc | 6:bf90044188d0 | 34 | _score = 0; |
el17oc | 6:bf90044188d0 | 35 | _direction = up; |
el17oc | 12:60c856354406 | 36 | _countdown = 30; |
el17oc | 7:0ce806455ef1 | 37 | // Vector2D *_snakebody = new Vector2D [_length]; |
el17oc | 6:bf90044188d0 | 38 | } |
el17oc | 6:bf90044188d0 | 39 | |
el17oc | 9:a69a6a06dddf | 40 | Vector2D Snake::get_Snakehead() |
el17oc | 9:a69a6a06dddf | 41 | { |
el17oc | 9:a69a6a06dddf | 42 | Vector2D Snakehead; |
el17oc | 9:a69a6a06dddf | 43 | Snakehead.x = _x0; |
el17oc | 9:a69a6a06dddf | 44 | Snakehead.y = _y0; |
el17oc | 9:a69a6a06dddf | 45 | |
el17oc | 9:a69a6a06dddf | 46 | return Snakehead; |
el17oc | 9:a69a6a06dddf | 47 | |
el17oc | 9:a69a6a06dddf | 48 | } |
el17oc | 9:a69a6a06dddf | 49 | |
el17oc | 6:bf90044188d0 | 50 | |
el17oc | 6:bf90044188d0 | 51 | |
el17oc | 9:a69a6a06dddf | 52 | |
el17oc | 9:a69a6a06dddf | 53 | |
el17oc | 12:60c856354406 | 54 | void Snake::apple_collected(N5110 &lcd, Gamepad &pad) |
el17oc | 12:60c856354406 | 55 | { |
el17oc | 12:60c856354406 | 56 | |
el17oc | 9:a69a6a06dddf | 57 | //need to code clear apple and make sure apple isnt spawning every time |
el17oc | 8:997f90c88246 | 58 | if((_x0 == _apx) && (_y0 == _apy)) { |
el17oc | 9:a69a6a06dddf | 59 | |
el17oc | 12:60c856354406 | 60 | // _countdown = _reset_value; |
el17oc | 12:60c856354406 | 61 | _score++; |
el17oc | 12:60c856354406 | 62 | _reset_apple = true; |
el17oc | 12:60c856354406 | 63 | _countdown =10; //causes new apple position to be generated |
el17oc | 9:a69a6a06dddf | 64 | pad.tone(1500.0,0.5); |
el17oc | 8:997f90c88246 | 65 | pad.led(2, 1); |
el17oc | 8:997f90c88246 | 66 | pad.led(4, 1); |
el17oc | 12:60c856354406 | 67 | |
el17oc | 12:60c856354406 | 68 | //pad.led(2, 0); |
el17oc | 12:60c856354406 | 69 | //pad.led(4, 0); |
el17oc | 12:60c856354406 | 70 | |
el17oc | 12:60c856354406 | 71 | |
el17oc | 12:60c856354406 | 72 | |
el17oc | 12:60c856354406 | 73 | } else { |
el17oc | 12:60c856354406 | 74 | _countdown = _countdown - 1; |
el17oc | 12:60c856354406 | 75 | printf(" countdown = %d score %d ", _countdown ,_score); |
el17oc | 6:bf90044188d0 | 76 | |
el17oc | 6:bf90044188d0 | 77 | } |
el17oc | 12:60c856354406 | 78 | } |
el17oc | 1:897160a1a3ae | 79 | |
el17oc | 12:60c856354406 | 80 | int Snake::get_countdown() |
el17oc | 12:60c856354406 | 81 | { |
el17oc | 12:60c856354406 | 82 | return _countdown; |
el17oc | 1:897160a1a3ae | 83 | } |
el17oc | 1:897160a1a3ae | 84 | |
el17oc | 1:897160a1a3ae | 85 | |
el17oc | 12:60c856354406 | 86 | void Snake::check_gameover(N5110 &lcd) //code where it hits itself |
el17oc | 1:897160a1a3ae | 87 | { |
el17oc | 12:60c856354406 | 88 | if (_x0 == 0 ||_x0 == 84 || _y0 == 32 || _y0 == 0) { //how do i access snakehead.headx |
el17oc | 1:897160a1a3ae | 89 | |
el17oc | 12:60c856354406 | 90 | _gameover = true; |
el17oc | 12:60c856354406 | 91 | } |
el17oc | 12:60c856354406 | 92 | if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2) || (_x0 == _x3 && _y0 == _y3) || (_x0 == _x4 && _y0 == _y4)|| (_x0 == _x5 && _y0 == _y5)) { |
el17oc | 12:60c856354406 | 93 | _gameover = true; |
el17oc | 12:60c856354406 | 94 | } |
el17oc | 12:60c856354406 | 95 | if(_countdown == 0) { |
el17oc | 12:60c856354406 | 96 | _gameover = true; //|| (_x0 == _x4 && _y0 == _y4)) |
el17oc | 12:60c856354406 | 97 | |
el17oc | 12:60c856354406 | 98 | } |
el17oc | 8:997f90c88246 | 99 | } |
el17oc | 12:60c856354406 | 100 | |
el17oc | 12:60c856354406 | 101 | void Snake::gameover_true(N5110 &lcd) |
el17oc | 12:60c856354406 | 102 | { |
el17oc | 12:60c856354406 | 103 | |
el17oc | 12:60c856354406 | 104 | while (_gameover == true) { |
el17oc | 12:60c856354406 | 105 | |
el17oc | 12:60c856354406 | 106 | lcd.printString( " Game Over L ", 0, 2 ); |
el17oc | 12:60c856354406 | 107 | char buffer1[14]; |
el17oc | 12:60c856354406 | 108 | sprintf(buffer1,"%2d",_score); |
el17oc | 12:60c856354406 | 109 | lcd.printString(buffer1,0 ,48); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
el17oc | 12:60c856354406 | 110 | |
el17oc | 12:60c856354406 | 111 | |
el17oc | 12:60c856354406 | 112 | if (_score <= 7) { |
el17oc | 12:60c856354406 | 113 | lcd.printString(" Loser ", 0,10); |
el17oc | 12:60c856354406 | 114 | } |
el17oc | 12:60c856354406 | 115 | if (7< _score <= 25) { |
el17oc | 12:60c856354406 | 116 | lcd.printString(" Good Job! ", 0,10); |
el17oc | 12:60c856354406 | 117 | } else { |
el17oc | 12:60c856354406 | 118 | lcd.printString(" SNAKE PRO ", 0,10); |
el17oc | 12:60c856354406 | 119 | lcd.printString(" ~.~.~.~.<8>~ ", 0, 5); |
el17oc | 12:60c856354406 | 120 | |
el17oc | 12:60c856354406 | 121 | } |
el17oc | 12:60c856354406 | 122 | |
el17oc | 1:897160a1a3ae | 123 | } |
el17oc | 1:897160a1a3ae | 124 | } |
el17oc | 1:897160a1a3ae | 125 | |
el17oc | 12:60c856354406 | 126 | void Snake::render(N5110 &lcd) |
el17oc | 12:60c856354406 | 127 | { |
el17oc | 12:60c856354406 | 128 | lcd.refresh(); |
el17oc | 12:60c856354406 | 129 | |
el17oc | 12:60c856354406 | 130 | //apple |
el17oc | 12:60c856354406 | 131 | lcd.setPixel(_apx, _apy,1); //apple |
el17oc | 12:60c856354406 | 132 | |
el17oc | 12:60c856354406 | 133 | lcd.drawRect(0, 0, 84, 40, FILL_TRANSPARENT); //game |
el17oc | 12:60c856354406 | 134 | lcd.setPixel(_x0, _y0,1); //snake |
el17oc | 12:60c856354406 | 135 | lcd.setPixel(_x1, _y1,1); |
el17oc | 12:60c856354406 | 136 | lcd.setPixel(_x2, _y2,1); |
el17oc | 12:60c856354406 | 137 | lcd.setPixel(_x3, _y3,1); |
el17oc | 12:60c856354406 | 138 | lcd.setPixel(_x4, _y4,1); |
el17oc | 12:60c856354406 | 139 | lcd.setPixel(_x5, _y5,1); |
el17oc | 12:60c856354406 | 140 | } |
el17oc | 12:60c856354406 | 141 | |
el17oc | 1:897160a1a3ae | 142 | |
el17oc | 1:897160a1a3ae | 143 | |
el17oc | 2:44e4a6ecdbef | 144 | |
el17oc | 8:997f90c88246 | 145 | |
el17oc | 8:997f90c88246 | 146 | void Snake::get_direction(Gamepad &pad) |
el17oc | 6:bf90044188d0 | 147 | { |
el17oc | 12:60c856354406 | 148 | // int x; |
el17oc | 6:bf90044188d0 | 149 | Directions direction = _direction; |
el17oc | 6:bf90044188d0 | 150 | if(direction != left) { |
el17oc | 6:bf90044188d0 | 151 | if (pad.A_pressed()) { |
el17oc | 6:bf90044188d0 | 152 | |
el17oc | 6:bf90044188d0 | 153 | _direction = right; |
el17oc | 12:60c856354406 | 154 | // x=1; |
el17oc | 6:bf90044188d0 | 155 | } |
el17oc | 6:bf90044188d0 | 156 | } |
el17oc | 6:bf90044188d0 | 157 | if(direction != right) { |
el17oc | 10:ee781d18e0f6 | 158 | if (pad.Y_pressed()) { |
el17oc | 6:bf90044188d0 | 159 | |
el17oc | 6:bf90044188d0 | 160 | _direction = left; |
el17oc | 12:60c856354406 | 161 | // x=2; |
el17oc | 12:60c856354406 | 162 | } |
el17oc | 12:60c856354406 | 163 | |
el17oc | 12:60c856354406 | 164 | //check these are orrecrt |
el17oc | 6:bf90044188d0 | 165 | } |
el17oc | 6:bf90044188d0 | 166 | if(direction != down) { |
el17oc | 12:60c856354406 | 167 | if (pad.B_pressed()) { |
el17oc | 12:60c856354406 | 168 | // x=3; |
el17oc | 6:bf90044188d0 | 169 | _direction = up; |
el17oc | 6:bf90044188d0 | 170 | } |
el17oc | 6:bf90044188d0 | 171 | } |
el17oc | 6:bf90044188d0 | 172 | if(direction != up) { |
el17oc | 12:60c856354406 | 173 | if (pad.X_pressed()) { |
el17oc | 6:bf90044188d0 | 174 | |
el17oc | 6:bf90044188d0 | 175 | _direction = down; |
el17oc | 12:60c856354406 | 176 | // x=4; |
el17oc | 6:bf90044188d0 | 177 | } |
el17oc | 12:60c856354406 | 178 | } else { |
el17oc | 12:60c856354406 | 179 | _direction = _direction; |
el17oc | 6:bf90044188d0 | 180 | |
el17oc | 12:60c856354406 | 181 | } |
el17oc | 12:60c856354406 | 182 | // printf("direction %d ", x); |
el17oc | 12:60c856354406 | 183 | } |
el17oc | 1:897160a1a3ae | 184 | |
el17oc | 5:d716013c6a18 | 185 | |
el17oc | 5:d716013c6a18 | 186 | |
el17oc | 12:60c856354406 | 187 | void Snake::move_snake() |
el17oc | 12:60c856354406 | 188 | { |
el17oc | 12:60c856354406 | 189 | if (_direction == up) { |
el17oc | 12:60c856354406 | 190 | _x5 = _x5; |
el17oc | 12:60c856354406 | 191 | _x4 = _x3; |
el17oc | 12:60c856354406 | 192 | _x3 = _x2; |
el17oc | 12:60c856354406 | 193 | _y3 = _y2; |
el17oc | 12:60c856354406 | 194 | _x2 = _x1; |
el17oc | 12:60c856354406 | 195 | _y2 = _y1; |
el17oc | 12:60c856354406 | 196 | _x1 = _x0; |
el17oc | 12:60c856354406 | 197 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 198 | |
el17oc | 12:60c856354406 | 199 | _x0 = _x0; |
el17oc | 12:60c856354406 | 200 | _y0 = _y0 + 1; |
el17oc | 12:60c856354406 | 201 | } |
el17oc | 12:60c856354406 | 202 | if (_direction == down) { |
el17oc | 12:60c856354406 | 203 | _x5 = _x4; |
el17oc | 12:60c856354406 | 204 | _x4 = _x3; |
el17oc | 12:60c856354406 | 205 | _x3 = _x2; |
el17oc | 12:60c856354406 | 206 | _y3 = _y2; |
el17oc | 12:60c856354406 | 207 | _x2 = _x1; |
el17oc | 12:60c856354406 | 208 | _y2 = _y1; |
el17oc | 12:60c856354406 | 209 | _x1 = _x0; |
el17oc | 12:60c856354406 | 210 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 211 | |
el17oc | 12:60c856354406 | 212 | _x0 = _x0; |
el17oc | 12:60c856354406 | 213 | _y0 = _y0 - 1; |
el17oc | 12:60c856354406 | 214 | |
el17oc | 12:60c856354406 | 215 | } |
el17oc | 12:60c856354406 | 216 | if (_direction == left) { |
el17oc | 12:60c856354406 | 217 | |
el17oc | 12:60c856354406 | 218 | _x3 = _x2; |
el17oc | 12:60c856354406 | 219 | _y3 = _y2; |
el17oc | 12:60c856354406 | 220 | _x2 = _x1; |
el17oc | 12:60c856354406 | 221 | _y2 = _y1; |
el17oc | 12:60c856354406 | 222 | _x1 = _x0; |
el17oc | 12:60c856354406 | 223 | _y1 = _y0; |
el17oc | 12:60c856354406 | 224 | |
el17oc | 12:60c856354406 | 225 | _x0 = _x0 - 1; |
el17oc | 12:60c856354406 | 226 | _y0 = _y0; |
el17oc | 6:bf90044188d0 | 227 | |
el17oc | 6:bf90044188d0 | 228 | |
el17oc | 5:d716013c6a18 | 229 | |
el17oc | 5:d716013c6a18 | 230 | } |
el17oc | 6:bf90044188d0 | 231 | |
el17oc | 12:60c856354406 | 232 | if (_direction == right) { |
el17oc | 12:60c856354406 | 233 | _x3 = _x2; |
el17oc | 12:60c856354406 | 234 | _y3 = _y2; |
el17oc | 12:60c856354406 | 235 | _x2 = _x1; |
el17oc | 12:60c856354406 | 236 | _y2 = _y1; |
el17oc | 12:60c856354406 | 237 | _x1 = _x0; |
el17oc | 12:60c856354406 | 238 | _y1 = _y0; |
el17oc | 12:60c856354406 | 239 | |
el17oc | 12:60c856354406 | 240 | _x0 = _x0 + 1; |
el17oc | 12:60c856354406 | 241 | _y0 = _y0; |
el17oc | 12:60c856354406 | 242 | |
el17oc | 12:60c856354406 | 243 | |
el17oc | 12:60c856354406 | 244 | } else |
el17oc | 12:60c856354406 | 245 | _direction = _direction; |
el17oc | 12:60c856354406 | 246 | } |
el17oc | 9:a69a6a06dddf | 247 | |
el17oc | 6:bf90044188d0 | 248 | |
el17oc | 9:a69a6a06dddf | 249 | void Snake::render_clear_tail(N5110 &lcd) |
el17oc | 9:a69a6a06dddf | 250 | { |
el17oc | 12:60c856354406 | 251 | |
el17oc | 12:60c856354406 | 252 | lcd.setPixel(_x5, _y5, false); |
el17oc | 9:a69a6a06dddf | 253 | } |
el17oc | 6:bf90044188d0 | 254 | |
el17oc | 8:997f90c88246 | 255 | bool Snake::get_gameover() |
el17oc | 8:997f90c88246 | 256 | { |
el17oc | 8:997f90c88246 | 257 | return _gameover; |
el17oc | 8:997f90c88246 | 258 | } |
el17oc | 8:997f90c88246 | 259 | int Snake::get_score() |
el17oc | 8:997f90c88246 | 260 | { |
el17oc | 8:997f90c88246 | 261 | return _score; |
el17oc | 8:997f90c88246 | 262 | } |
el17oc | 10:ee781d18e0f6 | 263 | int Snake::get_oldscore() |
el17oc | 10:ee781d18e0f6 | 264 | { |
el17oc | 10:ee781d18e0f6 | 265 | return _oldscore; |
el17oc | 10:ee781d18e0f6 | 266 | } |
el17oc | 6:bf90044188d0 | 267 | |
el17oc | 6:bf90044188d0 | 268 | |
el17oc | 12:60c856354406 | 269 | int Snake::get_reset_value() |
el17oc | 12:60c856354406 | 270 | { |
el17oc | 12:60c856354406 | 271 | return _reset_value; |
el17oc | 12:60c856354406 | 272 | } |
el17oc | 12:60c856354406 | 273 | |
el17oc | 8:997f90c88246 | 274 | void Snake::get_Apple_position(N5110 &lcd) |
el17oc | 9:a69a6a06dddf | 275 | { |
el17oc | 12:60c856354406 | 276 | if(_reset_apple == true) |
el17oc | 12:60c856354406 | 277 | { |
el17oc | 12:60c856354406 | 278 | lcd.setPixel(_apx, _apy,0); |
el17oc | 12:60c856354406 | 279 | _apx = rand()%82+1; |
el17oc | 12:60c856354406 | 280 | _apy = rand()%30+1; |
el17oc | 12:60c856354406 | 281 | |
el17oc | 12:60c856354406 | 282 | _reset_apple = false; |
el17oc | 12:60c856354406 | 283 | } else { |
el17oc | 12:60c856354406 | 284 | _apx = _apx; |
el17oc | 12:60c856354406 | 285 | _apy = _apy; |
el17oc | 9:a69a6a06dddf | 286 | } |
el17oc | 6:bf90044188d0 | 287 | |
el17oc | 10:ee781d18e0f6 | 288 | } |
el17oc | 12:60c856354406 | 289 | /* |
el17oc | 8:997f90c88246 | 290 | void Snake::get_time(Timer &timer) |
el17oc | 6:bf90044188d0 | 291 | { |
el17oc | 12:60c856354406 | 292 | ticker.attach(&flip, 2.0); |
el17oc | 6:bf90044188d0 | 293 | _realtime = timer.read(); |
el17oc | 6:bf90044188d0 | 294 | _display_time = (Reset_value - _realtime); |
el17oc | 12:60c856354406 | 295 | int x = _realtime; |
el17oc | 12:60c856354406 | 296 | int y = _display_time; |
el17oc | 1:897160a1a3ae | 297 | |
el17oc | 12:60c856354406 | 298 | printf("12-T = %d t = %d ", y, x); |
el17oc | 12:60c856354406 | 299 | */ |
el17oc | 12:60c856354406 | 300 | |
el17oc | 12:60c856354406 | 301 | int Snake::set_reset_value() |
el17oc | 12:60c856354406 | 302 | { |
el17oc | 6:bf90044188d0 | 303 | |
el17oc | 6:bf90044188d0 | 304 | if (0 <= _score && _score < 10) { |
el17oc | 12:60c856354406 | 305 | _reset_value = 12; |
el17oc | 6:bf90044188d0 | 306 | } |
el17oc | 6:bf90044188d0 | 307 | if (10 <= _score && _score < 15) { |
el17oc | 12:60c856354406 | 308 | _reset_value = 10; |
el17oc | 6:bf90044188d0 | 309 | } |
el17oc | 6:bf90044188d0 | 310 | if (15 <= _score && _score < 20) { |
el17oc | 12:60c856354406 | 311 | _reset_value = 8; |
el17oc | 1:897160a1a3ae | 312 | } else { |
el17oc | 12:60c856354406 | 313 | _reset_value = 6; |
el17oc | 6:bf90044188d0 | 314 | } |
el17oc | 12:60c856354406 | 315 | |
el17oc | 12:60c856354406 | 316 | return _reset_value; |
el17oc | 10:ee781d18e0f6 | 317 | } |
el17oc | 10:ee781d18e0f6 | 318 | |
el17oc | 12:60c856354406 | 319 | |
el17oc | 12:60c856354406 | 320 | //void Snake::print_display_time(N5110 &lcd) |
el17oc | 12:60c856354406 | 321 | //{ |
el17oc | 12:60c856354406 | 322 | // int countdown = _realtime; |
el17oc | 10:ee781d18e0f6 | 323 | |
el17oc | 12:60c856354406 | 324 | // char buffer1[14]; |
el17oc | 12:60c856354406 | 325 | //sprintf(buffer1,"%2d",countdown); |
el17oc | 12:60c856354406 | 326 | //lcd.printString(buffer1,WIDTH/2,30); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
el17oc | 12:60c856354406 | 327 | //char buffer2[14]; |
el17oc | 10:ee781d18e0f6 | 328 | |
el17oc | 12:60c856354406 | 329 | //} |