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