![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
Snake/Snake.cpp@37:ee47699915b8, 2019-04-18 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu Apr 18 14:03:43 2019 +0000
- Revision:
- 37:ee47699915b8
- Parent:
- 36:dfdd619874ae
- Child:
- 39:210ac915e0a0
Added feature that sets the real length to max 10, the virtual length increases upto anything i want, currently 20, I may have second thoughts on this, so may revert commit.
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 | 37:ee47699915b8 | 57 | for(int a=1; a<=10; 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 | 37:ee47699915b8 | 64 | if(length>=10) { |
AhmedPlaymaker | 37:ee47699915b8 | 65 | for(int a=1; a<=10; a++) { |
AhmedPlaymaker | 37:ee47699915b8 | 66 | for(int i=0; i<=a-1; i++) { |
AhmedPlaymaker | 37:ee47699915b8 | 67 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 37:ee47699915b8 | 68 | } |
AhmedPlaymaker | 37:ee47699915b8 | 69 | } |
AhmedPlaymaker | 37:ee47699915b8 | 70 | } |
AhmedPlaymaker | 24:1c118b071430 | 71 | |
AhmedPlaymaker | 37:ee47699915b8 | 72 | if(length >= 20) { |
AhmedPlaymaker | 17:54e13faeaf64 | 73 | lcd.clear(); |
AhmedPlaymaker | 17:54e13faeaf64 | 74 | lcd.printString("Level Complete",0,1); |
AhmedPlaymaker | 24:1c118b071430 | 75 | lcd.printString("Press Start",10,3); |
AhmedPlaymaker | 24:1c118b071430 | 76 | lcd.printString("to Proceed",12,4); |
AhmedPlaymaker | 17:54e13faeaf64 | 77 | lcd.refresh(); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 78 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
AhmedPlaymaker | 20:1e6338403427 | 79 | pad.leds_on(); |
AhmedPlaymaker | 24:1c118b071430 | 80 | pad.tone(rand()%1001,0.1); //helps to set max freq for random sound |
AhmedPlaymaker | 20:1e6338403427 | 81 | wait(0.1); |
AhmedPlaymaker | 20:1e6338403427 | 82 | pad.leds_off(); |
AhmedPlaymaker | 20:1e6338403427 | 83 | wait(0.1); |
AhmedPlaymaker | 20:1e6338403427 | 84 | } |
AhmedPlaymaker | 20:1e6338403427 | 85 | lcd.clear(); |
AhmedPlaymaker | 20:1e6338403427 | 86 | sprintf(bufferlevel,"%d",level); |
AhmedPlaymaker | 20:1e6338403427 | 87 | lcd.printString("Level:",25,1); |
AhmedPlaymaker | 20:1e6338403427 | 88 | lcd.printString(bufferlevel,40,3); |
AhmedPlaymaker | 20:1e6338403427 | 89 | lcd.refresh(); |
AhmedPlaymaker | 20:1e6338403427 | 90 | wait(2); |
AhmedPlaymaker | 17:54e13faeaf64 | 91 | length = 3; |
AhmedPlaymaker | 17:54e13faeaf64 | 92 | } |
AhmedPlaymaker | 17:54e13faeaf64 | 93 | return length; |
AhmedPlaymaker | 7:48ba87cd79b5 | 94 | } |
AhmedPlaymaker | 19:05cc9f801468 | 95 | |
AhmedPlaymaker | 24:1c118b071430 | 96 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 97 | Vector2D Snake::get_pos(int length) |
AhmedPlaymaker | 7:48ba87cd79b5 | 98 | { |
AhmedPlaymaker | 37:ee47699915b8 | 99 | for(int i=1; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 100 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 101 | Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 102 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 103 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 104 | } |
AhmedPlaymaker | 37:ee47699915b8 | 105 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 106 | Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 107 | return snakepos; |
AhmedPlaymaker | 37:ee47699915b8 | 108 | } |
AhmedPlaymaker | 24:1c118b071430 | 109 | Vector2D snakepos = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 110 | return snakepos; |
AhmedPlaymaker | 7:48ba87cd79b5 | 111 | //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); |
AhmedPlaymaker | 13:9785f2404045 | 112 | } |
AhmedPlaymaker | 13:9785f2404045 | 113 | |
AhmedPlaymaker | 13:9785f2404045 | 114 | Vector2D Snake::get_pos_before1(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 115 | { |
AhmedPlaymaker | 37:ee47699915b8 | 116 | for(int i=2; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 117 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 118 | Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 119 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 120 | } |
AhmedPlaymaker | 13:9785f2404045 | 121 | } |
AhmedPlaymaker | 37:ee47699915b8 | 122 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 123 | Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 124 | return snakepos_b1; |
AhmedPlaymaker | 37:ee47699915b8 | 125 | } |
AhmedPlaymaker | 24:1c118b071430 | 126 | Vector2D snakepos_b1 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 127 | return snakepos_b1; |
AhmedPlaymaker | 24:1c118b071430 | 128 | //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y); |
AhmedPlaymaker | 13:9785f2404045 | 129 | } |
AhmedPlaymaker | 13:9785f2404045 | 130 | |
AhmedPlaymaker | 13:9785f2404045 | 131 | Vector2D Snake::get_pos_before2(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 132 | { |
AhmedPlaymaker | 37:ee47699915b8 | 133 | for(int i=3; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 134 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 135 | Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 136 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 137 | } |
AhmedPlaymaker | 13:9785f2404045 | 138 | } |
AhmedPlaymaker | 37:ee47699915b8 | 139 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 140 | Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 141 | return snakepos_b2; |
AhmedPlaymaker | 37:ee47699915b8 | 142 | } |
AhmedPlaymaker | 24:1c118b071430 | 143 | Vector2D snakepos_b2 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 144 | return snakepos_b2; |
AhmedPlaymaker | 24:1c118b071430 | 145 | //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y); |
AhmedPlaymaker | 13:9785f2404045 | 146 | } |
AhmedPlaymaker | 13:9785f2404045 | 147 | |
AhmedPlaymaker | 13:9785f2404045 | 148 | Vector2D Snake::get_pos_before3(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 149 | { |
AhmedPlaymaker | 37:ee47699915b8 | 150 | for(int i=4; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 151 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 152 | Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 153 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 154 | } |
AhmedPlaymaker | 13:9785f2404045 | 155 | } |
AhmedPlaymaker | 37:ee47699915b8 | 156 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 157 | Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 158 | return snakepos_b3; |
AhmedPlaymaker | 37:ee47699915b8 | 159 | } |
AhmedPlaymaker | 24:1c118b071430 | 160 | Vector2D snakepos_b3 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 161 | return snakepos_b3; |
AhmedPlaymaker | 24:1c118b071430 | 162 | //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y); |
AhmedPlaymaker | 13:9785f2404045 | 163 | } |
AhmedPlaymaker | 13:9785f2404045 | 164 | |
AhmedPlaymaker | 13:9785f2404045 | 165 | Vector2D Snake::get_pos_before4(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 166 | { |
AhmedPlaymaker | 37:ee47699915b8 | 167 | for(int i=5; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 168 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 169 | Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 170 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 171 | } |
AhmedPlaymaker | 13:9785f2404045 | 172 | } |
AhmedPlaymaker | 37:ee47699915b8 | 173 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 174 | Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 175 | return snakepos_b4; |
AhmedPlaymaker | 37:ee47699915b8 | 176 | } |
AhmedPlaymaker | 24:1c118b071430 | 177 | Vector2D snakepos_b4 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 178 | return snakepos_b4; |
AhmedPlaymaker | 24:1c118b071430 | 179 | //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y); |
AhmedPlaymaker | 13:9785f2404045 | 180 | } |
AhmedPlaymaker | 13:9785f2404045 | 181 | |
AhmedPlaymaker | 13:9785f2404045 | 182 | Vector2D Snake::get_pos_before5(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 183 | { |
AhmedPlaymaker | 37:ee47699915b8 | 184 | for(int i=6; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 185 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 186 | Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 187 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 188 | } |
AhmedPlaymaker | 13:9785f2404045 | 189 | } |
AhmedPlaymaker | 37:ee47699915b8 | 190 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 191 | Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 192 | return snakepos_b5; |
AhmedPlaymaker | 37:ee47699915b8 | 193 | } |
AhmedPlaymaker | 24:1c118b071430 | 194 | Vector2D snakepos_b5 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 195 | return snakepos_b5; |
AhmedPlaymaker | 24:1c118b071430 | 196 | //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y); |
AhmedPlaymaker | 13:9785f2404045 | 197 | } |
AhmedPlaymaker | 13:9785f2404045 | 198 | |
AhmedPlaymaker | 13:9785f2404045 | 199 | Vector2D Snake::get_pos_before6(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 200 | { |
AhmedPlaymaker | 37:ee47699915b8 | 201 | for(int i=7; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 202 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 203 | Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 204 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 205 | } |
AhmedPlaymaker | 13:9785f2404045 | 206 | } |
AhmedPlaymaker | 37:ee47699915b8 | 207 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 208 | Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 209 | return snakepos_b6; |
AhmedPlaymaker | 37:ee47699915b8 | 210 | } |
AhmedPlaymaker | 24:1c118b071430 | 211 | Vector2D snakepos_b6 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 212 | return snakepos_b6; |
AhmedPlaymaker | 24:1c118b071430 | 213 | //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y); |
AhmedPlaymaker | 13:9785f2404045 | 214 | } |
AhmedPlaymaker | 13:9785f2404045 | 215 | |
AhmedPlaymaker | 13:9785f2404045 | 216 | Vector2D Snake::get_pos_before7(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 217 | { |
AhmedPlaymaker | 37:ee47699915b8 | 218 | for(int i=8; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 219 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 220 | Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 221 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 222 | } |
AhmedPlaymaker | 13:9785f2404045 | 223 | } |
AhmedPlaymaker | 37:ee47699915b8 | 224 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 225 | Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 226 | return snakepos_b7; |
AhmedPlaymaker | 37:ee47699915b8 | 227 | } |
AhmedPlaymaker | 24:1c118b071430 | 228 | Vector2D snakepos_b7 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 229 | return snakepos_b7; |
AhmedPlaymaker | 24:1c118b071430 | 230 | //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y); |
AhmedPlaymaker | 7:48ba87cd79b5 | 231 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 232 | |
AhmedPlaymaker | 13:9785f2404045 | 233 | Vector2D Snake::get_pos_before8(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 234 | { |
AhmedPlaymaker | 37:ee47699915b8 | 235 | for(int i=9; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 236 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 237 | Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 238 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 239 | } |
AhmedPlaymaker | 13:9785f2404045 | 240 | } |
AhmedPlaymaker | 37:ee47699915b8 | 241 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 242 | Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 243 | return snakepos_b8; |
AhmedPlaymaker | 37:ee47699915b8 | 244 | } |
AhmedPlaymaker | 24:1c118b071430 | 245 | Vector2D snakepos_b8 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 246 | return snakepos_b8; |
AhmedPlaymaker | 24:1c118b071430 | 247 | //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y); |
AhmedPlaymaker | 13:9785f2404045 | 248 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 249 | |
AhmedPlaymaker | 13:9785f2404045 | 250 | Vector2D Snake::get_pos_before9(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 251 | { |
AhmedPlaymaker | 37:ee47699915b8 | 252 | for(int i=10; i<=10; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 253 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 254 | Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 255 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 256 | } |
AhmedPlaymaker | 13:9785f2404045 | 257 | } |
AhmedPlaymaker | 37:ee47699915b8 | 258 | if(length >= 10) { |
AhmedPlaymaker | 37:ee47699915b8 | 259 | Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position. |
AhmedPlaymaker | 37:ee47699915b8 | 260 | return snakepos_b9; |
AhmedPlaymaker | 37:ee47699915b8 | 261 | } |
AhmedPlaymaker | 24:1c118b071430 | 262 | Vector2D snakepos_b9 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 263 | return snakepos_b9; |
AhmedPlaymaker | 24:1c118b071430 | 264 | //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y); |
AhmedPlaymaker | 13:9785f2404045 | 265 | } |
AhmedPlaymaker | 37:ee47699915b8 | 266 | /* |
AhmedPlaymaker | 13:9785f2404045 | 267 | Vector2D Snake::get_pos_before10(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 268 | { |
AhmedPlaymaker | 24:1c118b071430 | 269 | for(int i=11; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 270 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 271 | Vector2D snakepos_b10 = {_x[i-11],_y[i-11]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 272 | return snakepos_b10; |
AhmedPlaymaker | 24:1c118b071430 | 273 | } |
AhmedPlaymaker | 13:9785f2404045 | 274 | } |
AhmedPlaymaker | 24:1c118b071430 | 275 | Vector2D snakepos_b10 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 276 | return snakepos_b10; |
AhmedPlaymaker | 24:1c118b071430 | 277 | //printf("snakepos_b10 from player = %f %f \n", snakepos_b10.x, snakepos_b10.y); |
AhmedPlaymaker | 13:9785f2404045 | 278 | } |
AhmedPlaymaker | 13:9785f2404045 | 279 | |
AhmedPlaymaker | 13:9785f2404045 | 280 | Vector2D Snake::get_pos_before11(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 281 | { |
AhmedPlaymaker | 24:1c118b071430 | 282 | for(int i=12; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 283 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 284 | Vector2D snakepos_b11 = {_x[i-12],_y[i-12]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 285 | return snakepos_b11; |
AhmedPlaymaker | 24:1c118b071430 | 286 | } |
AhmedPlaymaker | 13:9785f2404045 | 287 | } |
AhmedPlaymaker | 24:1c118b071430 | 288 | Vector2D snakepos_b11 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 289 | return snakepos_b11; |
AhmedPlaymaker | 24:1c118b071430 | 290 | //printf("snakepos_b11 from player = %f %f \n", snakepos_b11.x, snakepos_b11.y); |
AhmedPlaymaker | 13:9785f2404045 | 291 | } |
AhmedPlaymaker | 13:9785f2404045 | 292 | |
AhmedPlaymaker | 13:9785f2404045 | 293 | Vector2D Snake::get_pos_before12(int length) //this sends data of following sprites. |
AhmedPlaymaker | 7:48ba87cd79b5 | 294 | { |
AhmedPlaymaker | 24:1c118b071430 | 295 | for(int i=13; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 296 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 297 | Vector2D snakepos_b12 = {_x[i-13],_y[i-13]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 298 | return snakepos_b12; |
AhmedPlaymaker | 24:1c118b071430 | 299 | } |
AhmedPlaymaker | 13:9785f2404045 | 300 | } |
AhmedPlaymaker | 24:1c118b071430 | 301 | Vector2D snakepos_b12 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 302 | return snakepos_b12; |
AhmedPlaymaker | 24:1c118b071430 | 303 | //printf("snakepos_b12 from player = %f %f \n", snakepos_b12.x, snakepos_b12.y); |
AhmedPlaymaker | 13:9785f2404045 | 304 | } |
AhmedPlaymaker | 13:9785f2404045 | 305 | |
AhmedPlaymaker | 13:9785f2404045 | 306 | Vector2D Snake::get_pos_before13(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 307 | { |
AhmedPlaymaker | 24:1c118b071430 | 308 | for(int i=14; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 309 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 310 | Vector2D snakepos_b13 = {_x[i-14],_y[i-14]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 311 | return snakepos_b13; |
AhmedPlaymaker | 24:1c118b071430 | 312 | } |
AhmedPlaymaker | 13:9785f2404045 | 313 | } |
AhmedPlaymaker | 24:1c118b071430 | 314 | Vector2D snakepos_b13 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 315 | return snakepos_b13; |
AhmedPlaymaker | 24:1c118b071430 | 316 | //printf("snakepos_b13 from player = %f %f \n", snakepos_b13.x, snakepos_b13.y); |
AhmedPlaymaker | 13:9785f2404045 | 317 | } |
AhmedPlaymaker | 13:9785f2404045 | 318 | |
AhmedPlaymaker | 13:9785f2404045 | 319 | Vector2D Snake::get_pos_before14(int length) //this sends data of following sprites. |
AhmedPlaymaker | 13:9785f2404045 | 320 | { |
AhmedPlaymaker | 24:1c118b071430 | 321 | for(int i=15; i<=15; i++) { |
AhmedPlaymaker | 24:1c118b071430 | 322 | if(length == i) { |
AhmedPlaymaker | 24:1c118b071430 | 323 | Vector2D snakepos_b14 = {_x[i-15],_y[i-15]}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 324 | return snakepos_b14; |
AhmedPlaymaker | 24:1c118b071430 | 325 | } |
AhmedPlaymaker | 13:9785f2404045 | 326 | } |
AhmedPlaymaker | 24:1c118b071430 | 327 | Vector2D snakepos_b14 = {-20,-20}; //Obtains the snake position. |
AhmedPlaymaker | 24:1c118b071430 | 328 | return snakepos_b14; |
AhmedPlaymaker | 24:1c118b071430 | 329 | //printf("snakepos_b14 from player = %f %f \n", snakepos_b14.x, snakepos_b14.y); |
AhmedPlaymaker | 13:9785f2404045 | 330 | } |
AhmedPlaymaker | 37:ee47699915b8 | 331 | */ |
AhmedPlaymaker | 18:b391caa5754c | 332 | |
AhmedPlaymaker | 13:9785f2404045 | 333 | |
AhmedPlaymaker | 36:dfdd619874ae | 334 | void Snake::update(Direction d,int length, int speed, int* b) |
AhmedPlaymaker | 13:9785f2404045 | 335 | { |
AhmedPlaymaker | 13:9785f2404045 | 336 | _speed = speed; //Speed changes depending on how much you push the joystick.(As Of Now) |
AhmedPlaymaker | 37:ee47699915b8 | 337 | if(length<=10) {_length = length;} //to stop the snake length virtually at 10 when it goes past it. |
AhmedPlaymaker | 37:ee47699915b8 | 338 | else {_length = 10;} |
AhmedPlaymaker | 25:e827f1a8fadc | 339 | //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 | 340 | //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 | 341 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 37:ee47699915b8 | 342 | if((_length > i+1)&&(_x[i] != _x[i+1])) { |
AhmedPlaymaker | 22:ee698f66146f | 343 | if ((_x[i] > _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 344 | _x[i]-=_speed; |
AhmedPlaymaker | 8:890b986b16a4 | 345 | } |
AhmedPlaymaker | 22:ee698f66146f | 346 | if ((_x[i] < _x[i+1])&&(b[i+1] == 1)&&(b[i] == 1)) { |
AhmedPlaymaker | 22:ee698f66146f | 347 | _x[i]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 348 | } |
AhmedPlaymaker | 21:e41126528cc6 | 349 | } |
AhmedPlaymaker | 21:e41126528cc6 | 350 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 351 | //this makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 22:ee698f66146f | 352 | for(int i=14; i>=0; i--) { |
AhmedPlaymaker | 37:ee47699915b8 | 353 | if((_length == i+1)&&(b[i] == 1)) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 354 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 355 | if (d == E) {_x[i]+= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 356 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 357 | if (d == W) {_x[i]-= _speed;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 358 | |
AhmedPlaymaker | 22:ee698f66146f | 359 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 360 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 361 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 362 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 363 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 364 | for(int i=2; i<=15; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 365 | |
AhmedPlaymaker | 37:ee47699915b8 | 366 | if(_length < i) {_x[i-1] = _x[i-2];} |
AhmedPlaymaker | 9:d1d79d4ee673 | 367 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 368 | |
AhmedPlaymaker | 25:e827f1a8fadc | 369 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 22:ee698f66146f | 370 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 32:3a3bdeffdf62 | 371 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 372 | if (_x[i] <= 0) {_x[i] = 0;} |
AhmedPlaymaker | 32:3a3bdeffdf62 | 373 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 374 | if (_x[i] > 81) {_x[i] = 81;} |
AhmedPlaymaker | 8:890b986b16a4 | 375 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 376 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 377 |