Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Tue Apr 23 12:25:49 2019 +0000
Revision:
41:4edac50f010d
Parent:
40:1debed7ec01c
Child:
42:973bb6036f81
This version has a separate class to interface with length, and also changed a lot of the code to interface with it.

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