Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Tue Apr 30 08:32:30 2019 +0000
Revision:
61:f3c402bc2ad0
Parent:
60:d3a9e0e4a0de
Child:
62:ebf6ecf8a6d5
Fixed an issue with the gamepad library that made it remember the previous button pressed, also removed pad.init() because of this

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 59:c65a2e933c47 17 _maxLength = 10; // this makes us go to the next level if if maxLength is achieved;
AhmedPlaymaker 41:4edac50f010d 18 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 19 foodbuff = 0; //this makes the food fall at diffrent times when a particular level starts.
AhmedPlaymaker 56:142e9fdb77a8 20 send_block_number = 0; //this is 0 when there is no collision, thus block number isn't remembered for next set (which would lead to empty blocks).
AhmedPlaymaker 59:c65a2e933c47 21 blockgap = 300; //this is the number of itterations the block will reccur after in the first level.
AhmedPlaymaker 49:441c32f6603e 22 for(int i=0; i<=14; i++) {b[i] = 1;} //makes all the snake beads move by default.
AhmedPlaymaker 49:441c32f6603e 23 SnakevsBlock::object_initialisations();
AhmedPlaymaker 7:48ba87cd79b5 24 }
AhmedPlaymaker 7:48ba87cd79b5 25
AhmedPlaymaker 41:4edac50f010d 26 void SnakevsBlock::reset()
AhmedPlaymaker 41:4edac50f010d 27 {
AhmedPlaymaker 41:4edac50f010d 28 //This prepares the game for the next level by reseting certain variables.
AhmedPlaymaker 41:4edac50f010d 29 foodbuff = 0;
AhmedPlaymaker 59:c65a2e933c47 30 if(blockgap >= 50) {blockgap -= 10;} //to make progressive levels harder by making the blocks drop more frequently.
AhmedPlaymaker 60:d3a9e0e4a0de 31 /*
AhmedPlaymaker 60:d3a9e0e4a0de 32 //to clear all the memory buffer of the game object positions from the previous level.
AhmedPlaymaker 60:d3a9e0e4a0de 33 for(int i = 0; i <=2 ; i++) {
AhmedPlaymaker 60:d3a9e0e4a0de 34 food_pos[i].x = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 35 food_pos[i].y = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 36 }
AhmedPlaymaker 60:d3a9e0e4a0de 37 b_pos.x = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 38 b_pos.y = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 39 */
AhmedPlaymaker 49:441c32f6603e 40 SnakevsBlock::object_initialisations();
AhmedPlaymaker 49:441c32f6603e 41 }
AhmedPlaymaker 49:441c32f6603e 42
AhmedPlaymaker 49:441c32f6603e 43 void SnakevsBlock::object_initialisations()
AhmedPlaymaker 49:441c32f6603e 44 {
AhmedPlaymaker 49:441c32f6603e 45 _l.init(); //length calc object initialisation.
AhmedPlaymaker 49:441c32f6603e 46 _s.init(); //snake object initialisation.
AhmedPlaymaker 49:441c32f6603e 47 _f.init(); //food 1 object initialisation.
AhmedPlaymaker 49:441c32f6603e 48 _ff.init(); //food 2 object initialisation.
AhmedPlaymaker 49:441c32f6603e 49 _fff.init(); //food 3 object initialisation.
AhmedPlaymaker 49:441c32f6603e 50 _b.init(); //block object initialisation.
AhmedPlaymaker 41:4edac50f010d 51 }
AhmedPlaymaker 7:48ba87cd79b5 52
AhmedPlaymaker 39:210ac915e0a0 53 void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int g_mode)
AhmedPlaymaker 7:48ba87cd79b5 54 {
AhmedPlaymaker 38:30e4e6191762 55 device.get_values();
AhmedPlaymaker 38:30e4e6191762 56 angle = -device.get_roll_angle();
AhmedPlaymaker 38:30e4e6191762 57 //if button A is pressed then reset that particular position to center
AhmedPlaymaker 38:30e4e6191762 58 if (pad.check_event(Gamepad::A_PRESSED) == true) {
AhmedPlaymaker 38:30e4e6191762 59 garbage = angle;
AhmedPlaymaker 38:30e4e6191762 60 }
AhmedPlaymaker 38:30e4e6191762 61 device.get_values();
AhmedPlaymaker 56:142e9fdb77a8 62 switch (g_mode) {
AhmedPlaymaker 56:142e9fdb77a8 63 case 1:
AhmedPlaymaker 38:30e4e6191762 64 _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
AhmedPlaymaker 61:f3c402bc2ad0 65 _mag = pad.get_mag(); //Obtains Magnitude of Joystick. REMOVE THIS LATER.
AhmedPlaymaker 56:142e9fdb77a8 66 break;
AhmedPlaymaker 56:142e9fdb77a8 67 case 2:
AhmedPlaymaker 56:142e9fdb77a8 68 angle = -device.get_roll_angle() - garbage;
AhmedPlaymaker 56:142e9fdb77a8 69 if (angle >= 6) {
AhmedPlaymaker 38:30e4e6191762 70 _d = E;
AhmedPlaymaker 58:affb42c56cf8 71 for(int led = 4; led <= 6; led++) { pad.led(led,1); }
AhmedPlaymaker 38:30e4e6191762 72 }
AhmedPlaymaker 56:142e9fdb77a8 73 else if (angle <= -6) {
AhmedPlaymaker 38:30e4e6191762 74 _d = W;
AhmedPlaymaker 58:affb42c56cf8 75 for(int led = 1; led <= 3; led++) { pad.led(led,1); }
AhmedPlaymaker 38:30e4e6191762 76 }
AhmedPlaymaker 38:30e4e6191762 77 else {
AhmedPlaymaker 38:30e4e6191762 78 _d = CENTRE;
AhmedPlaymaker 58:affb42c56cf8 79 for(int led = 1; led <= 6; led++) { pad.led(led,0); }
AhmedPlaymaker 38:30e4e6191762 80 }
AhmedPlaymaker 56:142e9fdb77a8 81 break;
AhmedPlaymaker 38:30e4e6191762 82 }
AhmedPlaymaker 38:30e4e6191762 83 //printf("%d",gm);
AhmedPlaymaker 38:30e4e6191762 84 //printf("%f",angle);
AhmedPlaymaker 38:30e4e6191762 85 //printf("%f",device.get_roll_angle());
AhmedPlaymaker 7:48ba87cd79b5 86 }
AhmedPlaymaker 7:48ba87cd79b5 87
AhmedPlaymaker 49:441c32f6603e 88 void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad) {
AhmedPlaymaker 55:df0825049171 89 _length = _l._getLength(); //saves the snake length into a private variable.
AhmedPlaymaker 53:527cf297b088 90 _s.draw(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 91 if(foodbuff >= 0) {
AhmedPlaymaker 56:142e9fdb77a8 92 _f.draw(lcd); //Draws the first food.
AhmedPlaymaker 39:210ac915e0a0 93 }
AhmedPlaymaker 41:4edac50f010d 94 if(foodbuff >= 50) {
AhmedPlaymaker 56:142e9fdb77a8 95 _ff.draw(lcd); //Draws the second food.
AhmedPlaymaker 9:d1d79d4ee673 96 }
AhmedPlaymaker 41:4edac50f010d 97 if(foodbuff >= 80) {
AhmedPlaymaker 56:142e9fdb77a8 98 _fff.draw(lcd); //Draws the third food.
AhmedPlaymaker 39:210ac915e0a0 99 }
AhmedPlaymaker 39:210ac915e0a0 100 foodbuff +=1;
AhmedPlaymaker 41:4edac50f010d 101 if(foodbuff >= 8) {
AhmedPlaymaker 48:d774bb162c61 102 _b.draw(lcd, _length);
AhmedPlaymaker 19:05cc9f801468 103 }
AhmedPlaymaker 7:48ba87cd79b5 104 //Code to print length on game screen.
AhmedPlaymaker 41:4edac50f010d 105 _l.print_length_on_screen(lcd);
AhmedPlaymaker 41:4edac50f010d 106 }
AhmedPlaymaker 39:210ac915e0a0 107
AhmedPlaymaker 49:441c32f6603e 108 int SnakevsBlock::update(N5110 &lcd, Gamepad &pad, SDFileSystem &sd) //Updates objects on screen.
AhmedPlaymaker 39:210ac915e0a0 109 {
AhmedPlaymaker 42:973bb6036f81 110 send_block_number = 0; //this is for the game to decide wether to remember the number on the block for the current itteration.
AhmedPlaymaker 42:973bb6036f81 111 //we dont need to remember if it has already gone past the screen.
AhmedPlaymaker 43:233f93860d08 112 CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
AhmedPlaymaker 18:b391caa5754c 113 CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
AhmedPlaymaker 38:30e4e6191762 114 CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
AhmedPlaymaker 41:4edac50f010d 115 _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 116 _f.update();
AhmedPlaymaker 9:d1d79d4ee673 117 _ff.update();
AhmedPlaymaker 9:d1d79d4ee673 118 _fff.update();
AhmedPlaymaker 48:d774bb162c61 119 _b.update(blocknum, blockgap, srn, send_block_number);
AhmedPlaymaker 49:441c32f6603e 120 //_statset.read(sd); //to read the currently stored value.
AhmedPlaymaker 49:441c32f6603e 121 if(_length == 0) {
AhmedPlaymaker 49:441c32f6603e 122 _wl.GameOver(lcd,pad);
AhmedPlaymaker 49:441c32f6603e 123 }
AhmedPlaymaker 49:441c32f6603e 124 if((pad.check_event(Gamepad::BACK_PRESSED))||(_length == 0)){ //Waits for Back button to be pressed.
AhmedPlaymaker 25:e827f1a8fadc 125 back = 1;
AhmedPlaymaker 25:e827f1a8fadc 126 SnakevsBlock::init();
AhmedPlaymaker 25:e827f1a8fadc 127 }
AhmedPlaymaker 25:e827f1a8fadc 128 else {
AhmedPlaymaker 25:e827f1a8fadc 129 back = 0;
AhmedPlaymaker 25:e827f1a8fadc 130 }
AhmedPlaymaker 52:c2faa96cf293 131 //printf("%d\n",_length);
AhmedPlaymaker 59:c65a2e933c47 132 if(_length >= _maxLength) {
AhmedPlaymaker 59:c65a2e933c47 133 _maxLength++;
AhmedPlaymaker 50:3cf9a94a264e 134 level = _wl.LevelComplete(lcd, pad, level);
AhmedPlaymaker 50:3cf9a94a264e 135 _Setstats.write(level, sd);
AhmedPlaymaker 50:3cf9a94a264e 136 SnakevsBlock::reset();
AhmedPlaymaker 50:3cf9a94a264e 137 }
AhmedPlaymaker 25:e827f1a8fadc 138 return back;
AhmedPlaymaker 7:48ba87cd79b5 139 }
AhmedPlaymaker 7:48ba87cd79b5 140
AhmedPlaymaker 7:48ba87cd79b5 141 void SnakevsBlock::get_pos()
AhmedPlaymaker 7:48ba87cd79b5 142 {
AhmedPlaymaker 7:48ba87cd79b5 143 //printf("player pos = %f %f \n", player_pos.x, player_pos.y); //top left of player sprite
AhmedPlaymaker 7:48ba87cd79b5 144 // 81.000000 0.000000 top right
AhmedPlaymaker 7:48ba87cd79b5 145 // 0.000000 0.000000 is top left
AhmedPlaymaker 7:48ba87cd79b5 146 // 81.000000 45.000000 bottom right
AhmedPlaymaker 43:233f93860d08 147 snakex = _s.get_pos().x; //this could be snake_pos[0].x or simply snake_pos[0] to represent both x&y but as it is used the most, it improves readability.
AhmedPlaymaker 43:233f93860d08 148 snakey = _s.get_pos().y; //this could be snake_pos[0].y or simply snake_pos[0] to represent both x&y but as it is used the most, it improves readability.
AhmedPlaymaker 7:48ba87cd79b5 149 //printf("snakexy in GAME = %d %d \n", snakex, snakey);
AhmedPlaymaker 41:4edac50f010d 150 //Obtains all required coordinates.
AhmedPlaymaker 41:4edac50f010d 151 food_pos[0] = _f.get_pos();
AhmedPlaymaker 41:4edac50f010d 152 food_pos[1] = _ff.get_pos();
AhmedPlaymaker 41:4edac50f010d 153 food_pos[2] = _fff.get_pos();
AhmedPlaymaker 41:4edac50f010d 154 //obtains origin cordinates of block.
AhmedPlaymaker 41:4edac50f010d 155 b_pos = _b.get_pos();
AhmedPlaymaker 41:4edac50f010d 156 //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 157 snake_pos[0] = _s.get_pos(); //gets the position of the top beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 158 snake_pos[1] = _s.get_pos_before1(); //gets the position of the second beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 159 snake_pos[2] = _s.get_pos_before2(); //gets the position of the third beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 160 snake_pos[3] = _s.get_pos_before3(); //gets the position of the fourth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 161 snake_pos[4] = _s.get_pos_before4(); //gets the position of the fifth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 162 snake_pos[5] = _s.get_pos_before5(); //gets the position of the sixth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 163 snake_pos[6] = _s.get_pos_before6(); //gets the position of the seventh beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 164 snake_pos[7] = _s.get_pos_before7(); //gets the position of the eight beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 165 snake_pos[8] = _s.get_pos_before8(); //gets the position of the ninth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 166 snake_pos[9] = _s.get_pos_before9(); //gets the position of the last beed and saves in array.
AhmedPlaymaker 7:48ba87cd79b5 167 }
AhmedPlaymaker 7:48ba87cd79b5 168
AhmedPlaymaker 9:d1d79d4ee673 169
AhmedPlaymaker 27:3b4775a0d0bb 170 void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad) {
AhmedPlaymaker 32:3a3bdeffdf62 171 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 32:3a3bdeffdf62 172 //of the three food sprites, if so then the food location is reset and
AhmedPlaymaker 32:3a3bdeffdf62 173 //length of the snake is increased using the length variable.
AhmedPlaymaker 29:c6358c39a70e 174 for(int y=0; y<=2; y++) { //this loop automatically detects each combination of collision in the y postion
AhmedPlaymaker 29:c6358c39a70e 175 for(int x=0; x<=2; x++) { //this loop automatically detects each combination of collision in the x postion
AhmedPlaymaker 29:c6358c39a70e 176 for(int food_sr=0; food_sr<=2; food_sr++) { //this loop automatically detects which food we are interacting with.
AhmedPlaymaker 29:c6358c39a70e 177 if (
AhmedPlaymaker 41:4edac50f010d 178 ((snakey + y == food_pos[food_sr].y) ||
AhmedPlaymaker 41:4edac50f010d 179 (snakey + y == food_pos[food_sr].y + 1) ||
AhmedPlaymaker 41:4edac50f010d 180 (snakey + y == food_pos[food_sr].y + 2)) &&
AhmedPlaymaker 41:4edac50f010d 181 ((snakex + x == food_pos[food_sr].x) ||
AhmedPlaymaker 41:4edac50f010d 182 (snakex + x == food_pos[food_sr].x + 1) ||
AhmedPlaymaker 41:4edac50f010d 183 (snakex + x == food_pos[food_sr].x + 2))
AhmedPlaymaker 29:c6358c39a70e 184 ) {
AhmedPlaymaker 29:c6358c39a70e 185 //printf("snake feast working \n");
AhmedPlaymaker 29:c6358c39a70e 186 //audio feedback
AhmedPlaymaker 46:dc7dccae9f9e 187 pad.tone(786.0,0.1);
AhmedPlaymaker 38:30e4e6191762 188 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 189 food_pos[food_sr].y = -3;
AhmedPlaymaker 41:4edac50f010d 190 _l.PlusLength();
AhmedPlaymaker 29:c6358c39a70e 191 }
AhmedPlaymaker 25:e827f1a8fadc 192 }
AhmedPlaymaker 25:e827f1a8fadc 193 }
AhmedPlaymaker 9:d1d79d4ee673 194 }
AhmedPlaymaker 29:c6358c39a70e 195 _f.set_pos(food_pos[0]);
AhmedPlaymaker 29:c6358c39a70e 196 _ff.set_pos(food_pos[1]);
AhmedPlaymaker 29:c6358c39a70e 197 _fff.set_pos(food_pos[2]);
AhmedPlaymaker 29:c6358c39a70e 198 }
AhmedPlaymaker 12:1e601b176437 199
AhmedPlaymaker 39:210ac915e0a0 200 void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad) {
AhmedPlaymaker 41:4edac50f010d 201 //Obtains the numbers inside the block.
AhmedPlaymaker 39:210ac915e0a0 202 b_number = _b.get_number();
AhmedPlaymaker 39:210ac915e0a0 203 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 39:210ac915e0a0 204 //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces
AhmedPlaymaker 39:210ac915e0a0 205 //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 206 for(int block=0; block<=83; block+=1) { //this loop automatically detects for each section of block and each combination of collision
AhmedPlaymaker 54:20abd16c7d74 207 if (((snakey == b_pos.y + 11)||
AhmedPlaymaker 54:20abd16c7d74 208 (snakey == b_pos.y + 10)||
AhmedPlaymaker 54:20abd16c7d74 209 (snakey == b_pos.y + 9)||
AhmedPlaymaker 54:20abd16c7d74 210 (snakey == b_pos.y + 8))&&
AhmedPlaymaker 54:20abd16c7d74 211 (snakex + 1 == b_pos.x + block)) {
AhmedPlaymaker 43:233f93860d08 212 //the or for the block's y position is due to the fact the exact y co-ordinate might not be collided if the snake's length has increased in the same itteration.
AhmedPlaymaker 39:210ac915e0a0 213 //printf("snake collision working \n");
AhmedPlaymaker 39:210ac915e0a0 214 //audio feedback
AhmedPlaymaker 39:210ac915e0a0 215 if(blocknum > 0) {b_pos.y = 0;} //change this to speed y = 0 when length = 10.
AhmedPlaymaker 39:210ac915e0a0 216 srn = CheckBlock(block); //this tells us which of the 5 blocks we are colliding with
AhmedPlaymaker 39:210ac915e0a0 217 blocknum = b_number[srn];
AhmedPlaymaker 41:4edac50f010d 218 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 219 velocity = 0;
AhmedPlaymaker 33:249cf423fb18 220 }
AhmedPlaymaker 39:210ac915e0a0 221 else {
AhmedPlaymaker 39:210ac915e0a0 222 velocity = 1;
AhmedPlaymaker 39:210ac915e0a0 223 }
AhmedPlaymaker 47:b448ffd073e7 224 ImplementCollision(pad);
AhmedPlaymaker 39:210ac915e0a0 225 SnakevsBlock::_set_velocity();
AhmedPlaymaker 12:1e601b176437 226 }
AhmedPlaymaker 39:210ac915e0a0 227 }
AhmedPlaymaker 39:210ac915e0a0 228 }
AhmedPlaymaker 39:210ac915e0a0 229
AhmedPlaymaker 39:210ac915e0a0 230 void SnakevsBlock::_set_velocity() {
AhmedPlaymaker 39:210ac915e0a0 231 _b.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 232 _f.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 233 _ff.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 234 _fff.velocity.y = velocity;
AhmedPlaymaker 27:3b4775a0d0bb 235 }
AhmedPlaymaker 29:c6358c39a70e 236
AhmedPlaymaker 29:c6358c39a70e 237 int SnakevsBlock::CheckBlock(int block) {
AhmedPlaymaker 29:c6358c39a70e 238 int srn;
AhmedPlaymaker 29:c6358c39a70e 239 if((block>=0)&&(block<=18)) {srn = 0;}
AhmedPlaymaker 29:c6358c39a70e 240 if((block>=19)&&(block<=34)) {srn = 1;}
AhmedPlaymaker 29:c6358c39a70e 241 if((block>=35)&&(block<=50)) {srn = 2;}
AhmedPlaymaker 29:c6358c39a70e 242 if((block>=51)&&(block<=66)) {srn = 3;}
AhmedPlaymaker 29:c6358c39a70e 243 if((block>=67)&&(block<=83)) {srn = 4;}
AhmedPlaymaker 29:c6358c39a70e 244 return srn;
AhmedPlaymaker 29:c6358c39a70e 245 }
AhmedPlaymaker 27:3b4775a0d0bb 246
AhmedPlaymaker 27:3b4775a0d0bb 247 void SnakevsBlock::ImplementCollision(Gamepad &pad) {
AhmedPlaymaker 42:973bb6036f81 248 send_block_number = 1;
AhmedPlaymaker 44:cd10d07ea1e5 249 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 250 _l.MinusLength();
AhmedPlaymaker 27:3b4775a0d0bb 251 pad.tone(1000.0,0.1);
AhmedPlaymaker 29:c6358c39a70e 252 wait(0.04);
AhmedPlaymaker 12:1e601b176437 253 }
AhmedPlaymaker 44:cd10d07ea1e5 254 blocknum-=1;
AhmedPlaymaker 13:9785f2404045 255 }
AhmedPlaymaker 13:9785f2404045 256
AhmedPlaymaker 38:30e4e6191762 257 void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad)
AhmedPlaymaker 42:973bb6036f81 258 {
AhmedPlaymaker 42:973bb6036f81 259 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 42:973bb6036f81 260 //of the blocks' sides and then stop the snake moving in x axis
AhmedPlaymaker 50:3cf9a94a264e 261 int length = _length;
AhmedPlaymaker 50:3cf9a94a264e 262 if(_length>=10) {length = 10;} //to stop the snake length virtually at 10 when it goes past it.
AhmedPlaymaker 37:ee47699915b8 263
AhmedPlaymaker 42:973bb6036f81 264 for(int i=0; i<=9; i++) {
AhmedPlaymaker 42:973bb6036f81 265 b[i] = 1;
AhmedPlaymaker 42:973bb6036f81 266 }
AhmedPlaymaker 37:ee47699915b8 267 for(int i=0; i<=9; i++) { //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 36:dfdd619874ae 268 for(int b_y_combination=0; b_y_combination<=10; b_y_combination++) {
AhmedPlaymaker 26:3495f7b0ede7 269 if (
AhmedPlaymaker 36:dfdd619874ae 270 (snake_pos[i].y == b_pos.y + b_y_combination) ||
AhmedPlaymaker 36:dfdd619874ae 271 (snake_pos[i].y + 1 == b_pos.y + b_y_combination) ||
AhmedPlaymaker 36:dfdd619874ae 272 (snake_pos[i].y + 2 == b_pos.y + b_y_combination)) {
AhmedPlaymaker 36:dfdd619874ae 273 for(int b_x_combination=2; b_x_combination<=82; b_x_combination+=16) {
AhmedPlaymaker 36:dfdd619874ae 274 //For West side of walls
AhmedPlaymaker 36:dfdd619874ae 275 if(
AhmedPlaymaker 36:dfdd619874ae 276 ((snake_pos[i].x == b_pos.x + b_x_combination+2) || //W
AhmedPlaymaker 50:3cf9a94a264e 277 (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(length > i) //W
AhmedPlaymaker 36:dfdd619874ae 278 ) {
AhmedPlaymaker 36:dfdd619874ae 279 //code makes sure that the colliding part doesn't move in x axis.
AhmedPlaymaker 37:ee47699915b8 280 for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) {
AhmedPlaymaker 50:3cf9a94a264e 281 if(length == snake_beed_num + i) {
AhmedPlaymaker 36:dfdd619874ae 282 b[snake_beed_num - 1] = 0;
AhmedPlaymaker 36:dfdd619874ae 283 }
AhmedPlaymaker 26:3495f7b0ede7 284 }
AhmedPlaymaker 25:e827f1a8fadc 285 }
AhmedPlaymaker 36:dfdd619874ae 286 //for East side of walls
AhmedPlaymaker 36:dfdd619874ae 287 else if (
AhmedPlaymaker 36:dfdd619874ae 288 ((snake_pos[i].x + 1 == b_x_combination) || //E
AhmedPlaymaker 50:3cf9a94a264e 289 (snake_pos[i].x + 2 == b_x_combination))&&(_d != W)&&(length > i) //E
AhmedPlaymaker 36:dfdd619874ae 290 ) {
AhmedPlaymaker 36:dfdd619874ae 291 //code makes sure that the colliding part doesn't move in x axis.
AhmedPlaymaker 37:ee47699915b8 292 for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) {
AhmedPlaymaker 50:3cf9a94a264e 293 if(length == snake_beed_num + i) {
AhmedPlaymaker 36:dfdd619874ae 294 b[snake_beed_num - 1] = 0;
AhmedPlaymaker 36:dfdd619874ae 295 }
AhmedPlaymaker 32:3a3bdeffdf62 296 }
AhmedPlaymaker 32:3a3bdeffdf62 297 }
AhmedPlaymaker 26:3495f7b0ede7 298 }
AhmedPlaymaker 25:e827f1a8fadc 299 }
AhmedPlaymaker 24:1c118b071430 300 }
AhmedPlaymaker 15:f4d069da093d 301 }
AhmedPlaymaker 22:ee698f66146f 302 }