![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.cpp@68:b9cfd27987ac, 2019-05-04 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Sat May 04 16:56:04 2019 +0000
- Revision:
- 68:b9cfd27987ac
- Parent:
- 63:205f0ca48473
- Child:
- 70:7caab8069b9b
Added test headers and functions that makes sure that my snake moves and updates as expected.
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 | 68:b9cfd27987ac | 20 | void Snake::init(int length, int speed) |
AhmedPlaymaker | 7:48ba87cd79b5 | 21 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 22 | _length = length; |
AhmedPlaymaker | 68:b9cfd27987ac | 23 | _speed = speed;// change this according to the options selected |
AhmedPlaymaker | 30:461231877c89 | 24 | m = 0; //Variable used to allow a starting location for the player. |
AhmedPlaymaker | 7:48ba87cd79b5 | 25 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 26 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 27 | |
AhmedPlaymaker | 53:527cf297b088 | 28 | void Snake::draw(N5110 &lcd, int length, int level) |
AhmedPlaymaker | 33:249cf423fb18 | 29 | { |
AhmedPlaymaker | 41:4edac50f010d | 30 | _length = length; |
AhmedPlaymaker | 63:205f0ca48473 | 31 | if(m == 0) { |
AhmedPlaymaker | 68:b9cfd27987ac | 32 | Vector2D p = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 33 | Snake::set_pos(p); |
AhmedPlaymaker | 63:205f0ca48473 | 34 | |
AhmedPlaymaker | 22:ee698f66146f | 35 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 22:ee698f66146f | 36 | b[i] = 1; |
AhmedPlaymaker | 22:ee698f66146f | 37 | } |
AhmedPlaymaker | 33:249cf423fb18 | 38 | b[14] = 1; |
AhmedPlaymaker | 33:249cf423fb18 | 39 | m = m+1; |
AhmedPlaymaker | 22:ee698f66146f | 40 | } |
AhmedPlaymaker | 63:205f0ca48473 | 41 | //printf("SPRITE %d %d \n", _x[0], _y[0]); |
AhmedPlaymaker | 63:205f0ca48473 | 42 | |
AhmedPlaymaker | 37:ee47699915b8 | 43 | for(int a=1; a<=10; a++) { |
AhmedPlaymaker | 41:4edac50f010d | 44 | if(_length == a) { |
AhmedPlaymaker | 41:4edac50f010d | 45 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 46 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 41:4edac50f010d | 47 | } |
AhmedPlaymaker | 41:4edac50f010d | 48 | } |
AhmedPlaymaker | 41:4edac50f010d | 49 | } |
AhmedPlaymaker | 41:4edac50f010d | 50 | if(_length >= 10) { |
AhmedPlaymaker | 41:4edac50f010d | 51 | for(int a=1; a<=10; a++) { |
AhmedPlaymaker | 24:1c118b071430 | 52 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 53 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 24:1c118b071430 | 54 | } |
AhmedPlaymaker | 24:1c118b071430 | 55 | } |
AhmedPlaymaker | 8:890b986b16a4 | 56 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 57 | } |
AhmedPlaymaker | 19:05cc9f801468 | 58 | |
AhmedPlaymaker | 24:1c118b071430 | 59 | |
AhmedPlaymaker | 68:b9cfd27987ac | 60 | void Snake::set_pos(Vector2D p) |
AhmedPlaymaker | 68:b9cfd27987ac | 61 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 62 | _x[0] = p.x; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 63 | _y[0] = p.y; |
AhmedPlaymaker | 68:b9cfd27987ac | 64 | |
AhmedPlaymaker | 68:b9cfd27987ac | 65 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 68:b9cfd27987ac | 66 | _x[i+1] = _x[i]; |
AhmedPlaymaker | 68:b9cfd27987ac | 67 | _y[i+1] = _y[i] - 3; |
AhmedPlaymaker | 68:b9cfd27987ac | 68 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 69 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 70 | |
AhmedPlaymaker | 41:4edac50f010d | 71 | Vector2D Snake::get_pos() |
AhmedPlaymaker | 7:48ba87cd79b5 | 72 | { |
AhmedPlaymaker | 37:ee47699915b8 | 73 | for(int i=1; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 74 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 75 | Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 76 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 77 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 78 | } |
AhmedPlaymaker | 41:4edac50f010d | 79 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 80 | Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position. |
AhmedPlaymaker | 63:205f0ca48473 | 81 | return snakepos; |
AhmedPlaymaker | 37:ee47699915b8 | 82 | } |
AhmedPlaymaker | 24:1c118b071430 | 83 | Vector2D snakepos = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 84 | return snakepos; |
AhmedPlaymaker | 7:48ba87cd79b5 | 85 | //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); |
AhmedPlaymaker | 13:9785f2404045 | 86 | } |
AhmedPlaymaker | 13:9785f2404045 | 87 | |
AhmedPlaymaker | 41:4edac50f010d | 88 | Vector2D Snake::get_pos_before1() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 89 | { |
AhmedPlaymaker | 37:ee47699915b8 | 90 | for(int i=2; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 91 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 92 | Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 93 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 94 | } |
AhmedPlaymaker | 13:9785f2404045 | 95 | } |
AhmedPlaymaker | 41:4edac50f010d | 96 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 97 | Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position. |
AhmedPlaymaker | 63:205f0ca48473 | 98 | return snakepos_b1; |
AhmedPlaymaker | 37:ee47699915b8 | 99 | } |
AhmedPlaymaker | 24:1c118b071430 | 100 | Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 101 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 102 | //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y); |
AhmedPlaymaker | 13:9785f2404045 | 103 | } |
AhmedPlaymaker | 13:9785f2404045 | 104 | |
AhmedPlaymaker | 41:4edac50f010d | 105 | Vector2D Snake::get_pos_before2() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 106 | { |
AhmedPlaymaker | 37:ee47699915b8 | 107 | for(int i=3; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 108 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 109 | Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 110 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 111 | } |
AhmedPlaymaker | 13:9785f2404045 | 112 | } |
AhmedPlaymaker | 41:4edac50f010d | 113 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 114 | Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position. |
AhmedPlaymaker | 63:205f0ca48473 | 115 | return snakepos_b2; |
AhmedPlaymaker | 37:ee47699915b8 | 116 | } |
AhmedPlaymaker | 24:1c118b071430 | 117 | Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 118 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 119 | //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y); |
AhmedPlaymaker | 13:9785f2404045 | 120 | } |
AhmedPlaymaker | 13:9785f2404045 | 121 | |
AhmedPlaymaker | 41:4edac50f010d | 122 | Vector2D Snake::get_pos_before3() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 123 | { |
AhmedPlaymaker | 37:ee47699915b8 | 124 | for(int i=4; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 125 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 126 | Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 127 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 128 | } |
AhmedPlaymaker | 13:9785f2404045 | 129 | } |
AhmedPlaymaker | 41:4edac50f010d | 130 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 131 | Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position. |
AhmedPlaymaker | 63:205f0ca48473 | 132 | return snakepos_b3; |
AhmedPlaymaker | 37:ee47699915b8 | 133 | } |
AhmedPlaymaker | 24:1c118b071430 | 134 | Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 135 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 136 | //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y); |
AhmedPlaymaker | 13:9785f2404045 | 137 | } |
AhmedPlaymaker | 13:9785f2404045 | 138 | |
AhmedPlaymaker | 41:4edac50f010d | 139 | Vector2D Snake::get_pos_before4() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 140 | { |
AhmedPlaymaker | 37:ee47699915b8 | 141 | for(int i=5; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 142 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 143 | Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 144 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 145 | } |
AhmedPlaymaker | 13:9785f2404045 | 146 | } |
AhmedPlaymaker | 41:4edac50f010d | 147 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 148 | Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position. |
AhmedPlaymaker | 63:205f0ca48473 | 149 | return snakepos_b4; |
AhmedPlaymaker | 37:ee47699915b8 | 150 | } |
AhmedPlaymaker | 24:1c118b071430 | 151 | Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 152 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 153 | //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y); |
AhmedPlaymaker | 13:9785f2404045 | 154 | } |
AhmedPlaymaker | 13:9785f2404045 | 155 | |
AhmedPlaymaker | 41:4edac50f010d | 156 | Vector2D Snake::get_pos_before5() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 157 | { |
AhmedPlaymaker | 37:ee47699915b8 | 158 | for(int i=6; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 159 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 160 | Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 161 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 162 | } |
AhmedPlaymaker | 13:9785f2404045 | 163 | } |
AhmedPlaymaker | 41:4edac50f010d | 164 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 165 | Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position. |
AhmedPlaymaker | 63:205f0ca48473 | 166 | return snakepos_b5; |
AhmedPlaymaker | 37:ee47699915b8 | 167 | } |
AhmedPlaymaker | 24:1c118b071430 | 168 | Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 169 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 170 | //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y); |
AhmedPlaymaker | 13:9785f2404045 | 171 | } |
AhmedPlaymaker | 13:9785f2404045 | 172 | |
AhmedPlaymaker | 41:4edac50f010d | 173 | Vector2D Snake::get_pos_before6() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 174 | { |
AhmedPlaymaker | 37:ee47699915b8 | 175 | for(int i=7; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 176 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 177 | Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 178 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 179 | } |
AhmedPlaymaker | 13:9785f2404045 | 180 | } |
AhmedPlaymaker | 41:4edac50f010d | 181 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 182 | Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position. |
AhmedPlaymaker | 63:205f0ca48473 | 183 | return snakepos_b6; |
AhmedPlaymaker | 37:ee47699915b8 | 184 | } |
AhmedPlaymaker | 24:1c118b071430 | 185 | Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 186 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 187 | //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y); |
AhmedPlaymaker | 13:9785f2404045 | 188 | } |
AhmedPlaymaker | 13:9785f2404045 | 189 | |
AhmedPlaymaker | 41:4edac50f010d | 190 | Vector2D Snake::get_pos_before7() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 191 | { |
AhmedPlaymaker | 37:ee47699915b8 | 192 | for(int i=8; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 193 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 194 | Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 195 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 196 | } |
AhmedPlaymaker | 13:9785f2404045 | 197 | } |
AhmedPlaymaker | 41:4edac50f010d | 198 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 199 | Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 200 | return snakepos_b7; |
AhmedPlaymaker | 37:ee47699915b8 | 201 | } |
AhmedPlaymaker | 24:1c118b071430 | 202 | Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 203 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 204 | //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y); |
AhmedPlaymaker | 7:48ba87cd79b5 | 205 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 206 | |
AhmedPlaymaker | 41:4edac50f010d | 207 | Vector2D Snake::get_pos_before8() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 208 | { |
AhmedPlaymaker | 37:ee47699915b8 | 209 | for(int i=9; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 210 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 211 | Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 212 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 213 | } |
AhmedPlaymaker | 13:9785f2404045 | 214 | } |
AhmedPlaymaker | 41:4edac50f010d | 215 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 216 | Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 217 | return snakepos_b8; |
AhmedPlaymaker | 37:ee47699915b8 | 218 | } |
AhmedPlaymaker | 24:1c118b071430 | 219 | Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 220 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 221 | //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y); |
AhmedPlaymaker | 13:9785f2404045 | 222 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 223 | |
AhmedPlaymaker | 41:4edac50f010d | 224 | Vector2D Snake::get_pos_before9() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 225 | { |
AhmedPlaymaker | 37:ee47699915b8 | 226 | for(int i=10; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 227 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 228 | Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 229 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 230 | } |
AhmedPlaymaker | 13:9785f2404045 | 231 | } |
AhmedPlaymaker | 41:4edac50f010d | 232 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 233 | Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 234 | return snakepos_b9; |
AhmedPlaymaker | 37:ee47699915b8 | 235 | } |
AhmedPlaymaker | 24:1c118b071430 | 236 | Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 237 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 238 | //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y); |
AhmedPlaymaker | 13:9785f2404045 | 239 | } |
AhmedPlaymaker | 13:9785f2404045 | 240 | |
AhmedPlaymaker | 41:4edac50f010d | 241 | void Snake::update(Direction d, int* b) |
AhmedPlaymaker | 13:9785f2404045 | 242 | { |
AhmedPlaymaker | 63:205f0ca48473 | 243 | if(_length >= 10) { |
AhmedPlaymaker | 63:205f0ca48473 | 244 | _length = 10; //to stop the snake length virtually at 10 when it goes past it. |
AhmedPlaymaker | 63:205f0ca48473 | 245 | } |
AhmedPlaymaker | 63:205f0ca48473 | 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 | 43:233f93860d08 | 248 | //the b[i] makes sure that the snake beed doesn't move if that beed is deactivated by colliding with a barrier. b[i] also signifies the specific beed number by i. |
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 | 63:205f0ca48473 | 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 | 63:205f0ca48473 | 255 | _x[i]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 256 | } |
AhmedPlaymaker | 21:e41126528cc6 | 257 | } |
AhmedPlaymaker | 21:e41126528cc6 | 258 | } |
AhmedPlaymaker | 63:205f0ca48473 | 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 | 63:205f0ca48473 | 261 | if((_length == i+1)&&(b[i] == 1)) { |
AhmedPlaymaker | 63:205f0ca48473 | 262 | |
AhmedPlaymaker | 63:205f0ca48473 | 263 | if (d == E) { |
AhmedPlaymaker | 63:205f0ca48473 | 264 | _x[i]+= _speed; |
AhmedPlaymaker | 22:ee698f66146f | 265 | } |
AhmedPlaymaker | 63:205f0ca48473 | 266 | |
AhmedPlaymaker | 63:205f0ca48473 | 267 | if (d == W) { |
AhmedPlaymaker | 63:205f0ca48473 | 268 | _x[i]-= _speed; |
AhmedPlaymaker | 63:205f0ca48473 | 269 | } |
AhmedPlaymaker | 63:205f0ca48473 | 270 | |
AhmedPlaymaker | 63:205f0ca48473 | 271 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 272 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 273 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 274 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 275 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 276 | for(int i=2; i<=15; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 277 | |
AhmedPlaymaker | 63:205f0ca48473 | 278 | if(_length < i) { |
AhmedPlaymaker | 63:205f0ca48473 | 279 | _x[i-1] = _x[i-2]; |
AhmedPlaymaker | 63:205f0ca48473 | 280 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 281 | } |
AhmedPlaymaker | 63:205f0ca48473 | 282 | |
AhmedPlaymaker | 25:e827f1a8fadc | 283 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 22:ee698f66146f | 284 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 285 | |
AhmedPlaymaker | 63:205f0ca48473 | 286 | if (_x[i] <= 0) { |
AhmedPlaymaker | 63:205f0ca48473 | 287 | _x[i] = 0; |
AhmedPlaymaker | 63:205f0ca48473 | 288 | } |
AhmedPlaymaker | 63:205f0ca48473 | 289 | |
AhmedPlaymaker | 63:205f0ca48473 | 290 | if (_x[i] > 81) { |
AhmedPlaymaker | 63:205f0ca48473 | 291 | _x[i] = 81; |
AhmedPlaymaker | 63:205f0ca48473 | 292 | } |
AhmedPlaymaker | 8:890b986b16a4 | 293 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 294 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 295 |