Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Snake/Snake.cpp@41:4edac50f010d, 2019-04-23 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Tue Apr 23 12:25:49 2019 +0000
- Revision:
- 41:4edac50f010d
- Parent:
- 39:210ac915e0a0
- Child:
- 43:233f93860d08
This version has a separate class to interface with length, and also changed a lot of the code to interface with it.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 7:48ba87cd79b5 | 1 | #include "Snake.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 2 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 3 | Snake::Snake() |
AhmedPlaymaker | 7:48ba87cd79b5 | 4 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 5 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 6 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 7 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 8 | Snake::~Snake() |
AhmedPlaymaker | 7:48ba87cd79b5 | 9 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 10 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 11 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 12 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 13 | //The Snake Sprite. |
AhmedPlaymaker | 8:890b986b16a4 | 14 | int snake_sprite[3][3] = { |
AhmedPlaymaker | 8:890b986b16a4 | 15 | {0,1,0}, |
AhmedPlaymaker | 8:890b986b16a4 | 16 | {1,0,1}, |
AhmedPlaymaker | 8:890b986b16a4 | 17 | {0,1,0}, |
AhmedPlaymaker | 9:d1d79d4ee673 | 18 | }; |
AhmedPlaymaker | 7:48ba87cd79b5 | 19 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 20 | void Snake::init() |
AhmedPlaymaker | 7:48ba87cd79b5 | 21 | { |
AhmedPlaymaker | 18:b391caa5754c | 22 | _speed = 1;// change this according to the options selected |
AhmedPlaymaker | 30:461231877c89 | 23 | m = 0; //Variable used to allow a starting location for the player. |
AhmedPlaymaker | 7:48ba87cd79b5 | 24 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 25 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 26 | |
AhmedPlaymaker | 41:4edac50f010d | 27 | void Snake::draw(Gamepad &pad, N5110 &lcd, int length, int level) |
AhmedPlaymaker | 33:249cf423fb18 | 28 | { |
AhmedPlaymaker | 41:4edac50f010d | 29 | _length = length; |
AhmedPlaymaker | 7:48ba87cd79b5 | 30 | if(m == 0){ |
AhmedPlaymaker | 21:e41126528cc6 | 31 | _x[0] = WIDTH/2; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 33:249cf423fb18 | 32 | _y[0] = HEIGHT - 3; |
AhmedPlaymaker | 22:ee698f66146f | 33 | |
AhmedPlaymaker | 22:ee698f66146f | 34 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 22:ee698f66146f | 35 | _x[i+1] = _x[i]; |
AhmedPlaymaker | 22:ee698f66146f | 36 | _y[i+1] = _y[i] - 3; |
AhmedPlaymaker | 22:ee698f66146f | 37 | b[i] = 1; |
AhmedPlaymaker | 22:ee698f66146f | 38 | } |
AhmedPlaymaker | 33:249cf423fb18 | 39 | b[14] = 1; |
AhmedPlaymaker | 33:249cf423fb18 | 40 | m = m+1; |
AhmedPlaymaker | 22:ee698f66146f | 41 | } |
AhmedPlaymaker | 21:e41126528cc6 | 42 | //printf("SPRITE %d %d \n", _x[0], _y[0]); |
AhmedPlaymaker | 41:4edac50f010d | 43 | if(_length == 0) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 44 | pad.init(); |
AhmedPlaymaker | 16:7b474f873683 | 45 | while ((pad.check_event(Gamepad::BACK_PRESSED) == false)) { |
AhmedPlaymaker | 16:7b474f873683 | 46 | lcd.clear(); |
AhmedPlaymaker | 16:7b474f873683 | 47 | lcd.printString("Game",33,1); |
AhmedPlaymaker | 16:7b474f873683 | 48 | lcd.printString("Over",33,3); |
AhmedPlaymaker | 16:7b474f873683 | 49 | lcd.printString("Press Back",14,5); //Function used to promt the user to Restart. |
AhmedPlaymaker | 16:7b474f873683 | 50 | lcd.refresh(); |
AhmedPlaymaker | 16:7b474f873683 | 51 | wait(0.1); |
AhmedPlaymaker | 16:7b474f873683 | 52 | } |
AhmedPlaymaker | 16:7b474f873683 | 53 | NVIC_SystemReset(); //Software Reset. //change this condition to loosing screen. |
AhmedPlaymaker | 8:890b986b16a4 | 54 | } |
AhmedPlaymaker | 24:1c118b071430 | 55 | |
AhmedPlaymaker | 37:ee47699915b8 | 56 | for(int a=1; a<=10; a++) { |
AhmedPlaymaker | 41:4edac50f010d | 57 | if(_length == a) { |
AhmedPlaymaker | 41:4edac50f010d | 58 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 59 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 41:4edac50f010d | 60 | } |
AhmedPlaymaker | 41:4edac50f010d | 61 | } |
AhmedPlaymaker | 41:4edac50f010d | 62 | } |
AhmedPlaymaker | 41:4edac50f010d | 63 | if(_length >= 10) { |
AhmedPlaymaker | 41:4edac50f010d | 64 | for(int a=1; a<=10; a++) { |
AhmedPlaymaker | 24:1c118b071430 | 65 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 66 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 24:1c118b071430 | 67 | } |
AhmedPlaymaker | 24:1c118b071430 | 68 | } |
AhmedPlaymaker | 8:890b986b16a4 | 69 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 70 | } |
AhmedPlaymaker | 19:05cc9f801468 | 71 | |
AhmedPlaymaker | 24:1c118b071430 | 72 | |
AhmedPlaymaker | 41:4edac50f010d | 73 | Vector2D Snake::get_pos() |
AhmedPlaymaker | 7:48ba87cd79b5 | 74 | { |
AhmedPlaymaker | 37:ee47699915b8 | 75 | for(int i=1; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 76 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 77 | Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 78 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 79 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 80 | } |
AhmedPlaymaker | 41:4edac50f010d | 81 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 82 | Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 83 | return snakepos; |
AhmedPlaymaker | 37:ee47699915b8 | 84 | } |
AhmedPlaymaker | 24:1c118b071430 | 85 | Vector2D snakepos = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 86 | return snakepos; |
AhmedPlaymaker | 7:48ba87cd79b5 | 87 | //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); |
AhmedPlaymaker | 13:9785f2404045 | 88 | } |
AhmedPlaymaker | 13:9785f2404045 | 89 | |
AhmedPlaymaker | 41:4edac50f010d | 90 | Vector2D Snake::get_pos_before1() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 91 | { |
AhmedPlaymaker | 37:ee47699915b8 | 92 | for(int i=2; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 93 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 94 | Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 95 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 96 | } |
AhmedPlaymaker | 13:9785f2404045 | 97 | } |
AhmedPlaymaker | 41:4edac50f010d | 98 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 99 | Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 100 | return snakepos_b1; |
AhmedPlaymaker | 37:ee47699915b8 | 101 | } |
AhmedPlaymaker | 24:1c118b071430 | 102 | Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 103 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 104 | //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y); |
AhmedPlaymaker | 13:9785f2404045 | 105 | } |
AhmedPlaymaker | 13:9785f2404045 | 106 | |
AhmedPlaymaker | 41:4edac50f010d | 107 | Vector2D Snake::get_pos_before2() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 108 | { |
AhmedPlaymaker | 37:ee47699915b8 | 109 | for(int i=3; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 110 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 111 | Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 112 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 113 | } |
AhmedPlaymaker | 13:9785f2404045 | 114 | } |
AhmedPlaymaker | 41:4edac50f010d | 115 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 116 | Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 117 | return snakepos_b2; |
AhmedPlaymaker | 37:ee47699915b8 | 118 | } |
AhmedPlaymaker | 24:1c118b071430 | 119 | Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 120 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 121 | //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y); |
AhmedPlaymaker | 13:9785f2404045 | 122 | } |
AhmedPlaymaker | 13:9785f2404045 | 123 | |
AhmedPlaymaker | 41:4edac50f010d | 124 | Vector2D Snake::get_pos_before3() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 125 | { |
AhmedPlaymaker | 37:ee47699915b8 | 126 | for(int i=4; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 127 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 128 | Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 129 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 130 | } |
AhmedPlaymaker | 13:9785f2404045 | 131 | } |
AhmedPlaymaker | 41:4edac50f010d | 132 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 133 | Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 134 | return snakepos_b3; |
AhmedPlaymaker | 37:ee47699915b8 | 135 | } |
AhmedPlaymaker | 24:1c118b071430 | 136 | Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 137 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 138 | //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y); |
AhmedPlaymaker | 13:9785f2404045 | 139 | } |
AhmedPlaymaker | 13:9785f2404045 | 140 | |
AhmedPlaymaker | 41:4edac50f010d | 141 | Vector2D Snake::get_pos_before4() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 142 | { |
AhmedPlaymaker | 37:ee47699915b8 | 143 | for(int i=5; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 144 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 145 | Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 146 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 147 | } |
AhmedPlaymaker | 13:9785f2404045 | 148 | } |
AhmedPlaymaker | 41:4edac50f010d | 149 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 150 | Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 151 | return snakepos_b4; |
AhmedPlaymaker | 37:ee47699915b8 | 152 | } |
AhmedPlaymaker | 24:1c118b071430 | 153 | Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 154 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 155 | //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y); |
AhmedPlaymaker | 13:9785f2404045 | 156 | } |
AhmedPlaymaker | 13:9785f2404045 | 157 | |
AhmedPlaymaker | 41:4edac50f010d | 158 | Vector2D Snake::get_pos_before5() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 159 | { |
AhmedPlaymaker | 37:ee47699915b8 | 160 | for(int i=6; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 161 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 162 | Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 163 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 164 | } |
AhmedPlaymaker | 13:9785f2404045 | 165 | } |
AhmedPlaymaker | 41:4edac50f010d | 166 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 167 | Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 168 | return snakepos_b5; |
AhmedPlaymaker | 37:ee47699915b8 | 169 | } |
AhmedPlaymaker | 24:1c118b071430 | 170 | Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 171 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 172 | //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y); |
AhmedPlaymaker | 13:9785f2404045 | 173 | } |
AhmedPlaymaker | 13:9785f2404045 | 174 | |
AhmedPlaymaker | 41:4edac50f010d | 175 | Vector2D Snake::get_pos_before6() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 176 | { |
AhmedPlaymaker | 37:ee47699915b8 | 177 | for(int i=7; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 178 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 179 | Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 180 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 181 | } |
AhmedPlaymaker | 13:9785f2404045 | 182 | } |
AhmedPlaymaker | 41:4edac50f010d | 183 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 184 | Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 185 | return snakepos_b6; |
AhmedPlaymaker | 37:ee47699915b8 | 186 | } |
AhmedPlaymaker | 24:1c118b071430 | 187 | Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 188 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 189 | //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y); |
AhmedPlaymaker | 13:9785f2404045 | 190 | } |
AhmedPlaymaker | 13:9785f2404045 | 191 | |
AhmedPlaymaker | 41:4edac50f010d | 192 | Vector2D Snake::get_pos_before7() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 193 | { |
AhmedPlaymaker | 37:ee47699915b8 | 194 | for(int i=8; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 195 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 196 | Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 197 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 198 | } |
AhmedPlaymaker | 13:9785f2404045 | 199 | } |
AhmedPlaymaker | 41:4edac50f010d | 200 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 201 | Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 202 | return snakepos_b7; |
AhmedPlaymaker | 37:ee47699915b8 | 203 | } |
AhmedPlaymaker | 24:1c118b071430 | 204 | Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 205 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 206 | //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y); |
AhmedPlaymaker | 7:48ba87cd79b5 | 207 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 208 | |
AhmedPlaymaker | 41:4edac50f010d | 209 | Vector2D Snake::get_pos_before8() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 210 | { |
AhmedPlaymaker | 37:ee47699915b8 | 211 | for(int i=9; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 212 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 213 | Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 214 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 215 | } |
AhmedPlaymaker | 13:9785f2404045 | 216 | } |
AhmedPlaymaker | 41:4edac50f010d | 217 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 218 | Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 219 | return snakepos_b8; |
AhmedPlaymaker | 37:ee47699915b8 | 220 | } |
AhmedPlaymaker | 24:1c118b071430 | 221 | Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 222 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 223 | //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y); |
AhmedPlaymaker | 13:9785f2404045 | 224 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 225 | |
AhmedPlaymaker | 41:4edac50f010d | 226 | Vector2D Snake::get_pos_before9() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 227 | { |
AhmedPlaymaker | 37:ee47699915b8 | 228 | for(int i=10; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 229 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 230 | Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 231 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 232 | } |
AhmedPlaymaker | 13:9785f2404045 | 233 | } |
AhmedPlaymaker | 41:4edac50f010d | 234 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 235 | Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 236 | return snakepos_b9; |
AhmedPlaymaker | 37:ee47699915b8 | 237 | } |
AhmedPlaymaker | 24:1c118b071430 | 238 | Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 239 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 240 | //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y); |
AhmedPlaymaker | 13:9785f2404045 | 241 | } |
AhmedPlaymaker | 13:9785f2404045 | 242 | |
AhmedPlaymaker | 41:4edac50f010d | 243 | void Snake::update(Direction d, int* b) |
AhmedPlaymaker | 13:9785f2404045 | 244 | { |
AhmedPlaymaker | 41:4edac50f010d | 245 | if(_length >= 10) {_length = 10;} //to stop the snake length virtually at 10 when it goes past it. |
AhmedPlaymaker | 41:4edac50f010d | 246 | |
AhmedPlaymaker | 25:e827f1a8fadc | 247 | //this makes all of the snake beeds chained together by making the lower ones drag towards where the top one was in the previous loop |
AhmedPlaymaker | 32:3a3bdeffdf62 | 248 | //the b[i] makes sure that the snake beed doesn't move if that beed is deactivated by colliding with a barrier. |
AhmedPlaymaker | 22:ee698f66146f | 249 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 37:ee47699915b8 | 250 | if((_length > i+1)&&(_x[i] != _x[i+1])) { |
AhmedPlaymaker | 22:ee698f66146f | 251 | if ((_x[i] > _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 252 | _x[i]-=_speed; |
AhmedPlaymaker | 8:890b986b16a4 | 253 | } |
AhmedPlaymaker | 22:ee698f66146f | 254 | if ((_x[i] < _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 255 | _x[i]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 256 | } |
AhmedPlaymaker | 21:e41126528cc6 | 257 | } |
AhmedPlaymaker | 21:e41126528cc6 | 258 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 259 | //this makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 22:ee698f66146f | 260 | for(int i=14; i>=0; i--) { |
AhmedPlaymaker | 37:ee47699915b8 | 261 | if((_length == i+1)&&(b[i] == 1)) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 262 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 263 | if (d == E) {_x[i]+= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 264 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 265 | if (d == W) {_x[i]-= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 266 | |
AhmedPlaymaker | 22:ee698f66146f | 267 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 268 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 269 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 270 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 271 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 272 | for(int i=2; i<=15; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 273 | |
AhmedPlaymaker | 37:ee47699915b8 | 274 | if(_length < i) {_x[i-1] = _x[i-2];} |
AhmedPlaymaker | 9:d1d79d4ee673 | 275 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 276 | |
AhmedPlaymaker | 25:e827f1a8fadc | 277 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 22:ee698f66146f | 278 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 279 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 280 | if (_x[i] <= 0) {_x[i] = 0;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 281 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 282 | if (_x[i] > 81) {_x[i] = 81;} |
AhmedPlaymaker | 8:890b986b16a4 | 283 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 284 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 285 |