ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp@11:e7c56013acd9, 2020-05-28 (annotated)
- Committer:
- el17oc
- Date:
- Thu May 28 21:59:12 2020 +0000
- Revision:
- 11:e7c56013acd9
- Parent:
- 10:ee781d18e0f6
messy test
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 | 11:e7c56013acd9 | 98 | |
el17oc | 8:997f90c88246 | 99 | lcd.refresh(); |
el17oc | 6:bf90044188d0 | 100 | |
el17oc | 5:d716013c6a18 | 101 | } |
el17oc | 5:d716013c6a18 | 102 | |
el17oc | 8:997f90c88246 | 103 | |
el17oc | 8:997f90c88246 | 104 | void Snake::get_direction(Gamepad &pad) |
el17oc | 6:bf90044188d0 | 105 | { |
el17oc | 6:bf90044188d0 | 106 | Directions direction = _direction; |
el17oc | 6:bf90044188d0 | 107 | if(direction != left) { |
el17oc | 6:bf90044188d0 | 108 | if (pad.A_pressed()) { |
el17oc | 6:bf90044188d0 | 109 | |
el17oc | 6:bf90044188d0 | 110 | _direction = right; |
el17oc | 6:bf90044188d0 | 111 | } |
el17oc | 6:bf90044188d0 | 112 | } |
el17oc | 6:bf90044188d0 | 113 | if(direction != right) { |
el17oc | 10:ee781d18e0f6 | 114 | if (pad.Y_pressed()) { |
el17oc | 6:bf90044188d0 | 115 | |
el17oc | 6:bf90044188d0 | 116 | _direction = left; |
el17oc | 6:bf90044188d0 | 117 | } //check these are orrecrt |
el17oc | 6:bf90044188d0 | 118 | } |
el17oc | 6:bf90044188d0 | 119 | if(direction != down) { |
el17oc | 6:bf90044188d0 | 120 | if (pad.X_pressed()) { |
el17oc | 6:bf90044188d0 | 121 | |
el17oc | 6:bf90044188d0 | 122 | _direction = up; |
el17oc | 6:bf90044188d0 | 123 | } |
el17oc | 6:bf90044188d0 | 124 | } |
el17oc | 6:bf90044188d0 | 125 | if(direction != up) { |
el17oc | 10:ee781d18e0f6 | 126 | if (pad.B_pressed()) { |
el17oc | 6:bf90044188d0 | 127 | |
el17oc | 6:bf90044188d0 | 128 | _direction = down; |
el17oc | 9:a69a6a06dddf | 129 | } |
el17oc | 9:a69a6a06dddf | 130 | } |
el17oc | 9:a69a6a06dddf | 131 | else { |
el17oc | 6:bf90044188d0 | 132 | _direction = direction; |
el17oc | 6:bf90044188d0 | 133 | } |
el17oc | 11:e7c56013acd9 | 134 | |
el17oc | 9:a69a6a06dddf | 135 | |
el17oc | 6:bf90044188d0 | 136 | if (_direction == up) { // /-/ |
el17oc | 6:bf90044188d0 | 137 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 138 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 139 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 140 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 141 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 142 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 143 | |
el17oc | 6:bf90044188d0 | 144 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 145 | _y0 = _y0 + 1; |
el17oc | 6:bf90044188d0 | 146 | } |
el17oc | 6:bf90044188d0 | 147 | if (_direction == down) { |
el17oc | 6:bf90044188d0 | 148 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 149 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 150 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 151 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 152 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 153 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 154 | |
el17oc | 6:bf90044188d0 | 155 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 156 | _y0 = _y0 - 1; |
el17oc | 6:bf90044188d0 | 157 | |
el17oc | 6:bf90044188d0 | 158 | } |
el17oc | 6:bf90044188d0 | 159 | if (_direction == left) { |
el17oc | 6:bf90044188d0 | 160 | |
el17oc | 6:bf90044188d0 | 161 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 162 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 163 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 164 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 165 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 166 | _y1 = _y0; |
el17oc | 6:bf90044188d0 | 167 | |
el17oc | 6:bf90044188d0 | 168 | _x0 = _x0 - 1; |
el17oc | 6:bf90044188d0 | 169 | _y0 = _y0; |
el17oc | 1:897160a1a3ae | 170 | |
el17oc | 5:d716013c6a18 | 171 | |
el17oc | 5:d716013c6a18 | 172 | |
el17oc | 5:d716013c6a18 | 173 | } |
el17oc | 5:d716013c6a18 | 174 | |
el17oc | 5:d716013c6a18 | 175 | if (_direction == right) { |
el17oc | 6:bf90044188d0 | 176 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 177 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 178 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 179 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 180 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 181 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 182 | |
el17oc | 6:bf90044188d0 | 183 | _x0 = _x0 + 1; |
el17oc | 6:bf90044188d0 | 184 | _y0 = _y0; |
el17oc | 6:bf90044188d0 | 185 | |
el17oc | 6:bf90044188d0 | 186 | |
el17oc | 5:d716013c6a18 | 187 | } |
el17oc | 5:d716013c6a18 | 188 | |
el17oc | 5:d716013c6a18 | 189 | } |
el17oc | 6:bf90044188d0 | 190 | |
el17oc | 9:a69a6a06dddf | 191 | |
el17oc | 6:bf90044188d0 | 192 | |
el17oc | 9:a69a6a06dddf | 193 | void Snake::render_clear_tail(N5110 &lcd) |
el17oc | 9:a69a6a06dddf | 194 | { |
el17oc | 11:e7c56013acd9 | 195 | int x = _score; |
el17oc | 11:e7c56013acd9 | 196 | _oldscore = x; |
el17oc | 9:a69a6a06dddf | 197 | lcd.clearPixel(_x3, _y3); |
el17oc | 9:a69a6a06dddf | 198 | } |
el17oc | 6:bf90044188d0 | 199 | |
el17oc | 8:997f90c88246 | 200 | bool Snake::get_gameover() |
el17oc | 8:997f90c88246 | 201 | { |
el17oc | 8:997f90c88246 | 202 | return _gameover; |
el17oc | 8:997f90c88246 | 203 | } |
el17oc | 8:997f90c88246 | 204 | int Snake::get_score() |
el17oc | 8:997f90c88246 | 205 | { |
el17oc | 8:997f90c88246 | 206 | return _score; |
el17oc | 8:997f90c88246 | 207 | } |
el17oc | 10:ee781d18e0f6 | 208 | int Snake::get_oldscore() |
el17oc | 10:ee781d18e0f6 | 209 | { |
el17oc | 10:ee781d18e0f6 | 210 | return _oldscore; |
el17oc | 10:ee781d18e0f6 | 211 | } |
el17oc | 6:bf90044188d0 | 212 | |
el17oc | 6:bf90044188d0 | 213 | |
el17oc | 8:997f90c88246 | 214 | void Snake::get_Apple_position(N5110 &lcd) |
el17oc | 9:a69a6a06dddf | 215 | { |
el17oc | 9:a69a6a06dddf | 216 | int appleposx; |
el17oc | 9:a69a6a06dddf | 217 | int appleposy; |
el17oc | 10:ee781d18e0f6 | 218 | if(_oldscore != _score) { |
el17oc | 9:a69a6a06dddf | 219 | appleposx = rand()%84; |
el17oc | 10:ee781d18e0f6 | 220 | appleposy = rand()%48; |
el17oc | 10:ee781d18e0f6 | 221 | |
el17oc | 10:ee781d18e0f6 | 222 | _apx = appleposx; |
el17oc | 10:ee781d18e0f6 | 223 | _apy = appleposy; |
el17oc | 9:a69a6a06dddf | 224 | } |
el17oc | 10:ee781d18e0f6 | 225 | else { |
el17oc | 10:ee781d18e0f6 | 226 | // if(lcd.getPixel(appleposx, appleposy)==1) { // this pixel is set -- 'if it is already filled on lcd, pick new position' |
el17oc | 10:ee781d18e0f6 | 227 | _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 | 228 | _apy = _apy; //alters value of private variable - this can then be accessed by get_Applepos |
el17oc | 6:bf90044188d0 | 229 | |
el17oc | 10:ee781d18e0f6 | 230 | } |
el17oc | 8:997f90c88246 | 231 | } |
el17oc | 8:997f90c88246 | 232 | |
el17oc | 9:a69a6a06dddf | 233 |