Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Committer:
el17oc
Date:
Sat May 30 02:31:43 2020 +0000
Revision:
14:7fb3c93343b6
Parent:
13:b37dde18bfdc
Child:
16:9500059ad5d8
ff

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 14:7fb3c93343b6 14 void Snake::init()//int x, int y)
el17oc 6:bf90044188d0 15 {
el17oc 14:7fb3c93343b6 16 _x0 = 48; //initialises the snake and apple position
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 14:7fb3c93343b6 29
el17oc 14:7fb3c93343b6 30 _apx = 48; //initial apple position - directly in front of snake
el17oc 9:a69a6a06dddf 31 _apy = 25;
el17oc 14:7fb3c93343b6 32 _gameover = false; //when _gameover = true, game cannot be played
el17oc 12:60c856354406 33 _reset_apple = false;
el17oc 6:bf90044188d0 34 _score = 0;
el17oc 6:bf90044188d0 35 _direction = up;
el17oc 14:7fb3c93343b6 36 _countdown = 12; //initial number of moves is lower than the reset value in order to keep the number of moves low to increase difficulty
el17oc 14:7fb3c93343b6 37
el17oc 7:0ce806455ef1 38 // Vector2D *_snakebody = new Vector2D [_length];
el17oc 6:bf90044188d0 39 }
el17oc 6:bf90044188d0 40
el17oc 9:a69a6a06dddf 41 Vector2D Snake::get_Snakehead()
el17oc 9:a69a6a06dddf 42 {
el17oc 9:a69a6a06dddf 43 Vector2D Snakehead;
el17oc 9:a69a6a06dddf 44 Snakehead.x = _x0;
el17oc 9:a69a6a06dddf 45 Snakehead.y = _y0;
el17oc 9:a69a6a06dddf 46
el17oc 9:a69a6a06dddf 47 return Snakehead;
el17oc 9:a69a6a06dddf 48
el17oc 9:a69a6a06dddf 49 }
el17oc 9:a69a6a06dddf 50
el17oc 6:bf90044188d0 51
el17oc 6:bf90044188d0 52
el17oc 9:a69a6a06dddf 53
el17oc 9:a69a6a06dddf 54
el17oc 12:60c856354406 55 void Snake::apple_collected(N5110 &lcd, Gamepad &pad)
el17oc 12:60c856354406 56 {
el17oc 12:60c856354406 57
el17oc 9:a69a6a06dddf 58 //need to code clear apple and make sure apple isnt spawning every time
el17oc 14:7fb3c93343b6 59 if((_x0 == _apx) && (_y0 == _apy)) { // directly comparing position of the apple and the Snakehead - if they are the same: the score increases, countdown timer is reset, a new apple position is generated, LEDS and Speaker are triggered
el17oc 9:a69a6a06dddf 60
el17oc 12:60c856354406 61 // _countdown = _reset_value;
el17oc 12:60c856354406 62 _score++;
el17oc 14:7fb3c93343b6 63 _reset_apple = true; //causes new apple position to be generated - _reset _apple is compared to a bool true - when an apple is collected a new apple position is generated
el17oc 14:7fb3c93343b6 64 _countdown = _countdown + 38; //38 is added to the current countdown timer - this larger _countdown is, the further the snake can travel to collect apples.
el17oc 14:7fb3c93343b6 65 pad.tone(1500.0,0.5); //if the counter is reset to just 38, there will be some distances the snake cannot as it is further than 38 pixels away - unless the player collects accumulates their available moves
el17oc 14:7fb3c93343b6 66 pad.led(2, 1); //
el17oc 8:997f90c88246 67 pad.led(4, 1);
el17oc 14:7fb3c93343b6 68 wait(0.1);
el17oc 14:7fb3c93343b6 69 pad.led(2, 0);
el17oc 14:7fb3c93343b6 70 pad.led(4, 0);
el17oc 12:60c856354406 71
el17oc 12:60c856354406 72
el17oc 12:60c856354406 73
el17oc 12:60c856354406 74 } else {
el17oc 14:7fb3c93343b6 75 _countdown = _countdown - 1; //for each change in position, counter decreases by 1
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 14:7fb3c93343b6 82 //allows _countdown value to be called from other classes
el17oc 12:60c856354406 83 return _countdown;
el17oc 1:897160a1a3ae 84 }
el17oc 1:897160a1a3ae 85
el17oc 1:897160a1a3ae 86
el17oc 14:7fb3c93343b6 87 void Snake::check_gameover(N5110 &lcd)
el17oc 1:897160a1a3ae 88 {
el17oc 14:7fb3c93343b6 89 if (_x0 == 15 ||_x0 == 69 || _y0 == 32 || _y0 == 0) { // the snakehead coordinate is on any edge of the rectangle, it is gameover
el17oc 14:7fb3c93343b6 90 //the second condition is the game is over if the snake head cooroinate is equal to one of the snake body bits coordinate
el17oc 14:7fb3c93343b6 91 _gameover = true; // the third conditios is game is over if the counter = 0, indicating you've ran out of moves.
el17oc 8:997f90c88246 92 }
el17oc 14:7fb3c93343b6 93 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)|| (_x0 == _x6 && _y0 == _y6)|| (_x0 == _x7 && _y0 == _y7)) {
el17oc 14:7fb3c93343b6 94 _gameover = true;
el17oc 14:7fb3c93343b6 95 }
el17oc 14:7fb3c93343b6 96 if(_countdown == 0) {
el17oc 14:7fb3c93343b6 97 _gameover = true;
el17oc 12:60c856354406 98
el17oc 1:897160a1a3ae 99 }
el17oc 1:897160a1a3ae 100 }
el17oc 1:897160a1a3ae 101
el17oc 14:7fb3c93343b6 102 void Snake::gameover_true(N5110 &lcd, Gamepad &pad)
el17oc 12:60c856354406 103 {
el17oc 13:b37dde18bfdc 104
el17oc 14:7fb3c93343b6 105 while (_gameover == true) { //As _gameover is a member variable, if the condition is the previous function is met, the true value will be stored in _gameover and can be accessed in this function
el17oc 14:7fb3c93343b6 106 lcd.clear();
el17oc 14:7fb3c93343b6 107 pad.tone(500,1);
el17oc 14:7fb3c93343b6 108 lcd.printString( " Game Over ", 0, 1 );
el17oc 14:7fb3c93343b6 109 lcd.printString( " ~~~~~~~<:>-<", 0, 3 );
el17oc 14:7fb3c93343b6 110 char buffer1[14];
el17oc 14:7fb3c93343b6 111 sprintf(buffer1," Score: %2d", _score); //prints your score - cannot simply use N5110::printString as we need to output a value and the No. of inputs wouldnt match up to the function declaration
el17oc 14:7fb3c93343b6 112 lcd.printString(buffer1,0,4); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits ///print score
el17oc 14:7fb3c93343b6 113
el17oc 14:7fb3c93343b6 114 lcd.refresh();
el17oc 14:7fb3c93343b6 115 }
el17oc 14:7fb3c93343b6 116 }
el17oc 14:7fb3c93343b6 117
el17oc 14:7fb3c93343b6 118 void Snake::render(N5110 &lcd) //final function in the main function's while loop as the screen updatesbased on all any changes which occur in the functions
el17oc 14:7fb3c93343b6 119 {
el17oc 14:7fb3c93343b6 120 lcd.clear();
el17oc 12:60c856354406 121
el17oc 12:60c856354406 122 //apple
el17oc 14:7fb3c93343b6 123 lcd.setPixel(_apx, _apy,1); //plot apple position -whether it is a new position or the same position
el17oc 12:60c856354406 124
el17oc 14:7fb3c93343b6 125 lcd.drawRect(15, 0, 54, 32, FILL_TRANSPARENT); //plots border of snake map
el17oc 14:7fb3c93343b6 126
el17oc 14:7fb3c93343b6 127 lcd.setPixel(_x0, _y0,1); //plots snake body's new position changed by move_snake() function
el17oc 12:60c856354406 128 lcd.setPixel(_x1, _y1,1);
el17oc 12:60c856354406 129 lcd.setPixel(_x2, _y2,1);
el17oc 12:60c856354406 130 lcd.setPixel(_x3, _y3,1);
el17oc 12:60c856354406 131 lcd.setPixel(_x4, _y4,1);
el17oc 12:60c856354406 132 lcd.setPixel(_x5, _y5,1);
el17oc 14:7fb3c93343b6 133 lcd.setPixel(_x6, _y6,1);
el17oc 14:7fb3c93343b6 134 lcd.setPixel(_x7, _y7,1); //new position of the end bit of the snake is plotted - its old position cleared
el17oc 14:7fb3c93343b6 135 //NOTES CHANGLE LED VALUES SO IT IS TOP RIHGT OF THE BOX NOT TOP HALF OF THE SCREEN //CHANGE APPLE POSITION COORDINATES SO ITS IN BOX // GET SCORE IN THE SAME PLACE WHEN IT PRINTS AT END
el17oc 14:7fb3c93343b6 136
el17oc 14:7fb3c93343b6 137 char buffer1[14];
el17oc 14:7fb3c93343b6 138 sprintf(buffer1," %2d %2d",_score, _countdown); //prints score and counter on the same line
el17oc 14:7fb3c93343b6 139 lcd.printString(buffer1,0,5); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
el17oc 14:7fb3c93343b6 140
el17oc 14:7fb3c93343b6 141
el17oc 14:7fb3c93343b6 142 // char buffer2[14];
el17oc 14:7fb3c93343b6 143 // sprintf(buffer2,"%2d",_score);
el17oc 14:7fb3c93343b6 144 // lcd.printString(buffer2,0,3); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
el17oc 14:7fb3c93343b6 145
el17oc 14:7fb3c93343b6 146 lcd.refresh(); //applies plotting commands togeher
el17oc 14:7fb3c93343b6 147 }
el17oc 12:60c856354406 148
el17oc 1:897160a1a3ae 149
el17oc 1:897160a1a3ae 150
el17oc 2:44e4a6ecdbef 151
el17oc 8:997f90c88246 152
el17oc 8:997f90c88246 153 void Snake::get_direction(Gamepad &pad)
el17oc 6:bf90044188d0 154 {
el17oc 14:7fb3c93343b6 155 // int x;
el17oc 14:7fb3c93343b6 156 Directions direction = _direction; //"direction" stores the previous value of _direction to stop the snake going in the opposite direction to the way its travelling
el17oc 14:7fb3c93343b6 157
el17oc 14:7fb3c93343b6 158 if(direction != left) { //if current direction is left, the not statement is false blocking access to the next command from the 'A' button preventing _direction being set to right
el17oc 6:bf90044188d0 159 if (pad.A_pressed()) {
el17oc 6:bf90044188d0 160
el17oc 6:bf90044188d0 161 _direction = right;
el17oc 14:7fb3c93343b6 162 // x=1;
el17oc 6:bf90044188d0 163 }
el17oc 6:bf90044188d0 164 }
el17oc 6:bf90044188d0 165 if(direction != right) {
el17oc 10:ee781d18e0f6 166 if (pad.Y_pressed()) {
el17oc 6:bf90044188d0 167
el17oc 6:bf90044188d0 168 _direction = left;
el17oc 14:7fb3c93343b6 169 // x=2;
el17oc 12:60c856354406 170 }
el17oc 12:60c856354406 171
el17oc 6:bf90044188d0 172 }
el17oc 6:bf90044188d0 173 if(direction != down) {
el17oc 12:60c856354406 174 if (pad.B_pressed()) {
el17oc 14:7fb3c93343b6 175 // x=3;
el17oc 6:bf90044188d0 176 _direction = up;
el17oc 6:bf90044188d0 177 }
el17oc 6:bf90044188d0 178 }
el17oc 6:bf90044188d0 179 if(direction != up) {
el17oc 12:60c856354406 180 if (pad.X_pressed()) {
el17oc 6:bf90044188d0 181
el17oc 6:bf90044188d0 182 _direction = down;
el17oc 14:7fb3c93343b6 183 // x=4;
el17oc 6:bf90044188d0 184 }
el17oc 12:60c856354406 185 } else {
el17oc 14:7fb3c93343b6 186 _direction = _direction; // setting the direction equal to itself if no button is pressed means the snake will move automatically once direction is set
el17oc 6:bf90044188d0 187
el17oc 12:60c856354406 188 }
el17oc 14:7fb3c93343b6 189 // printf("direction %d ", x); //printf statements used in CoolTerm to check whether the input commands where working correctly
el17oc 12:60c856354406 190 }
el17oc 1:897160a1a3ae 191
el17oc 5:d716013c6a18 192
el17oc 5:d716013c6a18 193
el17oc 12:60c856354406 194 void Snake::move_snake()
el17oc 12:60c856354406 195 {
el17oc 14:7fb3c93343b6 196 if (_direction == up) { //shifting the snake position bit by bit, with respect to _x0 and _y0;
el17oc 14:7fb3c93343b6 197 _x7 = _x6; //coordinate _x[i], _y[i] = _x[i-1], _y[i-1]
el17oc 14:7fb3c93343b6 198 _y7 = _y6;
el17oc 14:7fb3c93343b6 199 _x6 = _x5;
el17oc 14:7fb3c93343b6 200 _y6 = _y5;
el17oc 14:7fb3c93343b6 201 _x5 = _x4;
el17oc 14:7fb3c93343b6 202 _y5 = _y4;
el17oc 14:7fb3c93343b6 203 _x4 = _x3;
el17oc 14:7fb3c93343b6 204 _y4 = _y3;
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 14:7fb3c93343b6 210 _y1 = _y0; //_x1 ,_y1 and must be assingned to _x0,_y0 before its own position updates otherwise bit 0 and bit 1 would be plotted as 1 pixel
el17oc 5:d716013c6a18 211
el17oc 12:60c856354406 212 _x0 = _x0;
el17oc 14:7fb3c93343b6 213 _y0 = _y0 + 1; //changes the position one bit at a time so the snake can only move horizontally or vertically
el17oc 12:60c856354406 214 }
el17oc 12:60c856354406 215 if (_direction == down) {
el17oc 14:7fb3c93343b6 216 _x7 = _x6;
el17oc 14:7fb3c93343b6 217 _y7 = _y6;
el17oc 14:7fb3c93343b6 218 _x6 = _x5;
el17oc 14:7fb3c93343b6 219 _y6 = _y5;
el17oc 12:60c856354406 220 _x5 = _x4;
el17oc 14:7fb3c93343b6 221 _y5 = _y4;
el17oc 12:60c856354406 222 _x4 = _x3;
el17oc 14:7fb3c93343b6 223 _y4 = _y3;
el17oc 12:60c856354406 224 _x3 = _x2;
el17oc 12:60c856354406 225 _y3 = _y2;
el17oc 12:60c856354406 226 _x2 = _x1;
el17oc 12:60c856354406 227 _y2 = _y1;
el17oc 12:60c856354406 228 _x1 = _x0;
el17oc 12:60c856354406 229 _y1 = _y0;
el17oc 5:d716013c6a18 230
el17oc 12:60c856354406 231 _x0 = _x0;
el17oc 12:60c856354406 232 _y0 = _y0 - 1;
el17oc 12:60c856354406 233
el17oc 12:60c856354406 234 }
el17oc 12:60c856354406 235 if (_direction == left) {
el17oc 14:7fb3c93343b6 236 _x7 = _x6;
el17oc 14:7fb3c93343b6 237 _y7 = _y6;
el17oc 14:7fb3c93343b6 238 _x6 = _x5;
el17oc 14:7fb3c93343b6 239 _y6 = _y5;
el17oc 13:b37dde18bfdc 240 _x5 = _x4;
el17oc 13:b37dde18bfdc 241 _y5 = _y4;
el17oc 14:7fb3c93343b6 242 _x4 = _x3;
el17oc 14:7fb3c93343b6 243 _y4 = _y3;
el17oc 12:60c856354406 244 _x3 = _x2;
el17oc 12:60c856354406 245 _y3 = _y2;
el17oc 12:60c856354406 246 _x2 = _x1;
el17oc 12:60c856354406 247 _y2 = _y1;
el17oc 12:60c856354406 248 _x1 = _x0;
el17oc 12:60c856354406 249 _y1 = _y0;
el17oc 12:60c856354406 250
el17oc 14:7fb3c93343b6 251 _x0 = _x0 - 1; //changes x=x-1 , y remains the same to move left
el17oc 12:60c856354406 252 _y0 = _y0;
el17oc 6:bf90044188d0 253
el17oc 6:bf90044188d0 254
el17oc 5:d716013c6a18 255
el17oc 5:d716013c6a18 256 }
el17oc 6:bf90044188d0 257
el17oc 12:60c856354406 258 if (_direction == right) {
el17oc 14:7fb3c93343b6 259 _x7 = _x6;
el17oc 14:7fb3c93343b6 260 _y7 = _y6;
el17oc 14:7fb3c93343b6 261 _x6 = _x5;
el17oc 14:7fb3c93343b6 262 _y6 = _y5;
el17oc 13:b37dde18bfdc 263 _x5 = _x4;
el17oc 13:b37dde18bfdc 264 _y5 = _y4;
el17oc 14:7fb3c93343b6 265 _x4 = _x3;
el17oc 14:7fb3c93343b6 266 _y4 = _y3;
el17oc 12:60c856354406 267 _x3 = _x2;
el17oc 12:60c856354406 268 _y3 = _y2;
el17oc 12:60c856354406 269 _x2 = _x1;
el17oc 12:60c856354406 270 _y2 = _y1;
el17oc 12:60c856354406 271 _x1 = _x0;
el17oc 12:60c856354406 272 _y1 = _y0;
el17oc 12:60c856354406 273
el17oc 12:60c856354406 274 _x0 = _x0 + 1;
el17oc 12:60c856354406 275 _y0 = _y0;
el17oc 12:60c856354406 276
el17oc 12:60c856354406 277
el17oc 14:7fb3c93343b6 278 }
el17oc 12:60c856354406 279 }
el17oc 9:a69a6a06dddf 280
el17oc 6:bf90044188d0 281
el17oc 9:a69a6a06dddf 282 void Snake::render_clear_tail(N5110 &lcd)
el17oc 9:a69a6a06dddf 283 {
el17oc 12:60c856354406 284
el17oc 14:7fb3c93343b6 285 lcd.setPixel(_x7, _y7, false); //sets the end pixel to 0. it must be set to 0 before its position updates otherwise the snakebody will grow continually from _x7, _y7 coordinate
el17oc 9:a69a6a06dddf 286 }
el17oc 6:bf90044188d0 287
el17oc 14:7fb3c93343b6 288 bool Snake::get_gameover() //methods to access member variables of the class
el17oc 8:997f90c88246 289 {
el17oc 8:997f90c88246 290 return _gameover;
el17oc 8:997f90c88246 291 }
el17oc 8:997f90c88246 292 int Snake::get_score()
el17oc 8:997f90c88246 293 {
el17oc 8:997f90c88246 294 return _score;
el17oc 8:997f90c88246 295 }
el17oc 6:bf90044188d0 296
el17oc 6:bf90044188d0 297
el17oc 14:7fb3c93343b6 298
el17oc 14:7fb3c93343b6 299
el17oc 12:60c856354406 300
el17oc 8:997f90c88246 301 void Snake::get_Apple_position(N5110 &lcd)
el17oc 9:a69a6a06dddf 302 {
el17oc 14:7fb3c93343b6 303 if(_reset_apple == true) { // _reset_apple is a triggered when an apple is collected and causes a new Apple position to be randomly generated
el17oc 14:7fb3c93343b6 304 _reset_apple = false; // returned to false immediately so another position can be generated function can be triggered again in the next loop
el17oc 14:7fb3c93343b6 305 lcd.setPixel(_apx, _apy,0); //sets the exisiting apple position to 0 to remove it from lcd.
el17oc 14:7fb3c93343b6 306 _apx = rand()%52+16; //54 = width of the rectangle on the lcd. - the x value of the apple has a range of 52 so it cannot spawn in either side of the wall
el17oc 14:7fb3c93343b6 307 // the range is from 16 to 69 as the lowest x coordinate of the rectangle is 15 and the highest is 69
el17oc 14:7fb3c93343b6 308 _apy = rand()%28+2;
el17oc 14:7fb3c93343b6 309 }
el17oc 12:60c856354406 310
el17oc 6:bf90044188d0 311
el17oc 10:ee781d18e0f6 312 }
el17oc 1:897160a1a3ae 313