![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.cpp@51:387249f9b333, 2019-04-26 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Fri Apr 26 18:30:25 2019 +0000
- Revision:
- 51:387249f9b333
- Parent:
- Snake/Snake.cpp@44:cd10d07ea1e5
- Child:
- 53:527cf297b088
Sorted my game classes into folders to make it more organized
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 | 24:1c118b071430 | 43 | |
AhmedPlaymaker | 37:ee47699915b8 | 44 | for(int a=1; a<=10; a++) { |
AhmedPlaymaker | 41:4edac50f010d | 45 | if(_length == a) { |
AhmedPlaymaker | 41:4edac50f010d | 46 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 47 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 41:4edac50f010d | 48 | } |
AhmedPlaymaker | 41:4edac50f010d | 49 | } |
AhmedPlaymaker | 41:4edac50f010d | 50 | } |
AhmedPlaymaker | 41:4edac50f010d | 51 | if(_length >= 10) { |
AhmedPlaymaker | 41:4edac50f010d | 52 | for(int a=1; a<=10; a++) { |
AhmedPlaymaker | 24:1c118b071430 | 53 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 54 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 24:1c118b071430 | 55 | } |
AhmedPlaymaker | 24:1c118b071430 | 56 | } |
AhmedPlaymaker | 8:890b986b16a4 | 57 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 58 | } |
AhmedPlaymaker | 19:05cc9f801468 | 59 | |
AhmedPlaymaker | 24:1c118b071430 | 60 | |
AhmedPlaymaker | 41:4edac50f010d | 61 | Vector2D Snake::get_pos() |
AhmedPlaymaker | 7:48ba87cd79b5 | 62 | { |
AhmedPlaymaker | 37:ee47699915b8 | 63 | for(int i=1; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 64 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 65 | Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 66 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 67 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 68 | } |
AhmedPlaymaker | 41:4edac50f010d | 69 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 70 | Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 71 | return snakepos; |
AhmedPlaymaker | 37:ee47699915b8 | 72 | } |
AhmedPlaymaker | 24:1c118b071430 | 73 | Vector2D snakepos = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 74 | return snakepos; |
AhmedPlaymaker | 7:48ba87cd79b5 | 75 | //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); |
AhmedPlaymaker | 13:9785f2404045 | 76 | } |
AhmedPlaymaker | 13:9785f2404045 | 77 | |
AhmedPlaymaker | 41:4edac50f010d | 78 | Vector2D Snake::get_pos_before1() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 79 | { |
AhmedPlaymaker | 37:ee47699915b8 | 80 | for(int i=2; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 81 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 82 | Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 83 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 84 | } |
AhmedPlaymaker | 13:9785f2404045 | 85 | } |
AhmedPlaymaker | 41:4edac50f010d | 86 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 87 | Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 88 | return snakepos_b1; |
AhmedPlaymaker | 37:ee47699915b8 | 89 | } |
AhmedPlaymaker | 24:1c118b071430 | 90 | Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 91 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 92 | //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y); |
AhmedPlaymaker | 13:9785f2404045 | 93 | } |
AhmedPlaymaker | 13:9785f2404045 | 94 | |
AhmedPlaymaker | 41:4edac50f010d | 95 | Vector2D Snake::get_pos_before2() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 96 | { |
AhmedPlaymaker | 37:ee47699915b8 | 97 | for(int i=3; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 98 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 99 | Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 100 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 101 | } |
AhmedPlaymaker | 13:9785f2404045 | 102 | } |
AhmedPlaymaker | 41:4edac50f010d | 103 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 104 | Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 105 | return snakepos_b2; |
AhmedPlaymaker | 37:ee47699915b8 | 106 | } |
AhmedPlaymaker | 24:1c118b071430 | 107 | Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 108 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 109 | //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y); |
AhmedPlaymaker | 13:9785f2404045 | 110 | } |
AhmedPlaymaker | 13:9785f2404045 | 111 | |
AhmedPlaymaker | 41:4edac50f010d | 112 | Vector2D Snake::get_pos_before3() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 113 | { |
AhmedPlaymaker | 37:ee47699915b8 | 114 | for(int i=4; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 115 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 116 | Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 117 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 118 | } |
AhmedPlaymaker | 13:9785f2404045 | 119 | } |
AhmedPlaymaker | 41:4edac50f010d | 120 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 121 | Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 122 | return snakepos_b3; |
AhmedPlaymaker | 37:ee47699915b8 | 123 | } |
AhmedPlaymaker | 24:1c118b071430 | 124 | Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 125 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 126 | //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y); |
AhmedPlaymaker | 13:9785f2404045 | 127 | } |
AhmedPlaymaker | 13:9785f2404045 | 128 | |
AhmedPlaymaker | 41:4edac50f010d | 129 | Vector2D Snake::get_pos_before4() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 130 | { |
AhmedPlaymaker | 37:ee47699915b8 | 131 | for(int i=5; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 132 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 133 | Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 134 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 135 | } |
AhmedPlaymaker | 13:9785f2404045 | 136 | } |
AhmedPlaymaker | 41:4edac50f010d | 137 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 138 | Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 139 | return snakepos_b4; |
AhmedPlaymaker | 37:ee47699915b8 | 140 | } |
AhmedPlaymaker | 24:1c118b071430 | 141 | Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 142 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 143 | //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y); |
AhmedPlaymaker | 13:9785f2404045 | 144 | } |
AhmedPlaymaker | 13:9785f2404045 | 145 | |
AhmedPlaymaker | 41:4edac50f010d | 146 | Vector2D Snake::get_pos_before5() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 147 | { |
AhmedPlaymaker | 37:ee47699915b8 | 148 | for(int i=6; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 149 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 150 | Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 151 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 152 | } |
AhmedPlaymaker | 13:9785f2404045 | 153 | } |
AhmedPlaymaker | 41:4edac50f010d | 154 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 155 | Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 156 | return snakepos_b5; |
AhmedPlaymaker | 37:ee47699915b8 | 157 | } |
AhmedPlaymaker | 24:1c118b071430 | 158 | Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 159 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 160 | //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y); |
AhmedPlaymaker | 13:9785f2404045 | 161 | } |
AhmedPlaymaker | 13:9785f2404045 | 162 | |
AhmedPlaymaker | 41:4edac50f010d | 163 | Vector2D Snake::get_pos_before6() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 164 | { |
AhmedPlaymaker | 37:ee47699915b8 | 165 | for(int i=7; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 166 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 167 | Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 168 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 169 | } |
AhmedPlaymaker | 13:9785f2404045 | 170 | } |
AhmedPlaymaker | 41:4edac50f010d | 171 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 172 | Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 173 | return snakepos_b6; |
AhmedPlaymaker | 37:ee47699915b8 | 174 | } |
AhmedPlaymaker | 24:1c118b071430 | 175 | Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 176 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 177 | //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y); |
AhmedPlaymaker | 13:9785f2404045 | 178 | } |
AhmedPlaymaker | 13:9785f2404045 | 179 | |
AhmedPlaymaker | 41:4edac50f010d | 180 | Vector2D Snake::get_pos_before7() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 181 | { |
AhmedPlaymaker | 37:ee47699915b8 | 182 | for(int i=8; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 183 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 184 | Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 185 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 186 | } |
AhmedPlaymaker | 13:9785f2404045 | 187 | } |
AhmedPlaymaker | 41:4edac50f010d | 188 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 189 | Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 190 | return snakepos_b7; |
AhmedPlaymaker | 37:ee47699915b8 | 191 | } |
AhmedPlaymaker | 24:1c118b071430 | 192 | Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 193 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 194 | //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y); |
AhmedPlaymaker | 7:48ba87cd79b5 | 195 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 196 | |
AhmedPlaymaker | 41:4edac50f010d | 197 | Vector2D Snake::get_pos_before8() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 198 | { |
AhmedPlaymaker | 37:ee47699915b8 | 199 | for(int i=9; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 200 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 201 | Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 202 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 203 | } |
AhmedPlaymaker | 13:9785f2404045 | 204 | } |
AhmedPlaymaker | 41:4edac50f010d | 205 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 206 | Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 207 | return snakepos_b8; |
AhmedPlaymaker | 37:ee47699915b8 | 208 | } |
AhmedPlaymaker | 24:1c118b071430 | 209 | Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 210 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 211 | //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y); |
AhmedPlaymaker | 13:9785f2404045 | 212 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 213 | |
AhmedPlaymaker | 41:4edac50f010d | 214 | Vector2D Snake::get_pos_before9() //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 215 | { |
AhmedPlaymaker | 37:ee47699915b8 | 216 | for(int i=10; i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 217 | if(_length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 218 | Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 219 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 220 | } |
AhmedPlaymaker | 13:9785f2404045 | 221 | } |
AhmedPlaymaker | 41:4edac50f010d | 222 | if(_length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 223 | Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 224 | return snakepos_b9; |
AhmedPlaymaker | 37:ee47699915b8 | 225 | } |
AhmedPlaymaker | 24:1c118b071430 | 226 | Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 227 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 228 | //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y); |
AhmedPlaymaker | 13:9785f2404045 | 229 | } |
AhmedPlaymaker | 13:9785f2404045 | 230 | |
AhmedPlaymaker | 41:4edac50f010d | 231 | void Snake::update(Direction d, int* b) |
AhmedPlaymaker | 13:9785f2404045 | 232 | { |
AhmedPlaymaker | 41:4edac50f010d | 233 | if(_length >= 10) {_length = 10;} //to stop the snake length virtually at 10 when it goes past it. |
AhmedPlaymaker | 41:4edac50f010d | 234 | |
AhmedPlaymaker | 25:e827f1a8fadc | 235 | //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 | 236 | //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 | 237 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 37:ee47699915b8 | 238 | if((_length > i+1)&&(_x[i] != _x[i+1])) { |
AhmedPlaymaker | 22:ee698f66146f | 239 | if ((_x[i] > _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 240 | _x[i]-=_speed; |
AhmedPlaymaker | 8:890b986b16a4 | 241 | } |
AhmedPlaymaker | 22:ee698f66146f | 242 | if ((_x[i] < _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 243 | _x[i]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 244 | } |
AhmedPlaymaker | 21:e41126528cc6 | 245 | } |
AhmedPlaymaker | 21:e41126528cc6 | 246 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 247 | //this makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 22:ee698f66146f | 248 | for(int i=14; i>=0; i--) { |
AhmedPlaymaker | 37:ee47699915b8 | 249 | if((_length == i+1)&&(b[i] == 1)) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 250 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 251 | if (d == E) {_x[i]+= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 252 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 253 | if (d == W) {_x[i]-= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 254 | |
AhmedPlaymaker | 22:ee698f66146f | 255 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 256 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 257 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 258 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 259 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 260 | for(int i=2; i<=15; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 261 | |
AhmedPlaymaker | 37:ee47699915b8 | 262 | if(_length < i) {_x[i-1] = _x[i-2];} |
AhmedPlaymaker | 9:d1d79d4ee673 | 263 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 264 | |
AhmedPlaymaker | 25:e827f1a8fadc | 265 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 22:ee698f66146f | 266 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 267 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 268 | if (_x[i] <= 0) {_x[i] = 0;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 269 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 270 | if (_x[i] > 81) {_x[i] = 81;} |
AhmedPlaymaker | 8:890b986b16a4 | 271 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 272 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 273 |