Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Committer:
el17oc
Date:
Thu May 28 15:02:30 2020 +0000
Revision:
6:bf90044188d0
Parent:
5:d716013c6a18
Child:
7:0ce806455ef1
static snake

Who changed what in which revision?

UserRevisionLine numberNew 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 6:bf90044188d0 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 6:bf90044188d0 49 //by waiting unil the snake has passed through before clearing the old apple however the new apple spawn needs to be ASAP after collection
el17oc 6:bf90044188d0 50 //randomises new values for _apx,_apy and draws on lcd -- draws on next loop-- the position is not needed until we compare it to Snakehead
el17oc 6:bf90044188d0 51 pad.tone(1500.0,0.5); //need to clear apple
el17oc 6:bf90044188d0 52 pad.leds_on();
el17oc 6:bf90044188d0 53 wait(0.5);
el17oc 6:bf90044188d0 54 pad.leds_off();
el17oc 6:bf90044188d0 55
el17oc 6:bf90044188d0 56
el17oc 6:bf90044188d0 57 lcd.refresh();
el17oc 6:bf90044188d0 58
el17oc 6:bf90044188d0 59 }
el17oc 1:897160a1a3ae 60
el17oc 1:897160a1a3ae 61 }
el17oc 1:897160a1a3ae 62
el17oc 1:897160a1a3ae 63
el17oc 6:bf90044188d0 64 void Snake::check_collisions() //code where it hits itself
el17oc 1:897160a1a3ae 65 {
el17oc 6:bf90044188d0 66 if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) { //how do i access snakehead.headx
el17oc 1:897160a1a3ae 67
el17oc 6:bf90044188d0 68 _gameover = true;
el17oc 6:bf90044188d0 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 6:bf90044188d0 77 void Snake::render(N5110 &lcd, Apple &apple)
el17oc 1:897160a1a3ae 78 {
el17oc 1:897160a1a3ae 79
el17oc 6:bf90044188d0 80 Vector2D Apos = apple.get_Applepos(lcd);
el17oc 6:bf90044188d0 81 lcd.setPixel(Apos.x, Apos.y,1);
el17oc 6:bf90044188d0 82 lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);
el17oc 1:897160a1a3ae 83
el17oc 6:bf90044188d0 84 lcd.setPixel(_x0, _y0,1);
el17oc 6:bf90044188d0 85 lcd.setPixel(_x1, _y1,1);
el17oc 6:bf90044188d0 86 lcd.setPixel(_x2, _y2,1);
el17oc 6:bf90044188d0 87 lcd.setPixel(_x3, _y3,1);
el17oc 1:897160a1a3ae 88
el17oc 6:bf90044188d0 89 // lcd.printString(" &d ",0,0, _display_time);
el17oc 6:bf90044188d0 90
el17oc 5:d716013c6a18 91 }
el17oc 5:d716013c6a18 92
el17oc 6:bf90044188d0 93 void Snake::get_position(Gamepad &pad)
el17oc 6:bf90044188d0 94 {
el17oc 6:bf90044188d0 95 Directions direction = _direction;
el17oc 6:bf90044188d0 96 if(direction != left) {
el17oc 6:bf90044188d0 97 if (pad.A_pressed()) {
el17oc 6:bf90044188d0 98
el17oc 6:bf90044188d0 99 _direction = right;
el17oc 6:bf90044188d0 100 }
el17oc 6:bf90044188d0 101 }
el17oc 6:bf90044188d0 102 if(direction != right) {
el17oc 6:bf90044188d0 103 if (pad.B_pressed()) {
el17oc 6:bf90044188d0 104
el17oc 6:bf90044188d0 105 _direction = left;
el17oc 6:bf90044188d0 106 } //check these are orrecrt
el17oc 6:bf90044188d0 107 }
el17oc 6:bf90044188d0 108 if(direction != down) {
el17oc 6:bf90044188d0 109 if (pad.X_pressed()) {
el17oc 6:bf90044188d0 110
el17oc 6:bf90044188d0 111 _direction = up;
el17oc 6:bf90044188d0 112 }
el17oc 6:bf90044188d0 113 }
el17oc 6:bf90044188d0 114 if(direction != up) {
el17oc 6:bf90044188d0 115 if (pad.Y_pressed()) {
el17oc 6:bf90044188d0 116
el17oc 6:bf90044188d0 117 _direction = down;
el17oc 6:bf90044188d0 118 } else {
el17oc 6:bf90044188d0 119 _direction = direction;
el17oc 6:bf90044188d0 120 }
el17oc 5:d716013c6a18 121
el17oc 5:d716013c6a18 122
el17oc 6:bf90044188d0 123 if (_direction == up) { // /-/
el17oc 6:bf90044188d0 124 _x3 = _x2;
el17oc 6:bf90044188d0 125 _y3 = _y2;
el17oc 6:bf90044188d0 126 _x2 = _x1;
el17oc 6:bf90044188d0 127 _y2 = _y1;
el17oc 6:bf90044188d0 128 _x1 = _x0;
el17oc 6:bf90044188d0 129 _y1 = _y0;
el17oc 5:d716013c6a18 130
el17oc 6:bf90044188d0 131 _x0 = _x0;
el17oc 6:bf90044188d0 132 _y0 = _y0 + 1;
el17oc 5:d716013c6a18 133
el17oc 6:bf90044188d0 134 ///// alters Snakehead initial value
el17oc 6:bf90044188d0 135 // Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function
el17oc 6:bf90044188d0 136 }
el17oc 6:bf90044188d0 137 if (_direction == down) {
el17oc 6:bf90044188d0 138 _x3 = _x2;
el17oc 6:bf90044188d0 139 _y3 = _y2;
el17oc 6:bf90044188d0 140 _x2 = _x1;
el17oc 6:bf90044188d0 141 _y2 = _y1;
el17oc 6:bf90044188d0 142 _x1 = _x0;
el17oc 6:bf90044188d0 143 _y1 = _y0;
el17oc 5:d716013c6a18 144
el17oc 6:bf90044188d0 145 _x0 = _x0;
el17oc 6:bf90044188d0 146 _y0 = _y0 - 1;
el17oc 6:bf90044188d0 147
el17oc 6:bf90044188d0 148 }
el17oc 6:bf90044188d0 149 if (_direction == left) {
el17oc 6:bf90044188d0 150
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 6:bf90044188d0 157
el17oc 6:bf90044188d0 158 _x0 = _x0 - 1;
el17oc 6:bf90044188d0 159 _y0 = _y0;
el17oc 1:897160a1a3ae 160
el17oc 5:d716013c6a18 161
el17oc 5:d716013c6a18 162
el17oc 5:d716013c6a18 163 }
el17oc 5:d716013c6a18 164
el17oc 5:d716013c6a18 165 if (_direction == right) {
el17oc 6:bf90044188d0 166 _x3 = _x2;
el17oc 6:bf90044188d0 167 _y3 = _y2;
el17oc 6:bf90044188d0 168 _x2 = _x1;
el17oc 6:bf90044188d0 169 _y2 = _y1;
el17oc 6:bf90044188d0 170 _x1 = _x0;
el17oc 6:bf90044188d0 171 _y1 = _y0;
el17oc 5:d716013c6a18 172
el17oc 6:bf90044188d0 173 _x0 = _x0 + 1;
el17oc 6:bf90044188d0 174 _y0 = _y0;
el17oc 6:bf90044188d0 175
el17oc 6:bf90044188d0 176
el17oc 5:d716013c6a18 177 }
el17oc 5:d716013c6a18 178
el17oc 5:d716013c6a18 179 }
el17oc 6:bf90044188d0 180
el17oc 5:d716013c6a18 181 }
el17oc 6:bf90044188d0 182
el17oc 6:bf90044188d0 183 //MAKE WALL CLASS // MAIN MENU DIFFERENT DIFFUCLTIES CHANGING fps
el17oc 6:bf90044188d0 184 //DRAW RECTANGLE - SHRINK // CHANE SHAPE AFTER CERTAIN SCORE --- SET UP TIMER
el17oc 6:bf90044188d0 185
el17oc 6:bf90044188d0 186 //NOTES - maybe make 2 player - joystick is player 2
el17oc 6:bf90044188d0 187
el17oc 6:bf90044188d0 188
el17oc 6:bf90044188d0 189
el17oc 6:bf90044188d0 190
el17oc 6:bf90044188d0 191
el17oc 6:bf90044188d0 192
el17oc 6:bf90044188d0 193 void Snake::get_LEDs(Gamepad &pad)
el17oc 1:897160a1a3ae 194 {
el17oc 6:bf90044188d0 195 pad.leds_off();
el17oc 1:897160a1a3ae 196
el17oc 6:bf90044188d0 197 if (_x0 >= 42 && _y0 >= 24) {
el17oc 6:bf90044188d0 198 // top right led on
el17oc 6:bf90044188d0 199
el17oc 6:bf90044188d0 200 pad.led(4, 1);
el17oc 6:bf90044188d0 201 }
el17oc 6:bf90044188d0 202 // topleft led on
el17oc 6:bf90044188d0 203 if (_x0 <= 42 && _y0 >=24) {
el17oc 6:bf90044188d0 204 // top right led on
el17oc 6:bf90044188d0 205 pad.led(1, 1);
el17oc 6:bf90044188d0 206 }
el17oc 6:bf90044188d0 207 //bottom left
el17oc 6:bf90044188d0 208 if (_x0 <=42 && _y0 <= 24) {
el17oc 6:bf90044188d0 209
el17oc 6:bf90044188d0 210 pad.led(3,1);
el17oc 6:bf90044188d0 211 }
el17oc 6:bf90044188d0 212 //bottom right
el17oc 6:bf90044188d0 213 if (_x0 >= 42 && _y0 <= 24) {
el17oc 6:bf90044188d0 214 // top right led on
el17oc 6:bf90044188d0 215 pad.led(6, 1);
el17oc 6:bf90044188d0 216 }
el17oc 6:bf90044188d0 217 }
el17oc 6:bf90044188d0 218
el17oc 6:bf90044188d0 219
el17oc 6:bf90044188d0 220 void Snake::get_timer(Timer &timer)
el17oc 6:bf90044188d0 221 {
el17oc 6:bf90044188d0 222 _realtime = timer.read();
el17oc 6:bf90044188d0 223 _display_time = (Reset_value - _realtime);
el17oc 1:897160a1a3ae 224
el17oc 6:bf90044188d0 225 if(_realtime == Reset_value) {
el17oc 1:897160a1a3ae 226 _gameover = true;
el17oc 6:bf90044188d0 227 }
el17oc 6:bf90044188d0 228
el17oc 6:bf90044188d0 229 if (0 <= _score && _score < 10) {
el17oc 6:bf90044188d0 230 Reset_value = 12;
el17oc 6:bf90044188d0 231 }
el17oc 6:bf90044188d0 232 if (10 <= _score && _score < 15) {
el17oc 6:bf90044188d0 233 Reset_value = 10;
el17oc 6:bf90044188d0 234 }
el17oc 6:bf90044188d0 235 if (15 <= _score && _score < 20) {
el17oc 6:bf90044188d0 236 Reset_value = 8;
el17oc 1:897160a1a3ae 237 } else {
el17oc 6:bf90044188d0 238 Reset_value = 6;
el17oc 6:bf90044188d0 239 }
el17oc 6:bf90044188d0 240
el17oc 6:bf90044188d0 241
el17oc 6:bf90044188d0 242
el17oc 6:bf90044188d0 243 }
el17oc 6:bf90044188d0 244
el17oc 6:bf90044188d0 245
el17oc 6:bf90044188d0 246 bool Snake::get_gameover()
el17oc 6:bf90044188d0 247 {
el17oc 6:bf90044188d0 248 return _gameover;
el17oc 6:bf90044188d0 249 }
el17oc 6:bf90044188d0 250 int Snake::get_score()
el17oc 6:bf90044188d0 251 {
el17oc 6:bf90044188d0 252 return _score;
el17oc 1:897160a1a3ae 253 }
el17oc 6:bf90044188d0 254
el17oc 6:bf90044188d0 255 void Snake::print_scores(N5110 &lcd)
el17oc 6:bf90044188d0 256 {
el17oc 6:bf90044188d0 257 int score = _score;
el17oc 6:bf90044188d0 258
el17oc 6:bf90044188d0 259 char buffer1[14];
el17oc 6:bf90044188d0 260 sprintf(buffer1,"%2d",score);
el17oc 6:bf90044188d0 261 lcd.printString(buffer1,WIDTH/2 - 3,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
el17oc 6:bf90044188d0 262 char buffer2[14];
el17oc 5:d716013c6a18 263 }
el17oc 6:bf90044188d0 264
el17oc 6:bf90044188d0 265 void Snake::print_display_time(N5110 &lcd)
el17oc 6:bf90044188d0 266 {
el17oc 6:bf90044188d0 267 int countdown = _realtime;
el17oc 6:bf90044188d0 268
el17oc 6:bf90044188d0 269 char buffer1[14];
el17oc 6:bf90044188d0 270 sprintf(buffer1,"%2d",countdown);
el17oc 6:bf90044188d0 271 lcd.printString(buffer1,WIDTH/2,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
el17oc 6:bf90044188d0 272 char buffer2[14];
el17oc 6:bf90044188d0 273 }