Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Mon Apr 22 05:28:37 2019 +0000
Revision:
39:210ac915e0a0
Parent:
38:30e4e6191762
Child:
40:1debed7ec01c
Removed some unnecessary get pos functions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AhmedPlaymaker 7:48ba87cd79b5 1 #include "SnakevsBlock.h"
AhmedPlaymaker 7:48ba87cd79b5 2
AhmedPlaymaker 7:48ba87cd79b5 3 SnakevsBlock::SnakevsBlock()
AhmedPlaymaker 7:48ba87cd79b5 4 {
AhmedPlaymaker 7:48ba87cd79b5 5
AhmedPlaymaker 7:48ba87cd79b5 6 }
AhmedPlaymaker 7:48ba87cd79b5 7
AhmedPlaymaker 7:48ba87cd79b5 8 SnakevsBlock::~SnakevsBlock()
AhmedPlaymaker 7:48ba87cd79b5 9 {
AhmedPlaymaker 7:48ba87cd79b5 10
AhmedPlaymaker 7:48ba87cd79b5 11 }
AhmedPlaymaker 7:48ba87cd79b5 12
AhmedPlaymaker 7:48ba87cd79b5 13 void SnakevsBlock::init()
AhmedPlaymaker 7:48ba87cd79b5 14 {
AhmedPlaymaker 7:48ba87cd79b5 15 //The snake length configuration and all the other initial information passing will be done here
AhmedPlaymaker 12:1e601b176437 16 length = 3;
AhmedPlaymaker 19:05cc9f801468 17 level = 1;
AhmedPlaymaker 38:30e4e6191762 18 garbage = 0;
AhmedPlaymaker 38:30e4e6191762 19 angle = 0;
AhmedPlaymaker 12:1e601b176437 20 foodbuff = 0;
AhmedPlaymaker 12:1e601b176437 21 send=0;
AhmedPlaymaker 13:9785f2404045 22 speed = 1;
AhmedPlaymaker 36:dfdd619874ae 23 blockgap = 400;
AhmedPlaymaker 32:3a3bdeffdf62 24 blockbuff = -50;
AhmedPlaymaker 39:210ac915e0a0 25 velocity = 1;
AhmedPlaymaker 22:ee698f66146f 26 for(int i=0; i<=14; i++) {
AhmedPlaymaker 22:ee698f66146f 27 b[i] = 1;
AhmedPlaymaker 22:ee698f66146f 28 }
AhmedPlaymaker 25:e827f1a8fadc 29 _s.init();
AhmedPlaymaker 25:e827f1a8fadc 30 _f.init();
AhmedPlaymaker 25:e827f1a8fadc 31 _ff.init();
AhmedPlaymaker 25:e827f1a8fadc 32 _fff.init();
AhmedPlaymaker 25:e827f1a8fadc 33 _b.init();
AhmedPlaymaker 7:48ba87cd79b5 34 }
AhmedPlaymaker 7:48ba87cd79b5 35
AhmedPlaymaker 7:48ba87cd79b5 36
AhmedPlaymaker 7:48ba87cd79b5 37
AhmedPlaymaker 39:210ac915e0a0 38 void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int g_mode)
AhmedPlaymaker 7:48ba87cd79b5 39 {
AhmedPlaymaker 38:30e4e6191762 40 device.get_values();
AhmedPlaymaker 38:30e4e6191762 41 angle = -device.get_roll_angle();
AhmedPlaymaker 38:30e4e6191762 42 //if button A is pressed then reset that particular position to center
AhmedPlaymaker 38:30e4e6191762 43 if (pad.check_event(Gamepad::A_PRESSED) == true) {
AhmedPlaymaker 38:30e4e6191762 44 garbage = angle;
AhmedPlaymaker 38:30e4e6191762 45 }
AhmedPlaymaker 38:30e4e6191762 46 device.get_values();
AhmedPlaymaker 38:30e4e6191762 47 angle = -device.get_roll_angle() - garbage;
AhmedPlaymaker 39:210ac915e0a0 48 if(g_mode == 1) { //this condition returns the relevant working directions if we select joystick in StartScreen.
AhmedPlaymaker 38:30e4e6191762 49 _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
AhmedPlaymaker 38:30e4e6191762 50 _mag = pad.get_mag(); //Obtains Magnitude of Joystick.
AhmedPlaymaker 38:30e4e6191762 51 }
AhmedPlaymaker 39:210ac915e0a0 52 else if(g_mode == 2) { //this condition returns the relevant working directions if we select motion control in StartScreen.
AhmedPlaymaker 38:30e4e6191762 53 if (angle >= 8) {
AhmedPlaymaker 38:30e4e6191762 54 _d = E;
AhmedPlaymaker 38:30e4e6191762 55 }
AhmedPlaymaker 38:30e4e6191762 56 else if (angle <= -8) {
AhmedPlaymaker 38:30e4e6191762 57 _d = W;
AhmedPlaymaker 38:30e4e6191762 58 }
AhmedPlaymaker 38:30e4e6191762 59 else {
AhmedPlaymaker 38:30e4e6191762 60 _d = CENTRE;
AhmedPlaymaker 38:30e4e6191762 61 }
AhmedPlaymaker 38:30e4e6191762 62 }
AhmedPlaymaker 38:30e4e6191762 63 device.get_values();
AhmedPlaymaker 38:30e4e6191762 64 //printf("%d",gm);
AhmedPlaymaker 38:30e4e6191762 65 //printf("%f",angle);
AhmedPlaymaker 38:30e4e6191762 66 //printf("%f",device.get_roll_angle());
AhmedPlaymaker 7:48ba87cd79b5 67 }
AhmedPlaymaker 7:48ba87cd79b5 68
AhmedPlaymaker 27:3b4775a0d0bb 69 void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad) {
AhmedPlaymaker 20:1e6338403427 70 length = _s.draw(pad, lcd, length, level); //Draws the Snake. //Make these snake buffs relative to the snake drops which in turn relate to the game speed
AhmedPlaymaker 9:d1d79d4ee673 71 if(foodbuff >=0) {
AhmedPlaymaker 36:dfdd619874ae 72 _f.draw(lcd, blockbuff); //Draws the first food.
AhmedPlaymaker 39:210ac915e0a0 73 }
AhmedPlaymaker 39:210ac915e0a0 74 if(foodbuff >=50) {
AhmedPlaymaker 39:210ac915e0a0 75 _ff.draw(lcd, blockbuff); //Draws the second food.
AhmedPlaymaker 9:d1d79d4ee673 76 }
AhmedPlaymaker 39:210ac915e0a0 77 if(foodbuff >=80) {
AhmedPlaymaker 39:210ac915e0a0 78 _fff.draw(lcd, blockbuff); //Draws the third food.
AhmedPlaymaker 39:210ac915e0a0 79 }
AhmedPlaymaker 39:210ac915e0a0 80 foodbuff +=1;
AhmedPlaymaker 16:7b474f873683 81 if(foodbuff >=8) {
AhmedPlaymaker 36:dfdd619874ae 82 send = _b.draw(lcd, length, blockgap);
AhmedPlaymaker 19:05cc9f801468 83 }
AhmedPlaymaker 36:dfdd619874ae 84 if(foodbuff == 8) {
AhmedPlaymaker 38:30e4e6191762 85 blockbuff = -10;
AhmedPlaymaker 13:9785f2404045 86 }
AhmedPlaymaker 7:48ba87cd79b5 87 //Code to print length on game screen.
AhmedPlaymaker 39:210ac915e0a0 88 SnakevsBlock::print_length_on_screen(lcd);
AhmedPlaymaker 39:210ac915e0a0 89 }
AhmedPlaymaker 39:210ac915e0a0 90
AhmedPlaymaker 39:210ac915e0a0 91 void SnakevsBlock::print_length_on_screen(N5110 &lcd)
AhmedPlaymaker 39:210ac915e0a0 92 {
AhmedPlaymaker 7:48ba87cd79b5 93 char bufferscore[14];
AhmedPlaymaker 7:48ba87cd79b5 94 sprintf(bufferscore,"%d",length);
AhmedPlaymaker 39:210ac915e0a0 95 lcd.printString(bufferscore,1,0);
AhmedPlaymaker 39:210ac915e0a0 96 }
AhmedPlaymaker 7:48ba87cd79b5 97
AhmedPlaymaker 33:249cf423fb18 98 int SnakevsBlock::update(Gamepad &pad, SDFileSystem &sd) //Updates objects on screen.
AhmedPlaymaker 7:48ba87cd79b5 99 {
AhmedPlaymaker 18:b391caa5754c 100 CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
AhmedPlaymaker 12:1e601b176437 101 CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
AhmedPlaymaker 38:30e4e6191762 102 CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
AhmedPlaymaker 38:30e4e6191762 103 _s.update(_d, length, speed, b); //_d is the direction of joystick and b controls the motion of a section of the snake relative to obstruction
AhmedPlaymaker 9:d1d79d4ee673 104 _f.update();
AhmedPlaymaker 9:d1d79d4ee673 105 _ff.update();
AhmedPlaymaker 9:d1d79d4ee673 106 _fff.update();
AhmedPlaymaker 12:1e601b176437 107 _b.update(blocknum, srn, send);
AhmedPlaymaker 19:05cc9f801468 108 blockbuff++;
AhmedPlaymaker 36:dfdd619874ae 109 if(blockbuff == blockgap) { //this makes blockbuff reset every time the new set of blocks appear.
AhmedPlaymaker 36:dfdd619874ae 110 blockbuff = -11;
AhmedPlaymaker 19:05cc9f801468 111 }
AhmedPlaymaker 37:ee47699915b8 112 if(length >= 20) { //to make progressive levels harder
AhmedPlaymaker 33:249cf423fb18 113 if(blockgap>=50) { //to make progressive levels harder
AhmedPlaymaker 33:249cf423fb18 114 blockgap -= 20;
AhmedPlaymaker 33:249cf423fb18 115 }
AhmedPlaymaker 19:05cc9f801468 116 level += 1;
AhmedPlaymaker 33:249cf423fb18 117 _statset.write(level, sd);
AhmedPlaymaker 16:7b474f873683 118 }
AhmedPlaymaker 25:e827f1a8fadc 119 if(pad.check_event(Gamepad::BACK_PRESSED)){ //Waits for Back button to be pressed.
AhmedPlaymaker 25:e827f1a8fadc 120 back = 1;
AhmedPlaymaker 25:e827f1a8fadc 121 //add some warning here and use A as the button to confirm
AhmedPlaymaker 25:e827f1a8fadc 122 SnakevsBlock::init();
AhmedPlaymaker 25:e827f1a8fadc 123 }
AhmedPlaymaker 25:e827f1a8fadc 124 else {
AhmedPlaymaker 25:e827f1a8fadc 125 back = 0;
AhmedPlaymaker 25:e827f1a8fadc 126 }
AhmedPlaymaker 25:e827f1a8fadc 127 return back;
AhmedPlaymaker 7:48ba87cd79b5 128 }
AhmedPlaymaker 7:48ba87cd79b5 129
AhmedPlaymaker 7:48ba87cd79b5 130 void SnakevsBlock::get_pos()
AhmedPlaymaker 7:48ba87cd79b5 131 {
AhmedPlaymaker 9:d1d79d4ee673 132 Vector2D snake_pos = _s.get_pos(length);
AhmedPlaymaker 7:48ba87cd79b5 133 //printf("player pos = %f %f \n", player_pos.x, player_pos.y); //top left of player sprite
AhmedPlaymaker 7:48ba87cd79b5 134 // 81.000000 0.000000 top right
AhmedPlaymaker 7:48ba87cd79b5 135 // 0.000000 0.000000 is top left
AhmedPlaymaker 7:48ba87cd79b5 136 // 81.000000 45.000000 bottom right
AhmedPlaymaker 7:48ba87cd79b5 137 snakex = snake_pos.x;
AhmedPlaymaker 7:48ba87cd79b5 138 snakey = snake_pos.y;
AhmedPlaymaker 7:48ba87cd79b5 139 //printf("snakexy in GAME = %d %d \n", snakex, snakey);
AhmedPlaymaker 7:48ba87cd79b5 140 }
AhmedPlaymaker 7:48ba87cd79b5 141
AhmedPlaymaker 9:d1d79d4ee673 142
AhmedPlaymaker 27:3b4775a0d0bb 143 void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad) {
AhmedPlaymaker 32:3a3bdeffdf62 144 //Obtains all required coordinates.
AhmedPlaymaker 32:3a3bdeffdf62 145 Vector2D food_pos[3];
AhmedPlaymaker 32:3a3bdeffdf62 146 food_pos[0] = _f.get_pos();
AhmedPlaymaker 32:3a3bdeffdf62 147 food_pos[1] = _ff.get_pos();
AhmedPlaymaker 32:3a3bdeffdf62 148 food_pos[2] = _fff.get_pos();
AhmedPlaymaker 32:3a3bdeffdf62 149 Vector2D snake_pos = _s.get_pos(length);
AhmedPlaymaker 32:3a3bdeffdf62 150 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 32:3a3bdeffdf62 151 //of the three food sprites, if so then the food location is reset and
AhmedPlaymaker 32:3a3bdeffdf62 152 //length of the snake is increased using the length variable.
AhmedPlaymaker 29:c6358c39a70e 153 for(int y=0; y<=2; y++) { //this loop automatically detects each combination of collision in the y postion
AhmedPlaymaker 29:c6358c39a70e 154 for(int x=0; x<=2; x++) { //this loop automatically detects each combination of collision in the x postion
AhmedPlaymaker 29:c6358c39a70e 155 for(int food_sr=0; food_sr<=2; food_sr++) { //this loop automatically detects which food we are interacting with.
AhmedPlaymaker 29:c6358c39a70e 156 if (
AhmedPlaymaker 29:c6358c39a70e 157 ((snake_pos.y + y == food_pos[food_sr].y) ||
AhmedPlaymaker 29:c6358c39a70e 158 (snake_pos.y + y == food_pos[food_sr].y + 1) ||
AhmedPlaymaker 29:c6358c39a70e 159 (snake_pos.y + y == food_pos[food_sr].y + 2)) &&
AhmedPlaymaker 29:c6358c39a70e 160 ((snake_pos.x + x == food_pos[food_sr].x) ||
AhmedPlaymaker 29:c6358c39a70e 161 (snake_pos.x + x == food_pos[food_sr].x + 1) ||
AhmedPlaymaker 29:c6358c39a70e 162 (snake_pos.x + x == food_pos[food_sr].x + 2))
AhmedPlaymaker 29:c6358c39a70e 163 ) {
AhmedPlaymaker 29:c6358c39a70e 164 //printf("snake feast working \n");
AhmedPlaymaker 29:c6358c39a70e 165 //audio feedback
AhmedPlaymaker 29:c6358c39a70e 166 pad.tone(1000.0,0.1);
AhmedPlaymaker 38:30e4e6191762 167 food_pos[food_sr].x = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis.
AhmedPlaymaker 38:30e4e6191762 168 food_pos[food_sr].y = -3;
AhmedPlaymaker 29:c6358c39a70e 169 length+=1;
AhmedPlaymaker 29:c6358c39a70e 170 }
AhmedPlaymaker 25:e827f1a8fadc 171 }
AhmedPlaymaker 25:e827f1a8fadc 172 }
AhmedPlaymaker 9:d1d79d4ee673 173 }
AhmedPlaymaker 29:c6358c39a70e 174 _f.set_pos(food_pos[0]);
AhmedPlaymaker 29:c6358c39a70e 175 _ff.set_pos(food_pos[1]);
AhmedPlaymaker 29:c6358c39a70e 176 _fff.set_pos(food_pos[2]);
AhmedPlaymaker 29:c6358c39a70e 177 }
AhmedPlaymaker 12:1e601b176437 178
AhmedPlaymaker 39:210ac915e0a0 179 void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad) {
AhmedPlaymaker 39:210ac915e0a0 180 //Obtains all required coordinates.
AhmedPlaymaker 39:210ac915e0a0 181 Vector2D b_pos = _b.get_pos();
AhmedPlaymaker 39:210ac915e0a0 182 int *b_number;
AhmedPlaymaker 39:210ac915e0a0 183 b_number = _b.get_number();
AhmedPlaymaker 39:210ac915e0a0 184 Vector2D snake_pos = _s.get_pos(length);
AhmedPlaymaker 39:210ac915e0a0 185 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 39:210ac915e0a0 186 //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces
AhmedPlaymaker 39:210ac915e0a0 187 //the block has to move slower and come down after every 2/3 iterations(dependent on the snake size.(think about this)
AhmedPlaymaker 39:210ac915e0a0 188 for(int block=0; block<=83; block+=1) { //this loop automatically detects for each section of block and each combination of collision
AhmedPlaymaker 39:210ac915e0a0 189 if ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + block)) {
AhmedPlaymaker 39:210ac915e0a0 190 //printf("snake collision working \n");
AhmedPlaymaker 39:210ac915e0a0 191 //audio feedback
AhmedPlaymaker 39:210ac915e0a0 192 if(blocknum > 0) {b_pos.y = 0;} //change this to speed y = 0 when length = 10.
AhmedPlaymaker 39:210ac915e0a0 193 srn = CheckBlock(block); //this tells us which of the 5 blocks we are colliding with
AhmedPlaymaker 39:210ac915e0a0 194 blocknum = b_number[srn];
AhmedPlaymaker 39:210ac915e0a0 195 ImplementCollision(pad);
AhmedPlaymaker 39:210ac915e0a0 196 if((length>=10)&&(b_number[srn]>0)) { //this makes the block stop moving down if it's length is more than 10 and still collides.
AhmedPlaymaker 39:210ac915e0a0 197 velocity = 0;
AhmedPlaymaker 33:249cf423fb18 198 }
AhmedPlaymaker 39:210ac915e0a0 199 else {
AhmedPlaymaker 39:210ac915e0a0 200 velocity = 1;
AhmedPlaymaker 39:210ac915e0a0 201 }
AhmedPlaymaker 39:210ac915e0a0 202 SnakevsBlock::_set_velocity();
AhmedPlaymaker 12:1e601b176437 203 }
AhmedPlaymaker 39:210ac915e0a0 204 }
AhmedPlaymaker 39:210ac915e0a0 205 }
AhmedPlaymaker 39:210ac915e0a0 206
AhmedPlaymaker 39:210ac915e0a0 207 void SnakevsBlock::_set_velocity() {
AhmedPlaymaker 39:210ac915e0a0 208 _b.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 209 _f.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 210 _ff.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 211 _fff.velocity.y = velocity;
AhmedPlaymaker 27:3b4775a0d0bb 212 }
AhmedPlaymaker 29:c6358c39a70e 213
AhmedPlaymaker 29:c6358c39a70e 214 int SnakevsBlock::CheckBlock(int block) {
AhmedPlaymaker 29:c6358c39a70e 215 int srn;
AhmedPlaymaker 29:c6358c39a70e 216 if((block>=0)&&(block<=18)) {srn = 0;}
AhmedPlaymaker 29:c6358c39a70e 217 if((block>=19)&&(block<=34)) {srn = 1;}
AhmedPlaymaker 29:c6358c39a70e 218 if((block>=35)&&(block<=50)) {srn = 2;}
AhmedPlaymaker 29:c6358c39a70e 219 if((block>=51)&&(block<=66)) {srn = 3;}
AhmedPlaymaker 29:c6358c39a70e 220 if((block>=67)&&(block<=83)) {srn = 4;}
AhmedPlaymaker 29:c6358c39a70e 221 return srn;
AhmedPlaymaker 29:c6358c39a70e 222 }
AhmedPlaymaker 27:3b4775a0d0bb 223
AhmedPlaymaker 27:3b4775a0d0bb 224 void SnakevsBlock::ImplementCollision(Gamepad &pad) {
AhmedPlaymaker 27:3b4775a0d0bb 225 send=1;
AhmedPlaymaker 27:3b4775a0d0bb 226 blocknum-=1;
AhmedPlaymaker 27:3b4775a0d0bb 227 if(blocknum >= 0) { // to make sure that snake doesn't decrease in length if number on the block is less than 1;
AhmedPlaymaker 27:3b4775a0d0bb 228 length-=1;
AhmedPlaymaker 27:3b4775a0d0bb 229 pad.tone(1000.0,0.1);
AhmedPlaymaker 29:c6358c39a70e 230 wait(0.04);
AhmedPlaymaker 12:1e601b176437 231 }
AhmedPlaymaker 13:9785f2404045 232 }
AhmedPlaymaker 13:9785f2404045 233
AhmedPlaymaker 38:30e4e6191762 234 void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad)
AhmedPlaymaker 13:9785f2404045 235 {
AhmedPlaymaker 26:3495f7b0ede7 236 //Obtains all required coordinates for checking block sides and snake collision.
AhmedPlaymaker 13:9785f2404045 237 Vector2D b_pos = _b.get_pos();
AhmedPlaymaker 24:1c118b071430 238 snake_pos[0] = _s.get_pos(length);
AhmedPlaymaker 24:1c118b071430 239 snake_pos[1] = _s.get_pos_before1(length);
AhmedPlaymaker 24:1c118b071430 240 snake_pos[2] = _s.get_pos_before2(length);
AhmedPlaymaker 24:1c118b071430 241 snake_pos[3] = _s.get_pos_before3(length);
AhmedPlaymaker 24:1c118b071430 242 snake_pos[4] = _s.get_pos_before4(length);
AhmedPlaymaker 24:1c118b071430 243 snake_pos[5] = _s.get_pos_before5(length);
AhmedPlaymaker 24:1c118b071430 244 snake_pos[6] = _s.get_pos_before6(length);
AhmedPlaymaker 24:1c118b071430 245 snake_pos[7] = _s.get_pos_before7(length);
AhmedPlaymaker 24:1c118b071430 246 snake_pos[8] = _s.get_pos_before8(length);
AhmedPlaymaker 24:1c118b071430 247 snake_pos[9] = _s.get_pos_before9(length);
AhmedPlaymaker 13:9785f2404045 248 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 13:9785f2404045 249 //of the blocks' sides and then stop the snake moving in x axis
AhmedPlaymaker 13:9785f2404045 250
AhmedPlaymaker 37:ee47699915b8 251 int _length;
AhmedPlaymaker 37:ee47699915b8 252
AhmedPlaymaker 37:ee47699915b8 253 if(length<=10) {_length = length;} //to stop the snake length virtually at 10 when it goes past it.
AhmedPlaymaker 37:ee47699915b8 254 else {_length = 10;}
AhmedPlaymaker 37:ee47699915b8 255
AhmedPlaymaker 37:ee47699915b8 256 for(int i=0; i<=9; i++) {
AhmedPlaymaker 24:1c118b071430 257 b[i] = 1;
AhmedPlaymaker 24:1c118b071430 258 }
AhmedPlaymaker 23:592345a70329 259
AhmedPlaymaker 37:ee47699915b8 260 for(int i=0; i<=9; i++) { //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 36:dfdd619874ae 261 for(int b_y_combination=0; b_y_combination<=10; b_y_combination++) {
AhmedPlaymaker 26:3495f7b0ede7 262 if (
AhmedPlaymaker 36:dfdd619874ae 263 (snake_pos[i].y == b_pos.y + b_y_combination) ||
AhmedPlaymaker 36:dfdd619874ae 264 (snake_pos[i].y + 1 == b_pos.y + b_y_combination) ||
AhmedPlaymaker 36:dfdd619874ae 265 (snake_pos[i].y + 2 == b_pos.y + b_y_combination)) {
AhmedPlaymaker 36:dfdd619874ae 266 for(int b_x_combination=2; b_x_combination<=82; b_x_combination+=16) {
AhmedPlaymaker 36:dfdd619874ae 267 //For West side of walls
AhmedPlaymaker 36:dfdd619874ae 268 if(
AhmedPlaymaker 36:dfdd619874ae 269 ((snake_pos[i].x == b_pos.x + b_x_combination+2) || //W
AhmedPlaymaker 38:30e4e6191762 270 (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(_length > i) //W
AhmedPlaymaker 36:dfdd619874ae 271 ) {
AhmedPlaymaker 36:dfdd619874ae 272 //code makes sure that the colliding part doesn't move in x axis.
AhmedPlaymaker 37:ee47699915b8 273 for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) {
AhmedPlaymaker 37:ee47699915b8 274 if(_length == snake_beed_num + i) {
AhmedPlaymaker 36:dfdd619874ae 275 b[snake_beed_num - 1] = 0;
AhmedPlaymaker 36:dfdd619874ae 276 }
AhmedPlaymaker 26:3495f7b0ede7 277 }
AhmedPlaymaker 25:e827f1a8fadc 278 }
AhmedPlaymaker 36:dfdd619874ae 279
AhmedPlaymaker 36:dfdd619874ae 280 //for East side of walls
AhmedPlaymaker 36:dfdd619874ae 281 else if (
AhmedPlaymaker 36:dfdd619874ae 282 ((snake_pos[i].x + 1 == b_x_combination) || //E
AhmedPlaymaker 38:30e4e6191762 283 (snake_pos[i].x + 2 == b_x_combination))&&(_d != W)&&(_length > i) //E
AhmedPlaymaker 36:dfdd619874ae 284 ) {
AhmedPlaymaker 36:dfdd619874ae 285 //code makes sure that the colliding part doesn't move in x axis.
AhmedPlaymaker 37:ee47699915b8 286 for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) {
AhmedPlaymaker 37:ee47699915b8 287 if(_length == snake_beed_num + i) {
AhmedPlaymaker 36:dfdd619874ae 288 b[snake_beed_num - 1] = 0;
AhmedPlaymaker 36:dfdd619874ae 289 }
AhmedPlaymaker 32:3a3bdeffdf62 290 }
AhmedPlaymaker 32:3a3bdeffdf62 291 }
AhmedPlaymaker 26:3495f7b0ede7 292 }
AhmedPlaymaker 25:e827f1a8fadc 293 }
AhmedPlaymaker 24:1c118b071430 294 }
AhmedPlaymaker 15:f4d069da093d 295 }
AhmedPlaymaker 22:ee698f66146f 296 }