ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp@7:0ce806455ef1, 2020-05-28 (annotated)
- Committer:
- el17oc
- Date:
- Thu May 28 15:08:11 2020 +0000
- Revision:
- 7:0ce806455ef1
- Parent:
- 6:bf90044188d0
- Child:
- 8:997f90c88246
static snake
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 | 6:bf90044188d0 | 26 | // Vector2D SK0 {_x0, _y0}; |
el17oc | 6:bf90044188d0 | 27 | // Vector2D SK1 {_x1, _y1}; |
el17oc | 6:bf90044188d0 | 28 | //Vector2D SK2 {_x2, _y2}; |
el17oc | 6:bf90044188d0 | 29 | //Vector2D SK3 {_x3, _y3}; |
el17oc | 6:bf90044188d0 | 30 | |
el17oc | 6:bf90044188d0 | 31 | _gameover = false; |
el17oc | 6:bf90044188d0 | 32 | _score = 0; |
el17oc | 6:bf90044188d0 | 33 | _direction = up; |
el17oc | 7:0ce806455ef1 | 34 | // Vector2D *_snakebody = new Vector2D [_length]; |
el17oc | 6:bf90044188d0 | 35 | } |
el17oc | 6:bf90044188d0 | 36 | |
el17oc | 6:bf90044188d0 | 37 | |
el17oc | 6:bf90044188d0 | 38 | void Snake::check_score(N5110 &lcd, Gamepad &pad, Apple &apple, Timer &timer) |
el17oc | 6:bf90044188d0 | 39 | { |
el17oc | 6:bf90044188d0 | 40 | |
el17oc | 6:bf90044188d0 | 41 | |
el17oc | 6:bf90044188d0 | 42 | Vector2D Apos = apple.get_Applepos(lcd); |
el17oc | 6:bf90044188d0 | 43 | // printf("apple coordinate =, %d , %d",Apos.x, Apos.y); //need to code clear apple and make sure apple isnt spawning every time |
el17oc | 6:bf90044188d0 | 44 | if((_x0 == Apos.x) && (_y0 == Apos.y)) { |
el17oc | 6:bf90044188d0 | 45 | |
el17oc | 6:bf90044188d0 | 46 | lcd.setPixel(Apos.x, Apos.y, 0); // Plem:: Wanted to make sure that this didnt clear the pixel of the snake as it passes through it |
el17oc | 6:bf90044188d0 | 47 | _score++; |
el17oc | 6:bf90044188d0 | 48 | timer.reset(); |
el17oc | 7:0ce806455ef1 | 49 | pad.tone(1500.0,0.5); |
el17oc | 6:bf90044188d0 | 50 | pad.leds_on(); |
el17oc | 6:bf90044188d0 | 51 | wait(0.5); |
el17oc | 6:bf90044188d0 | 52 | pad.leds_off(); |
el17oc | 6:bf90044188d0 | 53 | |
el17oc | 6:bf90044188d0 | 54 | |
el17oc | 6:bf90044188d0 | 55 | lcd.refresh(); |
el17oc | 6:bf90044188d0 | 56 | |
el17oc | 6:bf90044188d0 | 57 | } |
el17oc | 1:897160a1a3ae | 58 | |
el17oc | 1:897160a1a3ae | 59 | } |
el17oc | 1:897160a1a3ae | 60 | |
el17oc | 1:897160a1a3ae | 61 | |
el17oc | 6:bf90044188d0 | 62 | void Snake::check_collisions() //code where it hits itself |
el17oc | 1:897160a1a3ae | 63 | { |
el17oc | 6:bf90044188d0 | 64 | if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) { //how do i access snakehead.headx |
el17oc | 1:897160a1a3ae | 65 | |
el17oc | 6:bf90044188d0 | 66 | _gameover = true; |
el17oc | 6:bf90044188d0 | 67 | } else { |
el17oc | 6:bf90044188d0 | 68 | _gameover = _gameover; |
el17oc | 1:897160a1a3ae | 69 | } |
el17oc | 1:897160a1a3ae | 70 | } |
el17oc | 1:897160a1a3ae | 71 | |
el17oc | 1:897160a1a3ae | 72 | |
el17oc | 1:897160a1a3ae | 73 | |
el17oc | 2:44e4a6ecdbef | 74 | |
el17oc | 6:bf90044188d0 | 75 | void Snake::render(N5110 &lcd, Apple &apple) |
el17oc | 1:897160a1a3ae | 76 | { |
el17oc | 1:897160a1a3ae | 77 | |
el17oc | 6:bf90044188d0 | 78 | Vector2D Apos = apple.get_Applepos(lcd); |
el17oc | 6:bf90044188d0 | 79 | lcd.setPixel(Apos.x, Apos.y,1); |
el17oc | 6:bf90044188d0 | 80 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); |
el17oc | 1:897160a1a3ae | 81 | |
el17oc | 6:bf90044188d0 | 82 | lcd.setPixel(_x0, _y0,1); |
el17oc | 6:bf90044188d0 | 83 | lcd.setPixel(_x1, _y1,1); |
el17oc | 6:bf90044188d0 | 84 | lcd.setPixel(_x2, _y2,1); |
el17oc | 6:bf90044188d0 | 85 | lcd.setPixel(_x3, _y3,1); |
el17oc | 1:897160a1a3ae | 86 | |
el17oc | 6:bf90044188d0 | 87 | // lcd.printString(" &d ",0,0, _display_time); |
el17oc | 6:bf90044188d0 | 88 | |
el17oc | 5:d716013c6a18 | 89 | } |
el17oc | 5:d716013c6a18 | 90 | |
el17oc | 6:bf90044188d0 | 91 | void Snake::get_position(Gamepad &pad) |
el17oc | 6:bf90044188d0 | 92 | { |
el17oc | 6:bf90044188d0 | 93 | Directions direction = _direction; |
el17oc | 6:bf90044188d0 | 94 | if(direction != left) { |
el17oc | 6:bf90044188d0 | 95 | if (pad.A_pressed()) { |
el17oc | 6:bf90044188d0 | 96 | |
el17oc | 6:bf90044188d0 | 97 | _direction = right; |
el17oc | 6:bf90044188d0 | 98 | } |
el17oc | 6:bf90044188d0 | 99 | } |
el17oc | 6:bf90044188d0 | 100 | if(direction != right) { |
el17oc | 6:bf90044188d0 | 101 | if (pad.B_pressed()) { |
el17oc | 6:bf90044188d0 | 102 | |
el17oc | 6:bf90044188d0 | 103 | _direction = left; |
el17oc | 6:bf90044188d0 | 104 | } //check these are orrecrt |
el17oc | 6:bf90044188d0 | 105 | } |
el17oc | 6:bf90044188d0 | 106 | if(direction != down) { |
el17oc | 6:bf90044188d0 | 107 | if (pad.X_pressed()) { |
el17oc | 6:bf90044188d0 | 108 | |
el17oc | 6:bf90044188d0 | 109 | _direction = up; |
el17oc | 6:bf90044188d0 | 110 | } |
el17oc | 6:bf90044188d0 | 111 | } |
el17oc | 6:bf90044188d0 | 112 | if(direction != up) { |
el17oc | 6:bf90044188d0 | 113 | if (pad.Y_pressed()) { |
el17oc | 6:bf90044188d0 | 114 | |
el17oc | 6:bf90044188d0 | 115 | _direction = down; |
el17oc | 6:bf90044188d0 | 116 | } else { |
el17oc | 6:bf90044188d0 | 117 | _direction = direction; |
el17oc | 6:bf90044188d0 | 118 | } |
el17oc | 5:d716013c6a18 | 119 | |
el17oc | 5:d716013c6a18 | 120 | |
el17oc | 6:bf90044188d0 | 121 | if (_direction == up) { // /-/ |
el17oc | 6:bf90044188d0 | 122 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 123 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 124 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 125 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 126 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 127 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 128 | |
el17oc | 6:bf90044188d0 | 129 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 130 | _y0 = _y0 + 1; |
el17oc | 5:d716013c6a18 | 131 | |
el17oc | 6:bf90044188d0 | 132 | ///// alters Snakehead initial value |
el17oc | 6:bf90044188d0 | 133 | // Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function |
el17oc | 6:bf90044188d0 | 134 | } |
el17oc | 6:bf90044188d0 | 135 | if (_direction == down) { |
el17oc | 6:bf90044188d0 | 136 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 137 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 138 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 139 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 140 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 141 | _y1 = _y0; |
el17oc | 5:d716013c6a18 | 142 | |
el17oc | 6:bf90044188d0 | 143 | _x0 = _x0; |
el17oc | 6:bf90044188d0 | 144 | _y0 = _y0 - 1; |
el17oc | 6:bf90044188d0 | 145 | |
el17oc | 6:bf90044188d0 | 146 | } |
el17oc | 6:bf90044188d0 | 147 | if (_direction == left) { |
el17oc | 6:bf90044188d0 | 148 | |
el17oc | 6:bf90044188d0 | 149 | _x3 = _x2; |
el17oc | 6:bf90044188d0 | 150 | _y3 = _y2; |
el17oc | 6:bf90044188d0 | 151 | _x2 = _x1; |
el17oc | 6:bf90044188d0 | 152 | _y2 = _y1; |
el17oc | 6:bf90044188d0 | 153 | _x1 = _x0; |
el17oc | 6:bf90044188d0 | 154 | _y1 = _y0; |
el17oc | 6:bf90044188d0 | 155 | |
el17oc | 6:bf90044188d0 | 156 | _x0 = _x0 - 1; |
el17oc | 6:bf90044188d0 | 157 | _y0 = _y0; |
el17oc | 1:897160a1a3ae | 158 | |
el17oc | 5:d716013c6a18 | 159 | |
el17oc | 5:d716013c6a18 | 160 | |
el17oc | 5:d716013c6a18 | 161 | } |
el17oc | 5:d716013c6a18 | 162 | |
el17oc | 5:d716013c6a18 | 163 | if (_direction == right) { |
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 | 5:d716013c6a18 | 170 | |
el17oc | 6:bf90044188d0 | 171 | _x0 = _x0 + 1; |
el17oc | 6:bf90044188d0 | 172 | _y0 = _y0; |
el17oc | 6:bf90044188d0 | 173 | |
el17oc | 6:bf90044188d0 | 174 | |
el17oc | 5:d716013c6a18 | 175 | } |
el17oc | 5:d716013c6a18 | 176 | |
el17oc | 5:d716013c6a18 | 177 | } |
el17oc | 6:bf90044188d0 | 178 | |
el17oc | 5:d716013c6a18 | 179 | } |
el17oc | 6:bf90044188d0 | 180 | |
el17oc | 6:bf90044188d0 | 181 | //MAKE WALL CLASS // MAIN MENU DIFFERENT DIFFUCLTIES CHANGING fps |
el17oc | 6:bf90044188d0 | 182 | //DRAW RECTANGLE - SHRINK // CHANE SHAPE AFTER CERTAIN SCORE --- SET UP TIMER |
el17oc | 6:bf90044188d0 | 183 | |
el17oc | 6:bf90044188d0 | 184 | //NOTES - maybe make 2 player - joystick is player 2 |
el17oc | 6:bf90044188d0 | 185 | |
el17oc | 6:bf90044188d0 | 186 | |
el17oc | 6:bf90044188d0 | 187 | |
el17oc | 6:bf90044188d0 | 188 | |
el17oc | 6:bf90044188d0 | 189 | |
el17oc | 6:bf90044188d0 | 190 | |
el17oc | 6:bf90044188d0 | 191 | void Snake::get_LEDs(Gamepad &pad) |
el17oc | 1:897160a1a3ae | 192 | { |
el17oc | 6:bf90044188d0 | 193 | pad.leds_off(); |
el17oc | 1:897160a1a3ae | 194 | |
el17oc | 6:bf90044188d0 | 195 | if (_x0 >= 42 && _y0 >= 24) { |
el17oc | 6:bf90044188d0 | 196 | // top right led on |
el17oc | 6:bf90044188d0 | 197 | |
el17oc | 6:bf90044188d0 | 198 | pad.led(4, 1); |
el17oc | 6:bf90044188d0 | 199 | } |
el17oc | 6:bf90044188d0 | 200 | // topleft led on |
el17oc | 6:bf90044188d0 | 201 | if (_x0 <= 42 && _y0 >=24) { |
el17oc | 6:bf90044188d0 | 202 | // top right led on |
el17oc | 6:bf90044188d0 | 203 | pad.led(1, 1); |
el17oc | 6:bf90044188d0 | 204 | } |
el17oc | 6:bf90044188d0 | 205 | //bottom left |
el17oc | 6:bf90044188d0 | 206 | if (_x0 <=42 && _y0 <= 24) { |
el17oc | 6:bf90044188d0 | 207 | |
el17oc | 6:bf90044188d0 | 208 | pad.led(3,1); |
el17oc | 6:bf90044188d0 | 209 | } |
el17oc | 6:bf90044188d0 | 210 | //bottom right |
el17oc | 6:bf90044188d0 | 211 | if (_x0 >= 42 && _y0 <= 24) { |
el17oc | 6:bf90044188d0 | 212 | // top right led on |
el17oc | 6:bf90044188d0 | 213 | pad.led(6, 1); |
el17oc | 6:bf90044188d0 | 214 | } |
el17oc | 6:bf90044188d0 | 215 | } |
el17oc | 6:bf90044188d0 | 216 | |
el17oc | 6:bf90044188d0 | 217 | |
el17oc | 6:bf90044188d0 | 218 | void Snake::get_timer(Timer &timer) |
el17oc | 6:bf90044188d0 | 219 | { |
el17oc | 6:bf90044188d0 | 220 | _realtime = timer.read(); |
el17oc | 6:bf90044188d0 | 221 | _display_time = (Reset_value - _realtime); |
el17oc | 1:897160a1a3ae | 222 | |
el17oc | 6:bf90044188d0 | 223 | if(_realtime == Reset_value) { |
el17oc | 1:897160a1a3ae | 224 | _gameover = true; |
el17oc | 6:bf90044188d0 | 225 | } |
el17oc | 6:bf90044188d0 | 226 | |
el17oc | 6:bf90044188d0 | 227 | if (0 <= _score && _score < 10) { |
el17oc | 6:bf90044188d0 | 228 | Reset_value = 12; |
el17oc | 6:bf90044188d0 | 229 | } |
el17oc | 6:bf90044188d0 | 230 | if (10 <= _score && _score < 15) { |
el17oc | 6:bf90044188d0 | 231 | Reset_value = 10; |
el17oc | 6:bf90044188d0 | 232 | } |
el17oc | 6:bf90044188d0 | 233 | if (15 <= _score && _score < 20) { |
el17oc | 6:bf90044188d0 | 234 | Reset_value = 8; |
el17oc | 1:897160a1a3ae | 235 | } else { |
el17oc | 6:bf90044188d0 | 236 | Reset_value = 6; |
el17oc | 6:bf90044188d0 | 237 | } |
el17oc | 6:bf90044188d0 | 238 | |
el17oc | 6:bf90044188d0 | 239 | |
el17oc | 6:bf90044188d0 | 240 | |
el17oc | 6:bf90044188d0 | 241 | } |
el17oc | 6:bf90044188d0 | 242 | |
el17oc | 6:bf90044188d0 | 243 | |
el17oc | 6:bf90044188d0 | 244 | bool Snake::get_gameover() |
el17oc | 6:bf90044188d0 | 245 | { |
el17oc | 6:bf90044188d0 | 246 | return _gameover; |
el17oc | 6:bf90044188d0 | 247 | } |
el17oc | 6:bf90044188d0 | 248 | int Snake::get_score() |
el17oc | 6:bf90044188d0 | 249 | { |
el17oc | 6:bf90044188d0 | 250 | return _score; |
el17oc | 1:897160a1a3ae | 251 | } |
el17oc | 6:bf90044188d0 | 252 | |
el17oc | 6:bf90044188d0 | 253 | void Snake::print_scores(N5110 &lcd) |
el17oc | 6:bf90044188d0 | 254 | { |
el17oc | 6:bf90044188d0 | 255 | int score = _score; |
el17oc | 6:bf90044188d0 | 256 | |
el17oc | 6:bf90044188d0 | 257 | char buffer1[14]; |
el17oc | 6:bf90044188d0 | 258 | sprintf(buffer1,"%2d",score); |
el17oc | 6:bf90044188d0 | 259 | lcd.printString(buffer1,WIDTH/2 - 3,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
el17oc | 6:bf90044188d0 | 260 | char buffer2[14]; |
el17oc | 5:d716013c6a18 | 261 | } |
el17oc | 6:bf90044188d0 | 262 | |
el17oc | 6:bf90044188d0 | 263 | void Snake::print_display_time(N5110 &lcd) |
el17oc | 6:bf90044188d0 | 264 | { |
el17oc | 7:0ce806455ef1 | 265 | int countdown = _realtime; |
el17oc | 6:bf90044188d0 | 266 | |
el17oc | 6:bf90044188d0 | 267 | char buffer1[14]; |
el17oc | 6:bf90044188d0 | 268 | sprintf(buffer1,"%2d",countdown); |
el17oc | 6:bf90044188d0 | 269 | lcd.printString(buffer1,WIDTH/2,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
el17oc | 6:bf90044188d0 | 270 | char buffer2[14]; |
el17oc | 6:bf90044188d0 | 271 | } |