ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp@8:997f90c88246, 2020-05-28 (annotated)
- Committer:
- el17oc
- Date:
- Thu May 28 18:50:56 2020 +0000
- Revision:
- 8:997f90c88246
- Parent:
- 7:0ce806455ef1
- Child:
- 9:a69a6a06dddf
GG
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 | 8:997f90c88246 | 26 | _apx = 48 |
el17oc | 8:997f90c88246 | 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 | 8:997f90c88246 | 34 | Vector2D get_Snakehead() { |
el17oc | 8:997f90c88246 | 35 | Vector2D Snakehead = {_x0, _y0}; |
el17oc | 8:997f90c88246 | 36 | return Snakehead |
el17oc | 8:997f90c88246 | 37 | } |
el17oc | 8:997f90c88246 | 38 | |
el17oc | 6:bf90044188d0 | 39 | |
el17oc | 6:bf90044188d0 | 40 | |
el17oc | 8:997f90c88246 | 41 | void Snake::apple_collected(N5110 &lcd, Gamepad &pad, Apple &apple, Timer &timer) |
el17oc | 8:997f90c88246 | 42 | { |
el17oc | 8:997f90c88246 | 43 | |
el17oc | 8:997f90c88246 | 44 | //need to code clear apple and make sure apple isnt spawning every time |
el17oc | 8:997f90c88246 | 45 | if((_x0 == _apx) && (_y0 == _apy)) { |
el17oc | 8:997f90c88246 | 46 | |
el17oc | 8:997f90c88246 | 47 | _score++; //causes new apple position to be generated |
el17oc | 6:bf90044188d0 | 48 | timer.reset(); |
el17oc | 7:0ce806455ef1 | 49 | pad.tone(1500.0,0.5); |
el17oc | 8:997f90c88246 | 50 | pad.led(2, 1); |
el17oc | 8:997f90c88246 | 51 | pad.led(4, 1); |
el17oc | 6:bf90044188d0 | 52 | wait(0.5); |
el17oc | 8:997f90c88246 | 53 | pad.led(2, 0); |
el17oc | 8:997f90c88246 | 54 | pad.led(4, 0); |
el17oc | 6:bf90044188d0 | 55 | |
el17oc | 6:bf90044188d0 | 56 | } |
el17oc | 1:897160a1a3ae | 57 | |
el17oc | 1:897160a1a3ae | 58 | } |
el17oc | 1:897160a1a3ae | 59 | |
el17oc | 1:897160a1a3ae | 60 | |
el17oc | 6:bf90044188d0 | 61 | void Snake::check_collisions() //code where it hits itself |
el17oc | 1:897160a1a3ae | 62 | { |
el17oc | 6:bf90044188d0 | 63 | if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) { //how do i access snakehead.headx |
el17oc | 1:897160a1a3ae | 64 | |
el17oc | 6:bf90044188d0 | 65 | _gameover = true; |
el17oc | 8:997f90c88246 | 66 | } |
el17oc | 8:997f90c88246 | 67 | if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2)) { |
el17oc | 8:997f90c88246 | 68 | _gameover = true; //|| (_x0 == _x4 && _y0 == _y4)) |
el17oc | 8:997f90c88246 | 69 | else { |
el17oc | 6:bf90044188d0 | 70 | _gameover = _gameover; |
el17oc | 1:897160a1a3ae | 71 | } |
el17oc | 1:897160a1a3ae | 72 | } |
el17oc | 1:897160a1a3ae | 73 | |
el17oc | 1:897160a1a3ae | 74 | |
el17oc | 1:897160a1a3ae | 75 | |
el17oc | 2:44e4a6ecdbef | 76 | |
el17oc | 8:997f90c88246 | 77 | void Snake::render(N5110 &lcd) |
el17oc | 1:897160a1a3ae | 78 | { |
el17oc | 1:897160a1a3ae | 79 | |
el17oc | 8:997f90c88246 | 80 | //apple |
el17oc | 8:997f90c88246 | 81 | lcd.setPixel(_apx, _apy,1); |
el17oc | 1:897160a1a3ae | 82 | |
el17oc | 8:997f90c88246 | 83 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); //game |
el17oc | 8:997f90c88246 | 84 | |
el17oc | 8:997f90c88246 | 85 | |
el17oc | 8:997f90c88246 | 86 | lcd.setPixel(_x0, _y0,1); //snake |
el17oc | 6:bf90044188d0 | 87 | lcd.setPixel(_x1, _y1,1); |
el17oc | 6:bf90044188d0 | 88 | lcd.setPixel(_x2, _y2,1); |
el17oc | 6:bf90044188d0 | 89 | lcd.setPixel(_x3, _y3,1); |
el17oc | 8:997f90c88246 | 90 | |
el17oc | 1:897160a1a3ae | 91 | |
el17oc | 8:997f90c88246 | 92 | lcd.refresh(); |
el17oc | 6:bf90044188d0 | 93 | |
el17oc | 5:d716013c6a18 | 94 | } |
el17oc | 5:d716013c6a18 | 95 | |
el17oc | 8:997f90c88246 | 96 | |
el17oc | 8:997f90c88246 | 97 | void Snake::get_direction(Gamepad &pad) |
el17oc | 6:bf90044188d0 | 98 | { |
el17oc | 6:bf90044188d0 | 99 | Directions direction = _direction; |
el17oc | 6:bf90044188d0 | 100 | if(direction != left) { |
el17oc | 6:bf90044188d0 | 101 | if (pad.A_pressed()) { |
el17oc | 6:bf90044188d0 | 102 | |
el17oc | 6:bf90044188d0 | 103 | _direction = right; |
el17oc | 6:bf90044188d0 | 104 | } |
el17oc | 6:bf90044188d0 | 105 | } |
el17oc | 6:bf90044188d0 | 106 | if(direction != right) { |
el17oc | 6:bf90044188d0 | 107 | if (pad.B_pressed()) { |
el17oc | 6:bf90044188d0 | 108 | |
el17oc | 6:bf90044188d0 | 109 | _direction = left; |
el17oc | 6:bf90044188d0 | 110 | } //check these are orrecrt |
el17oc | 6:bf90044188d0 | 111 | } |
el17oc | 6:bf90044188d0 | 112 | if(direction != down) { |
el17oc | 6:bf90044188d0 | 113 | if (pad.X_pressed()) { |
el17oc | 6:bf90044188d0 | 114 | |
el17oc | 6:bf90044188d0 | 115 | _direction = up; |
el17oc | 6:bf90044188d0 | 116 | } |
el17oc | 6:bf90044188d0 | 117 | } |
el17oc | 6:bf90044188d0 | 118 | if(direction != up) { |
el17oc | 6:bf90044188d0 | 119 | if (pad.Y_pressed()) { |
el17oc | 6:bf90044188d0 | 120 | |
el17oc | 6:bf90044188d0 | 121 | _direction = down; |
el17oc | 6:bf90044188d0 | 122 | } else { |
el17oc | 6:bf90044188d0 | 123 | _direction = direction; |
el17oc | 6:bf90044188d0 | 124 | } |
el17oc | 8:997f90c88246 | 125 | } |
el17oc | 8:997f90c88246 | 126 | void Snake::move_snake() { |
el17oc | 6:bf90044188d0 | 127 | if (_direction == up) { // /-/ |
el17oc | 6:bf90044188d0 | 128 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 129 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 130 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 131 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 132 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 133 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 134 | |
el17oc | 6:bf90044188d0 | 135 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 136 | _y0 = _y0 + 1; |
el17oc | 5:d716013c6a18 | 137 | |
el17oc | 6:bf90044188d0 | 138 | ///// alters Snakehead initial value |
el17oc | 6:bf90044188d0 | 139 | // Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function |
el17oc | 6:bf90044188d0 | 140 | } |
el17oc | 6:bf90044188d0 | 141 | if (_direction == down) { |
el17oc | 6:bf90044188d0 | 142 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 143 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 144 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 145 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 146 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 147 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 148 | |
el17oc | 6:bf90044188d0 | 149 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 150 | _y0 = _y0 - 1; |
el17oc | 6:bf90044188d0 | 151 | |
el17oc | 6:bf90044188d0 | 152 | } |
el17oc | 6:bf90044188d0 | 153 | if (_direction == left) { |
el17oc | 6:bf90044188d0 | 154 | |
el17oc | 6:bf90044188d0 | 155 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 156 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 157 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 158 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 159 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 160 | _y1 = _y0; |
el17oc | 6:bf90044188d0 | 161 | |
el17oc | 6:bf90044188d0 | 162 | _x0 = _x0 - 1; |
el17oc | 6:bf90044188d0 | 163 | _y0 = _y0; |
el17oc | 1:897160a1a3ae | 164 | |
el17oc | 5:d716013c6a18 | 165 | |
el17oc | 5:d716013c6a18 | 166 | |
el17oc | 5:d716013c6a18 | 167 | } |
el17oc | 5:d716013c6a18 | 168 | |
el17oc | 5:d716013c6a18 | 169 | if (_direction == right) { |
el17oc | 6:bf90044188d0 | 170 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 171 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 172 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 173 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 174 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 175 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 176 | |
el17oc | 6:bf90044188d0 | 177 | _x0 = _x0 + 1; |
el17oc | 6:bf90044188d0 | 178 | _y0 = _y0; |
el17oc | 6:bf90044188d0 | 179 | |
el17oc | 6:bf90044188d0 | 180 | |
el17oc | 5:d716013c6a18 | 181 | } |
el17oc | 5:d716013c6a18 | 182 | |
el17oc | 5:d716013c6a18 | 183 | } |
el17oc | 6:bf90044188d0 | 184 | |
el17oc | 5:d716013c6a18 | 185 | } |
el17oc | 6:bf90044188d0 | 186 | |
el17oc | 8:997f90c88246 | 187 | void Snake::render_clear_tail(N5110 &lcd) { |
el17oc | 8:997f90c88246 | 188 | lcd.clearPixel(_x3, _y3, 0); |
el17oc | 8:997f90c88246 | 189 | } |
el17oc | 6:bf90044188d0 | 190 | |
el17oc | 8:997f90c88246 | 191 | bool Snake::get_gameover() |
el17oc | 8:997f90c88246 | 192 | { |
el17oc | 8:997f90c88246 | 193 | return _gameover; |
el17oc | 8:997f90c88246 | 194 | } |
el17oc | 8:997f90c88246 | 195 | int Snake::get_score() |
el17oc | 8:997f90c88246 | 196 | { |
el17oc | 8:997f90c88246 | 197 | return _score; |
el17oc | 8:997f90c88246 | 198 | } |
el17oc | 6:bf90044188d0 | 199 | |
el17oc | 6:bf90044188d0 | 200 | |
el17oc | 6:bf90044188d0 | 201 | |
el17oc | 8:997f90c88246 | 202 | void Snake::get_Apple_position(N5110 &lcd) |
el17oc | 8:997f90c88246 | 203 | { |
el17oc | 6:bf90044188d0 | 204 | |
el17oc | 8:997f90c88246 | 205 | if(_score++) { |
el17oc | 8:997f90c88246 | 206 | int appleposx = rand()%84; |
el17oc | 8:997f90c88246 | 207 | int appleposy = rand()%48; //ROB apparently rand() is seeded so will generate same position each time - do you know any other random methods to put here? |
el17oc | 6:bf90044188d0 | 208 | } |
el17oc | 6:bf90044188d0 | 209 | |
el17oc | 8:997f90c88246 | 210 | if(lcd.getPixel(appleposx, appleposy)==1) { // this pixel is set -- 'if it is already filled on lcd, pick new position' |
el17oc | 8:997f90c88246 | 211 | appleposx = rand()%84; |
el17oc | 8:997f90c88246 | 212 | appleposy = rand()%48; // making sure the apple doesnt spawn inside the snakes body or wall which would increase the score |
el17oc | 8:997f90c88246 | 213 | } |
el17oc | 6:bf90044188d0 | 214 | |
el17oc | 8:997f90c88246 | 215 | |
el17oc | 8:997f90c88246 | 216 | _apx = appleposx; //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 | 8:997f90c88246 | 217 | _apy = appleposy; //alters value of private variable - this can then be accessed by get_Applepos |
el17oc | 8:997f90c88246 | 218 | |
el17oc | 8:997f90c88246 | 219 | } |
el17oc | 8:997f90c88246 | 220 | |
el17oc | 8:997f90c88246 | 221 | void Snake::get_time(Timer &timer) |
el17oc | 6:bf90044188d0 | 222 | { |
el17oc | 8:997f90c88246 | 223 | |
el17oc | 6:bf90044188d0 | 224 | _realtime = timer.read(); |
el17oc | 6:bf90044188d0 | 225 | _display_time = (Reset_value - _realtime); |
el17oc | 1:897160a1a3ae | 226 | |
el17oc | 6:bf90044188d0 | 227 | if(_realtime == Reset_value) { |
el17oc | 1:897160a1a3ae | 228 | _gameover = true; |
el17oc | 6:bf90044188d0 | 229 | } |
el17oc | 6:bf90044188d0 | 230 | |
el17oc | 6:bf90044188d0 | 231 | if (0 <= _score && _score < 10) { |
el17oc | 6:bf90044188d0 | 232 | Reset_value = 12; |
el17oc | 6:bf90044188d0 | 233 | } |
el17oc | 6:bf90044188d0 | 234 | if (10 <= _score && _score < 15) { |
el17oc | 6:bf90044188d0 | 235 | Reset_value = 10; |
el17oc | 6:bf90044188d0 | 236 | } |
el17oc | 6:bf90044188d0 | 237 | if (15 <= _score && _score < 20) { |
el17oc | 6:bf90044188d0 | 238 | Reset_value = 8; |
el17oc | 1:897160a1a3ae | 239 | } else { |
el17oc | 6:bf90044188d0 | 240 | Reset_value = 6; |
el17oc | 6:bf90044188d0 | 241 | } |
el17oc | 6:bf90044188d0 | 242 | } |