![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
Snake/Snake.cpp@36:dfdd619874ae, 2019-04-18 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu Apr 18 06:19:33 2019 +0000
- Revision:
- 36:dfdd619874ae
- Parent:
- 33:249cf423fb18
- Child:
- 37:ee47699915b8
Minor Bug fixes, and performance improvements. #UpdateVibes
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 | 7:48ba87cd79b5 | 22 | |
AhmedPlaymaker | 18:b391caa5754c | 23 | _speed = 1;// 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 | 7:48ba87cd79b5 | 28 | |
AhmedPlaymaker | 20:1e6338403427 | 29 | int Snake::draw(Gamepad &pad, N5110 &lcd, int length, int level) |
AhmedPlaymaker | 33:249cf423fb18 | 30 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 31 | if(m == 0){ |
AhmedPlaymaker | 21:e41126528cc6 | 32 | _x[0] = WIDTH/2; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 33:249cf423fb18 | 33 | _y[0] = HEIGHT - 3; |
AhmedPlaymaker | 22:ee698f66146f | 34 | |
AhmedPlaymaker | 22:ee698f66146f | 35 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 22:ee698f66146f | 36 | _x[i+1] = _x[i]; |
AhmedPlaymaker | 22:ee698f66146f | 37 | _y[i+1] = _y[i] - 3; |
AhmedPlaymaker | 22:ee698f66146f | 38 | b[i] = 1; |
AhmedPlaymaker | 22:ee698f66146f | 39 | } |
AhmedPlaymaker | 33:249cf423fb18 | 40 | b[14] = 1; |
AhmedPlaymaker | 33:249cf423fb18 | 41 | m = m+1; |
AhmedPlaymaker | 22:ee698f66146f | 42 | } |
AhmedPlaymaker | 21:e41126528cc6 | 43 | //printf("SPRITE %d %d \n", _x[0], _y[0]); |
AhmedPlaymaker | 8:890b986b16a4 | 44 | if(length == 0) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 45 | pad.init(); |
AhmedPlaymaker | 16:7b474f873683 | 46 | while ((pad.check_event(Gamepad::BACK_PRESSED) == false)) { |
AhmedPlaymaker | 16:7b474f873683 | 47 | lcd.clear(); |
AhmedPlaymaker | 16:7b474f873683 | 48 | lcd.printString("Game",33,1); |
AhmedPlaymaker | 16:7b474f873683 | 49 | lcd.printString("Over",33,3); |
AhmedPlaymaker | 16:7b474f873683 | 50 | lcd.printString("Press Back",14,5); //Function used to promt the user to Restart. |
AhmedPlaymaker | 16:7b474f873683 | 51 | lcd.refresh(); |
AhmedPlaymaker | 16:7b474f873683 | 52 | wait(0.1); |
AhmedPlaymaker | 16:7b474f873683 | 53 | } |
AhmedPlaymaker | 16:7b474f873683 | 54 | NVIC_SystemReset(); //Software Reset. //change this condition to loosing screen. |
AhmedPlaymaker | 8:890b986b16a4 | 55 | } |
AhmedPlaymaker | 24:1c118b071430 | 56 | |
AhmedPlaymaker | 24:1c118b071430 | 57 | for(int a=1; a<=15; a++) { |
AhmedPlaymaker | 24:1c118b071430 | 58 | if(length == a) { |
AhmedPlaymaker | 24:1c118b071430 | 59 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 60 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 24:1c118b071430 | 61 | } |
AhmedPlaymaker | 24:1c118b071430 | 62 | } |
AhmedPlaymaker | 8:890b986b16a4 | 63 | } |
AhmedPlaymaker | 24:1c118b071430 | 64 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 65 | if(length >= 16) { |
AhmedPlaymaker | 17:54e13faeaf64 | 66 | lcd.clear(); |
AhmedPlaymaker | 17:54e13faeaf64 | 67 | lcd.printString("Level Complete",0,1); |
AhmedPlaymaker | 24:1c118b071430 | 68 | lcd.printString("Press Start",10,3); |
AhmedPlaymaker | 24:1c118b071430 | 69 | lcd.printString("to Proceed",12,4); |
AhmedPlaymaker | 17:54e13faeaf64 | 70 | lcd.refresh(); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 71 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
AhmedPlaymaker | 20:1e6338403427 | 72 | pad.leds_on(); |
AhmedPlaymaker | 24:1c118b071430 | 73 | pad.tone(rand()%1001,0.1); //helps to set max freq for random sound |
AhmedPlaymaker | 20:1e6338403427 | 74 | wait(0.1); |
AhmedPlaymaker | 20:1e6338403427 | 75 | pad.leds_off(); |
AhmedPlaymaker | 20:1e6338403427 | 76 | wait(0.1); |
AhmedPlaymaker | 20:1e6338403427 | 77 | } |
AhmedPlaymaker | 20:1e6338403427 | 78 | lcd.clear(); |
AhmedPlaymaker | 20:1e6338403427 | 79 | sprintf(bufferlevel,"%d",level); |
AhmedPlaymaker | 20:1e6338403427 | 80 | lcd.printString("Level:",25,1); |
AhmedPlaymaker | 20:1e6338403427 | 81 | lcd.printString(bufferlevel,40,3); |
AhmedPlaymaker | 20:1e6338403427 | 82 | lcd.refresh(); |
AhmedPlaymaker | 20:1e6338403427 | 83 | wait(2); |
AhmedPlaymaker | 17:54e13faeaf64 | 84 | length = 3; |
AhmedPlaymaker | 17:54e13faeaf64 | 85 | } |
AhmedPlaymaker | 17:54e13faeaf64 | 86 | return length; |
AhmedPlaymaker | 7:48ba87cd79b5 | 87 | } |
AhmedPlaymaker | 19:05cc9f801468 | 88 | |
AhmedPlaymaker | 24:1c118b071430 | 89 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 90 | Vector2D Snake::get_pos(int length) |
AhmedPlaymaker | 7:48ba87cd79b5 | 91 | { |
AhmedPlaymaker | 24:1c118b071430 | 92 | for(int i=1; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 93 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 94 | Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 95 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 96 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 97 | } |
AhmedPlaymaker | 24:1c118b071430 | 98 | Vector2D snakepos = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 99 | return snakepos; |
AhmedPlaymaker | 7:48ba87cd79b5 | 100 | //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); |
AhmedPlaymaker | 13:9785f2404045 | 101 | } |
AhmedPlaymaker | 13:9785f2404045 | 102 | |
AhmedPlaymaker | 13:9785f2404045 | 103 | Vector2D Snake::get_pos_before1(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 104 | { |
AhmedPlaymaker | 24:1c118b071430 | 105 | for(int i=2; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 106 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 107 | Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 108 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 109 | } |
AhmedPlaymaker | 13:9785f2404045 | 110 | } |
AhmedPlaymaker | 24:1c118b071430 | 111 | Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 112 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 113 | //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y); |
AhmedPlaymaker | 13:9785f2404045 | 114 | } |
AhmedPlaymaker | 13:9785f2404045 | 115 | |
AhmedPlaymaker | 13:9785f2404045 | 116 | Vector2D Snake::get_pos_before2(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 117 | { |
AhmedPlaymaker | 24:1c118b071430 | 118 | for(int i=3; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 119 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 120 | Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 121 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 122 | } |
AhmedPlaymaker | 13:9785f2404045 | 123 | } |
AhmedPlaymaker | 24:1c118b071430 | 124 | Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 125 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 126 | //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y); |
AhmedPlaymaker | 13:9785f2404045 | 127 | } |
AhmedPlaymaker | 13:9785f2404045 | 128 | |
AhmedPlaymaker | 13:9785f2404045 | 129 | Vector2D Snake::get_pos_before3(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 130 | { |
AhmedPlaymaker | 24:1c118b071430 | 131 | for(int i=4; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 132 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 133 | Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 134 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 135 | } |
AhmedPlaymaker | 13:9785f2404045 | 136 | } |
AhmedPlaymaker | 24:1c118b071430 | 137 | Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 138 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 139 | //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y); |
AhmedPlaymaker | 13:9785f2404045 | 140 | } |
AhmedPlaymaker | 13:9785f2404045 | 141 | |
AhmedPlaymaker | 13:9785f2404045 | 142 | Vector2D Snake::get_pos_before4(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 143 | { |
AhmedPlaymaker | 24:1c118b071430 | 144 | for(int i=5; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 145 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 146 | Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 147 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 148 | } |
AhmedPlaymaker | 13:9785f2404045 | 149 | } |
AhmedPlaymaker | 24:1c118b071430 | 150 | Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 151 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 152 | //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y); |
AhmedPlaymaker | 13:9785f2404045 | 153 | } |
AhmedPlaymaker | 13:9785f2404045 | 154 | |
AhmedPlaymaker | 13:9785f2404045 | 155 | Vector2D Snake::get_pos_before5(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 156 | { |
AhmedPlaymaker | 24:1c118b071430 | 157 | for(int i=6; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 158 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 159 | Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 160 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 161 | } |
AhmedPlaymaker | 13:9785f2404045 | 162 | } |
AhmedPlaymaker | 24:1c118b071430 | 163 | Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 164 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 165 | //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y); |
AhmedPlaymaker | 13:9785f2404045 | 166 | } |
AhmedPlaymaker | 13:9785f2404045 | 167 | |
AhmedPlaymaker | 13:9785f2404045 | 168 | Vector2D Snake::get_pos_before6(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 169 | { |
AhmedPlaymaker | 24:1c118b071430 | 170 | for(int i=7; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 171 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 172 | Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 173 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 174 | } |
AhmedPlaymaker | 13:9785f2404045 | 175 | } |
AhmedPlaymaker | 24:1c118b071430 | 176 | Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 177 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 178 | //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y); |
AhmedPlaymaker | 13:9785f2404045 | 179 | } |
AhmedPlaymaker | 13:9785f2404045 | 180 | |
AhmedPlaymaker | 13:9785f2404045 | 181 | Vector2D Snake::get_pos_before7(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 182 | { |
AhmedPlaymaker | 24:1c118b071430 | 183 | for(int i=8; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 184 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 185 | Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 186 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 187 | } |
AhmedPlaymaker | 13:9785f2404045 | 188 | } |
AhmedPlaymaker | 24:1c118b071430 | 189 | Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 190 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 191 | //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y); |
AhmedPlaymaker | 7:48ba87cd79b5 | 192 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 193 | |
AhmedPlaymaker | 13:9785f2404045 | 194 | Vector2D Snake::get_pos_before8(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 195 | { |
AhmedPlaymaker | 24:1c118b071430 | 196 | for(int i=9; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 197 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 198 | Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 199 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 200 | } |
AhmedPlaymaker | 13:9785f2404045 | 201 | } |
AhmedPlaymaker | 24:1c118b071430 | 202 | Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 203 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 204 | //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y); |
AhmedPlaymaker | 13:9785f2404045 | 205 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 206 | |
AhmedPlaymaker | 13:9785f2404045 | 207 | Vector2D Snake::get_pos_before9(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 208 | { |
AhmedPlaymaker | 24:1c118b071430 | 209 | for(int i=10; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 210 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 211 | Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 212 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 213 | } |
AhmedPlaymaker | 13:9785f2404045 | 214 | } |
AhmedPlaymaker | 24:1c118b071430 | 215 | Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 216 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 217 | //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y); |
AhmedPlaymaker | 13:9785f2404045 | 218 | } |
AhmedPlaymaker | 13:9785f2404045 | 219 | |
AhmedPlaymaker | 13:9785f2404045 | 220 | Vector2D Snake::get_pos_before10(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 221 | { |
AhmedPlaymaker | 24:1c118b071430 | 222 | for(int i=11; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 223 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 224 | Vector2D snakepos_b10 = {_x[i-11],_y[i-11]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 225 | return snakepos_b10; |
AhmedPlaymaker | 24:1c118b071430 | 226 | } |
AhmedPlaymaker | 13:9785f2404045 | 227 | } |
AhmedPlaymaker | 24:1c118b071430 | 228 | Vector2D snakepos_b10 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 229 | return snakepos_b10; |
AhmedPlaymaker | 24:1c118b071430 | 230 | //printf("snakepos_b10 from player = %f %f \n", snakepos_b10.x, snakepos_b10.y); |
AhmedPlaymaker | 13:9785f2404045 | 231 | } |
AhmedPlaymaker | 13:9785f2404045 | 232 | |
AhmedPlaymaker | 13:9785f2404045 | 233 | Vector2D Snake::get_pos_before11(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 234 | { |
AhmedPlaymaker | 24:1c118b071430 | 235 | for(int i=12; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 236 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 237 | Vector2D snakepos_b11 = {_x[i-12],_y[i-12]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 238 | return snakepos_b11; |
AhmedPlaymaker | 24:1c118b071430 | 239 | } |
AhmedPlaymaker | 13:9785f2404045 | 240 | } |
AhmedPlaymaker | 24:1c118b071430 | 241 | Vector2D snakepos_b11 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 242 | return snakepos_b11; |
AhmedPlaymaker | 24:1c118b071430 | 243 | //printf("snakepos_b11 from player = %f %f \n", snakepos_b11.x, snakepos_b11.y); |
AhmedPlaymaker | 13:9785f2404045 | 244 | } |
AhmedPlaymaker | 13:9785f2404045 | 245 | |
AhmedPlaymaker | 13:9785f2404045 | 246 | Vector2D Snake::get_pos_before12(int length) //this sends data of following sprites. |
AhmedPlaymaker | 7:48ba87cd79b5 | 247 | { |
AhmedPlaymaker | 24:1c118b071430 | 248 | for(int i=13; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 249 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 250 | Vector2D snakepos_b12 = {_x[i-13],_y[i-13]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 251 | return snakepos_b12; |
AhmedPlaymaker | 24:1c118b071430 | 252 | } |
AhmedPlaymaker | 13:9785f2404045 | 253 | } |
AhmedPlaymaker | 24:1c118b071430 | 254 | Vector2D snakepos_b12 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 255 | return snakepos_b12; |
AhmedPlaymaker | 24:1c118b071430 | 256 | //printf("snakepos_b12 from player = %f %f \n", snakepos_b12.x, snakepos_b12.y); |
AhmedPlaymaker | 13:9785f2404045 | 257 | } |
AhmedPlaymaker | 13:9785f2404045 | 258 | |
AhmedPlaymaker | 13:9785f2404045 | 259 | Vector2D Snake::get_pos_before13(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 260 | { |
AhmedPlaymaker | 24:1c118b071430 | 261 | for(int i=14; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 262 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 263 | Vector2D snakepos_b13 = {_x[i-14],_y[i-14]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 264 | return snakepos_b13; |
AhmedPlaymaker | 24:1c118b071430 | 265 | } |
AhmedPlaymaker | 13:9785f2404045 | 266 | } |
AhmedPlaymaker | 24:1c118b071430 | 267 | Vector2D snakepos_b13 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 268 | return snakepos_b13; |
AhmedPlaymaker | 24:1c118b071430 | 269 | //printf("snakepos_b13 from player = %f %f \n", snakepos_b13.x, snakepos_b13.y); |
AhmedPlaymaker | 13:9785f2404045 | 270 | } |
AhmedPlaymaker | 13:9785f2404045 | 271 | |
AhmedPlaymaker | 13:9785f2404045 | 272 | Vector2D Snake::get_pos_before14(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 273 | { |
AhmedPlaymaker | 24:1c118b071430 | 274 | for(int i=15; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 275 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 276 | Vector2D snakepos_b14 = {_x[i-15],_y[i-15]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 277 | return snakepos_b14; |
AhmedPlaymaker | 24:1c118b071430 | 278 | } |
AhmedPlaymaker | 13:9785f2404045 | 279 | } |
AhmedPlaymaker | 24:1c118b071430 | 280 | Vector2D snakepos_b14 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 281 | return snakepos_b14; |
AhmedPlaymaker | 24:1c118b071430 | 282 | //printf("snakepos_b14 from player = %f %f \n", snakepos_b14.x, snakepos_b14.y); |
AhmedPlaymaker | 13:9785f2404045 | 283 | } |
AhmedPlaymaker | 13:9785f2404045 | 284 | |
AhmedPlaymaker | 18:b391caa5754c | 285 | |
AhmedPlaymaker | 13:9785f2404045 | 286 | |
AhmedPlaymaker | 36:dfdd619874ae | 287 | void Snake::update(Direction d,int length, int speed, int* b) |
AhmedPlaymaker | 13:9785f2404045 | 288 | { |
AhmedPlaymaker | 13:9785f2404045 | 289 | _speed = speed; //Speed changes depending on how much you push the joystick.(As Of Now) |
AhmedPlaymaker | 8:890b986b16a4 | 290 | |
AhmedPlaymaker | 25:e827f1a8fadc | 291 | //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 | 292 | //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 | 293 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 294 | if((length > i+1)&&(_x[i] != _x[i+1])) { |
AhmedPlaymaker | 22:ee698f66146f | 295 | if ((_x[i] > _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 296 | _x[i]-=_speed; |
AhmedPlaymaker | 8:890b986b16a4 | 297 | } |
AhmedPlaymaker | 22:ee698f66146f | 298 | if ((_x[i] < _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 299 | _x[i]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 300 | } |
AhmedPlaymaker | 21:e41126528cc6 | 301 | } |
AhmedPlaymaker | 21:e41126528cc6 | 302 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 303 | //this makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 22:ee698f66146f | 304 | for(int i=14; i>=0; i--) { |
AhmedPlaymaker | 22:ee698f66146f | 305 | if((length == i+1)&&(b[i] == 1)) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 306 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 307 | if (d == E) {_x[i]+= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 308 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 309 | if (d == W) {_x[i]-= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 310 | |
AhmedPlaymaker | 22:ee698f66146f | 311 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 312 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 313 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 314 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 315 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 316 | for(int i=2; i<=15; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 317 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 318 | if(length < i) {_x[i-1] = _x[i-2];} |
AhmedPlaymaker | 9:d1d79d4ee673 | 319 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 320 | |
AhmedPlaymaker | 25:e827f1a8fadc | 321 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 22:ee698f66146f | 322 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 323 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 324 | if (_x[i] <= 0) {_x[i] = 0;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 325 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 326 | if (_x[i] > 81) {_x[i] = 81;} |
AhmedPlaymaker | 8:890b986b16a4 | 327 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 328 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 329 |