Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Mon May 06 10:49:50 2019 +0000
Revision:
82:c51ae8a501d1
Parent:
81:4c1641e10dcd
Child:
83:329da564799a
Improved barrier collision

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 67:39b9ba6019b0 15 SnakevsBlock::object_initialisations();
AhmedPlaymaker 41:4edac50f010d 16 //The level initialisation and all the other initial information passing will be done here
AhmedPlaymaker 19:05cc9f801468 17 level = 1;
AhmedPlaymaker 59:c65a2e933c47 18 _maxLength = 10; // this makes us go to the next level if if maxLength is achieved;
AhmedPlaymaker 41:4edac50f010d 19 garbage = 0; //this is to allow the user to change the position of reference for motion control by saving the absolute angle.
AhmedPlaymaker 64:540aa1602372 20 _dropbuff = 0; //this makes the food fall at diffrent times when a particular level starts.
AhmedPlaymaker 56:142e9fdb77a8 21 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 22 blockgap = 300; //this is the number of itterations the block will reccur after in the first level.
AhmedPlaymaker 70:7caab8069b9b 23
AhmedPlaymaker 74:7b6568bc16d5 24 //To make default motion of the snake free.
AhmedPlaymaker 71:4bd2b27693f3 25 for(int i=0; i<=9; i++) {
AhmedPlaymaker 70:7caab8069b9b 26 b[i] = 1;
AhmedPlaymaker 62:ebf6ecf8a6d5 27 }
AhmedPlaymaker 7:48ba87cd79b5 28 }
AhmedPlaymaker 7:48ba87cd79b5 29
AhmedPlaymaker 41:4edac50f010d 30 void SnakevsBlock::reset()
AhmedPlaymaker 41:4edac50f010d 31 {
AhmedPlaymaker 67:39b9ba6019b0 32 SnakevsBlock::object_initialisations();
AhmedPlaymaker 41:4edac50f010d 33 //This prepares the game for the next level by reseting certain variables.
AhmedPlaymaker 64:540aa1602372 34 _dropbuff = 0;
AhmedPlaymaker 67:39b9ba6019b0 35 if(blockgap >= 60) {
AhmedPlaymaker 62:ebf6ecf8a6d5 36 blockgap -= 10; //to make progressive levels harder by making the blocks drop more frequently.
AhmedPlaymaker 62:ebf6ecf8a6d5 37 }
AhmedPlaymaker 74:7b6568bc16d5 38 //To make default motion of the snake free.
AhmedPlaymaker 74:7b6568bc16d5 39 for(int i=0; i<=9; i++) {
AhmedPlaymaker 74:7b6568bc16d5 40 b[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 41 }
AhmedPlaymaker 49:441c32f6603e 42 }
AhmedPlaymaker 49:441c32f6603e 43
AhmedPlaymaker 49:441c32f6603e 44 void SnakevsBlock::object_initialisations()
AhmedPlaymaker 49:441c32f6603e 45 {
AhmedPlaymaker 49:441c32f6603e 46 _l.init(); //length calc object initialisation.
AhmedPlaymaker 68:b9cfd27987ac 47 _s.init(3,1); //snake object initialisation.
AhmedPlaymaker 49:441c32f6603e 48 _f.init(); //food 1 object initialisation.
AhmedPlaymaker 49:441c32f6603e 49 _ff.init(); //food 2 object initialisation.
AhmedPlaymaker 49:441c32f6603e 50 _fff.init(); //food 3 object initialisation.
AhmedPlaymaker 49:441c32f6603e 51 _b.init(); //block object initialisation.
AhmedPlaymaker 81:4c1641e10dcd 52 _barA.init(); //barrier A object initialisation.
AhmedPlaymaker 81:4c1641e10dcd 53 _barB.init(); //barrier B object initialisation.
AhmedPlaymaker 41:4edac50f010d 54 }
AhmedPlaymaker 7:48ba87cd79b5 55
AhmedPlaymaker 39:210ac915e0a0 56 void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int g_mode)
AhmedPlaymaker 7:48ba87cd79b5 57 {
AhmedPlaymaker 63:205f0ca48473 58 SnakevsBlock::calculateTilt(pad, device);
AhmedPlaymaker 63:205f0ca48473 59
AhmedPlaymaker 56:142e9fdb77a8 60 switch (g_mode) {
AhmedPlaymaker 62:ebf6ecf8a6d5 61 case 1:
AhmedPlaymaker 62:ebf6ecf8a6d5 62 _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
AhmedPlaymaker 62:ebf6ecf8a6d5 63 break;
AhmedPlaymaker 62:ebf6ecf8a6d5 64 case 2:
AhmedPlaymaker 80:51ca38c5dcdf 65 if (_tiltAngle >= 5) {
AhmedPlaymaker 62:ebf6ecf8a6d5 66 _d = E;
AhmedPlaymaker 80:51ca38c5dcdf 67 } else if (_tiltAngle <= -5) {
AhmedPlaymaker 62:ebf6ecf8a6d5 68 _d = W;
AhmedPlaymaker 62:ebf6ecf8a6d5 69 } else {
AhmedPlaymaker 62:ebf6ecf8a6d5 70 _d = CENTRE;
AhmedPlaymaker 62:ebf6ecf8a6d5 71 }
AhmedPlaymaker 62:ebf6ecf8a6d5 72 break;
AhmedPlaymaker 38:30e4e6191762 73 }
AhmedPlaymaker 63:205f0ca48473 74 SnakevsBlock::lightTheLEDS(pad); //This function ligths the LEDS dependent on the direction of travel.
AhmedPlaymaker 38:30e4e6191762 75 //printf("%d",gm);
AhmedPlaymaker 63:205f0ca48473 76 //printf("%f",_tiltAngle);
AhmedPlaymaker 63:205f0ca48473 77 //printf("%f",device.get_roll_angle());
AhmedPlaymaker 63:205f0ca48473 78 }
AhmedPlaymaker 63:205f0ca48473 79
AhmedPlaymaker 76:7fa91122907f 80 void SnakevsBlock::calculateTilt(Gamepad &pad, FXOS8700CQ &device) //this just reads the angle of tilt required for motion contol and also resets it if A is pressed.
AhmedPlaymaker 63:205f0ca48473 81 {
AhmedPlaymaker 63:205f0ca48473 82 device.get_values();
AhmedPlaymaker 63:205f0ca48473 83 _tiltAngle = -device.get_roll_angle();
AhmedPlaymaker 63:205f0ca48473 84 //if button A is pressed then reset that particular position to center
AhmedPlaymaker 63:205f0ca48473 85 if (pad.check_event(Gamepad::A_PRESSED) == true) {
AhmedPlaymaker 63:205f0ca48473 86 garbage = _tiltAngle;
AhmedPlaymaker 63:205f0ca48473 87 }
AhmedPlaymaker 63:205f0ca48473 88 _tiltAngle = -device.get_roll_angle() - garbage;
AhmedPlaymaker 63:205f0ca48473 89
AhmedPlaymaker 63:205f0ca48473 90 //device.get_values();
AhmedPlaymaker 63:205f0ca48473 91 //printf("%f",_tiltAngle);
AhmedPlaymaker 38:30e4e6191762 92 //printf("%f",device.get_roll_angle());
AhmedPlaymaker 7:48ba87cd79b5 93 }
AhmedPlaymaker 7:48ba87cd79b5 94
AhmedPlaymaker 63:205f0ca48473 95 void SnakevsBlock::lightTheLEDS(Gamepad &pad) //This function ligths the LEDS dependent on the direction of travel.
AhmedPlaymaker 63:205f0ca48473 96 {
AhmedPlaymaker 63:205f0ca48473 97 if (_d == E) {
AhmedPlaymaker 63:205f0ca48473 98 for(int led = 4; led <= 6; led++) {
AhmedPlaymaker 63:205f0ca48473 99 pad.led(led,1);
AhmedPlaymaker 63:205f0ca48473 100 }
AhmedPlaymaker 63:205f0ca48473 101 } else if (_d == W) {
AhmedPlaymaker 63:205f0ca48473 102 for(int led = 1; led <= 3; led++) {
AhmedPlaymaker 63:205f0ca48473 103 pad.led(led,1);
AhmedPlaymaker 63:205f0ca48473 104 }
AhmedPlaymaker 63:205f0ca48473 105 } else {
AhmedPlaymaker 63:205f0ca48473 106 for(int led = 1; led <= 6; led++) {
AhmedPlaymaker 63:205f0ca48473 107 pad.led(led,0);
AhmedPlaymaker 63:205f0ca48473 108 }
AhmedPlaymaker 63:205f0ca48473 109 }
AhmedPlaymaker 63:205f0ca48473 110 }
AhmedPlaymaker 63:205f0ca48473 111
AhmedPlaymaker 62:ebf6ecf8a6d5 112 void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 113 {
AhmedPlaymaker 55:df0825049171 114 _length = _l._getLength(); //saves the snake length into a private variable.
AhmedPlaymaker 70:7caab8069b9b 115 SnakevsBlock::makeVirtualLengthMaxTen(); //stops the length at 10 for collision and drawing purposes.
AhmedPlaymaker 79:35cb65c52d25 116 _s.draw(lcd, _virtualLength); //Draws the Snake and sends the value of length to the snake class, which is capped at 10.
AhmedPlaymaker 64:540aa1602372 117 if(_dropbuff >= 0) {
AhmedPlaymaker 64:540aa1602372 118 _f.draw(lcd); //Draws the first food after a loop delay of 0.
AhmedPlaymaker 39:210ac915e0a0 119 }
AhmedPlaymaker 64:540aa1602372 120 if(_dropbuff >= 50) {
AhmedPlaymaker 64:540aa1602372 121 _ff.draw(lcd); //Draws the first food after a loop delay of 50.
AhmedPlaymaker 9:d1d79d4ee673 122 }
AhmedPlaymaker 64:540aa1602372 123 if(_dropbuff >= 80) {
AhmedPlaymaker 64:540aa1602372 124 _fff.draw(lcd); //Draws the first food after a loop delay of 80.
AhmedPlaymaker 39:210ac915e0a0 125 }
AhmedPlaymaker 81:4c1641e10dcd 126 if(_dropbuff >= 22) {
AhmedPlaymaker 81:4c1641e10dcd 127 _barA.draw(lcd, b_pos.y); //Draws the first set of blocks after a loop delay of 8.
AhmedPlaymaker 81:4c1641e10dcd 128 }
AhmedPlaymaker 81:4c1641e10dcd 129 if(_dropbuff >= 40) {
AhmedPlaymaker 81:4c1641e10dcd 130 _barB.draw(lcd, b_pos.y); //Draws the first set of blocks after a loop delay of 8.
AhmedPlaymaker 81:4c1641e10dcd 131 }
AhmedPlaymaker 79:35cb65c52d25 132 if(_dropbuff >= 8) {
AhmedPlaymaker 79:35cb65c52d25 133 _b.draw(lcd, _length); //Draws the first set of blocks after a loop delay of 8.
AhmedPlaymaker 79:35cb65c52d25 134 }
AhmedPlaymaker 64:540aa1602372 135 _dropbuff +=1;
AhmedPlaymaker 79:35cb65c52d25 136 //Code to print length of snake on nokia screen.
AhmedPlaymaker 41:4edac50f010d 137 _l.print_length_on_screen(lcd);
AhmedPlaymaker 62:ebf6ecf8a6d5 138 }
AhmedPlaymaker 39:210ac915e0a0 139
AhmedPlaymaker 63:205f0ca48473 140 void SnakevsBlock::update(N5110 &lcd, Gamepad &pad) //Updates objects on screen.
AhmedPlaymaker 39:210ac915e0a0 141 {
AhmedPlaymaker 77:5c6bd659c32d 142 SnakevsBlock::MakeDefaultMotionFree(); //this makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision.
AhmedPlaymaker 76:7fa91122907f 143 send_block_number = 0; //this is for the game to decide wether to send the number on the block for the current itteration to the blaocks class.
AhmedPlaymaker 76:7fa91122907f 144 //we dont need to remember if it has already gone past the screen. The saved number in this changes if the snake collides with the block in CheckSnakeBlockCollision.
AhmedPlaymaker 63:205f0ca48473 145 SnakevsBlock::CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
AhmedPlaymaker 63:205f0ca48473 146 SnakevsBlock::CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
AhmedPlaymaker 63:205f0ca48473 147 SnakevsBlock::CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
AhmedPlaymaker 82:c51ae8a501d1 148 SnakevsBlock::CheckSnakeBarrierCollision(pad, 0); //Function checks for when the snake collides with barrier A.
AhmedPlaymaker 82:c51ae8a501d1 149 SnakevsBlock::CheckSnakeBarrierCollision(pad, 1); //Function checks for when the snake collides with barrier B.
AhmedPlaymaker 41:4edac50f010d 150 _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 151 _f.update();
AhmedPlaymaker 9:d1d79d4ee673 152 _ff.update();
AhmedPlaymaker 62:ebf6ecf8a6d5 153 _fff.update();
AhmedPlaymaker 48:d774bb162c61 154 _b.update(blocknum, blockgap, srn, send_block_number);
AhmedPlaymaker 81:4c1641e10dcd 155 _barA.update(blockgap);
AhmedPlaymaker 81:4c1641e10dcd 156 _barB.update(blockgap);
AhmedPlaymaker 63:205f0ca48473 157
AhmedPlaymaker 63:205f0ca48473 158 }
AhmedPlaymaker 63:205f0ca48473 159
AhmedPlaymaker 63:205f0ca48473 160 int SnakevsBlock::CheckGameProgression(N5110 &lcd, Gamepad &pad, SDFileSystem &sd)
AhmedPlaymaker 63:205f0ca48473 161 {
AhmedPlaymaker 63:205f0ca48473 162 //Function handles level progression and level failure operations by using the class WinLoose.
AhmedPlaymaker 49:441c32f6603e 163 if(_length == 0) {
AhmedPlaymaker 49:441c32f6603e 164 _wl.GameOver(lcd,pad);
AhmedPlaymaker 49:441c32f6603e 165 }
AhmedPlaymaker 63:205f0ca48473 166 if((pad.check_event(Gamepad::BACK_PRESSED))||(_length == 0)) { //Waits for Back button to be pressed or the length to reach 0.
AhmedPlaymaker 25:e827f1a8fadc 167 back = 1;
AhmedPlaymaker 25:e827f1a8fadc 168 SnakevsBlock::init();
AhmedPlaymaker 62:ebf6ecf8a6d5 169 } else {
AhmedPlaymaker 25:e827f1a8fadc 170 back = 0;
AhmedPlaymaker 25:e827f1a8fadc 171 }
AhmedPlaymaker 52:c2faa96cf293 172 //printf("%d\n",_length);
AhmedPlaymaker 59:c65a2e933c47 173 if(_length >= _maxLength) {
AhmedPlaymaker 59:c65a2e933c47 174 _maxLength++;
AhmedPlaymaker 50:3cf9a94a264e 175 level = _wl.LevelComplete(lcd, pad, level);
AhmedPlaymaker 50:3cf9a94a264e 176 _Setstats.write(level, sd);
AhmedPlaymaker 63:205f0ca48473 177 //_statset.read(sd); //to read the currently stored value.
AhmedPlaymaker 63:205f0ca48473 178 SnakevsBlock::reset(); //reset prepares the game for the next level.
AhmedPlaymaker 50:3cf9a94a264e 179 }
AhmedPlaymaker 63:205f0ca48473 180 //_statset.read(sd); //to read the currently stored value.
AhmedPlaymaker 25:e827f1a8fadc 181 return back;
AhmedPlaymaker 7:48ba87cd79b5 182 }
AhmedPlaymaker 7:48ba87cd79b5 183
AhmedPlaymaker 7:48ba87cd79b5 184 void SnakevsBlock::get_pos()
AhmedPlaymaker 7:48ba87cd79b5 185 {
AhmedPlaymaker 7:48ba87cd79b5 186 //printf("player pos = %f %f \n", player_pos.x, player_pos.y); //top left of player sprite
AhmedPlaymaker 7:48ba87cd79b5 187 // 81.000000 0.000000 top right
AhmedPlaymaker 7:48ba87cd79b5 188 // 0.000000 0.000000 is top left
AhmedPlaymaker 7:48ba87cd79b5 189 // 81.000000 45.000000 bottom right
AhmedPlaymaker 70:7caab8069b9b 190 snakex = _s.get_pos(0).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 70:7caab8069b9b 191 snakey = _s.get_pos(0).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 192 //printf("snakexy in GAME = %d %d \n", snakex, snakey);
AhmedPlaymaker 41:4edac50f010d 193 //Obtains all required coordinates.
AhmedPlaymaker 41:4edac50f010d 194 food_pos[0] = _f.get_pos();
AhmedPlaymaker 41:4edac50f010d 195 food_pos[1] = _ff.get_pos();
AhmedPlaymaker 41:4edac50f010d 196 food_pos[2] = _fff.get_pos();
AhmedPlaymaker 41:4edac50f010d 197 //obtains origin cordinates of block.
AhmedPlaymaker 41:4edac50f010d 198 b_pos = _b.get_pos();
AhmedPlaymaker 41:4edac50f010d 199 //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 71:4bd2b27693f3 200 for(int i = 0; i <= 9; i++) {
AhmedPlaymaker 71:4bd2b27693f3 201 snake_pos[i] = _s.get_pos(i); //gets the position of the each beed from the snake class and saves in array.
AhmedPlaymaker 71:4bd2b27693f3 202 }
AhmedPlaymaker 82:c51ae8a501d1 203 bar_pos[0] = _barA.get_pos(); //gets the position of the barrier A's origin from barriers class and saves in array.
AhmedPlaymaker 82:c51ae8a501d1 204 bar_pos[1] = _barB.get_pos(); //gets the position of the barrier B's origin from barriers class and saves in array.
AhmedPlaymaker 62:ebf6ecf8a6d5 205 }
AhmedPlaymaker 62:ebf6ecf8a6d5 206
AhmedPlaymaker 9:d1d79d4ee673 207
AhmedPlaymaker 62:ebf6ecf8a6d5 208 void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 209 {
AhmedPlaymaker 32:3a3bdeffdf62 210 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 32:3a3bdeffdf62 211 //of the three food sprites, if so then the food location is reset and
AhmedPlaymaker 32:3a3bdeffdf62 212 //length of the snake is increased using the length variable.
AhmedPlaymaker 65:2872ca289b49 213
AhmedPlaymaker 79:35cb65c52d25 214 for(int food_sr=0; food_sr<=2; food_sr++) { //this loop runs 3 times to detect collision with all the three food objects.
AhmedPlaymaker 79:35cb65c52d25 215 SnakevsBlock::CheckSnakeFoodYCollision(pad, food_sr);
AhmedPlaymaker 79:35cb65c52d25 216 }
AhmedPlaymaker 65:2872ca289b49 217
AhmedPlaymaker 29:c6358c39a70e 218 _f.set_pos(food_pos[0]);
AhmedPlaymaker 29:c6358c39a70e 219 _ff.set_pos(food_pos[1]);
AhmedPlaymaker 29:c6358c39a70e 220 _fff.set_pos(food_pos[2]);
AhmedPlaymaker 29:c6358c39a70e 221 }
AhmedPlaymaker 62:ebf6ecf8a6d5 222
AhmedPlaymaker 79:35cb65c52d25 223 void SnakevsBlock::CheckSnakeFoodYCollision(Gamepad &pad, int food_sr)
AhmedPlaymaker 65:2872ca289b49 224 {
AhmedPlaymaker 79:35cb65c52d25 225 for(int y=0; y<=2; y++) { //this loop automatically detects for each collision of food and snake in the y axis.
AhmedPlaymaker 65:2872ca289b49 226
AhmedPlaymaker 79:35cb65c52d25 227 if(
AhmedPlaymaker 79:35cb65c52d25 228 ((snakey + y == food_pos[food_sr].y) ||
AhmedPlaymaker 79:35cb65c52d25 229 (snakey + y == food_pos[food_sr].y + 1) ||
AhmedPlaymaker 79:35cb65c52d25 230 (snakey + y == food_pos[food_sr].y + 2))
AhmedPlaymaker 79:35cb65c52d25 231 ) {
AhmedPlaymaker 79:35cb65c52d25 232 SnakevsBlock::CheckSnakeFoodXCollision(pad, food_sr); //checks X collision only if Y collisison satisfies.
AhmedPlaymaker 79:35cb65c52d25 233 }
AhmedPlaymaker 65:2872ca289b49 234 }
AhmedPlaymaker 65:2872ca289b49 235
AhmedPlaymaker 65:2872ca289b49 236 }
AhmedPlaymaker 65:2872ca289b49 237
AhmedPlaymaker 79:35cb65c52d25 238 void SnakevsBlock::CheckSnakeFoodXCollision(Gamepad &pad, int food_sr)
AhmedPlaymaker 65:2872ca289b49 239 {
AhmedPlaymaker 79:35cb65c52d25 240
AhmedPlaymaker 79:35cb65c52d25 241 for(int x=0; x<=2; x++) { //this loop automatically detects for each collision of food and snake in the x axis.
AhmedPlaymaker 79:35cb65c52d25 242 if(
AhmedPlaymaker 65:2872ca289b49 243 ((snakex + x == food_pos[food_sr].x) ||
AhmedPlaymaker 65:2872ca289b49 244 (snakex + x == food_pos[food_sr].x + 1) ||
AhmedPlaymaker 65:2872ca289b49 245 (snakex + x == food_pos[food_sr].x + 2))
AhmedPlaymaker 65:2872ca289b49 246 ) {
AhmedPlaymaker 79:35cb65c52d25 247 SnakevsBlock::ImplementSnakeFoodCollision(pad, food_sr);
AhmedPlaymaker 65:2872ca289b49 248 }
AhmedPlaymaker 79:35cb65c52d25 249
AhmedPlaymaker 65:2872ca289b49 250 }
AhmedPlaymaker 65:2872ca289b49 251
AhmedPlaymaker 65:2872ca289b49 252 }
AhmedPlaymaker 65:2872ca289b49 253
AhmedPlaymaker 79:35cb65c52d25 254 void SnakevsBlock::ImplementSnakeFoodCollision(Gamepad &pad, int food_sr)
AhmedPlaymaker 79:35cb65c52d25 255 {
AhmedPlaymaker 79:35cb65c52d25 256 //printf("snake feast working \n");
AhmedPlaymaker 79:35cb65c52d25 257 //audio feedback
AhmedPlaymaker 79:35cb65c52d25 258 pad.tone(786.0,0.1);
AhmedPlaymaker 79:35cb65c52d25 259 _l.PlusLength();
AhmedPlaymaker 79:35cb65c52d25 260
AhmedPlaymaker 79:35cb65c52d25 261 ////////////RESET FOOD POSITION////////////
AhmedPlaymaker 79:35cb65c52d25 262 food_pos[food_sr].x = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis.
AhmedPlaymaker 79:35cb65c52d25 263 food_pos[food_sr].y = -3;
AhmedPlaymaker 79:35cb65c52d25 264
AhmedPlaymaker 79:35cb65c52d25 265 }
AhmedPlaymaker 79:35cb65c52d25 266
AhmedPlaymaker 62:ebf6ecf8a6d5 267 void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 268 {
AhmedPlaymaker 41:4edac50f010d 269 //Obtains the numbers inside the block.
AhmedPlaymaker 39:210ac915e0a0 270 b_number = _b.get_number();
AhmedPlaymaker 39:210ac915e0a0 271 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 39:210ac915e0a0 272 //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces
AhmedPlaymaker 39:210ac915e0a0 273 //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 274 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 275 if (((snakey == b_pos.y + 11)||
AhmedPlaymaker 62:ebf6ecf8a6d5 276 (snakey == b_pos.y + 10)||
AhmedPlaymaker 62:ebf6ecf8a6d5 277 (snakey == b_pos.y + 9)||
AhmedPlaymaker 62:ebf6ecf8a6d5 278 (snakey == b_pos.y + 8))&&
AhmedPlaymaker 62:ebf6ecf8a6d5 279 (snakex + 1 == b_pos.x + block)) {
AhmedPlaymaker 43:233f93860d08 280 //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 281 //printf("snake collision working \n");
AhmedPlaymaker 39:210ac915e0a0 282 //audio feedback
AhmedPlaymaker 39:210ac915e0a0 283 srn = CheckBlock(block); //this tells us which of the 5 blocks we are colliding with
AhmedPlaymaker 63:205f0ca48473 284 blocknum = b_number[srn]; //this saves the number inside the colliding block into blocknum.
AhmedPlaymaker 65:2872ca289b49 285 ImplementSnakeBlockCollision(pad); //this implements the collision once the conditions are met.
AhmedPlaymaker 65:2872ca289b49 286 SnakevsBlock::_setVelocity(srn); //sets the block and foods free or frezes them depending on snake length.
AhmedPlaymaker 12:1e601b176437 287 }
AhmedPlaymaker 39:210ac915e0a0 288 }
AhmedPlaymaker 39:210ac915e0a0 289 }
AhmedPlaymaker 62:ebf6ecf8a6d5 290
AhmedPlaymaker 65:2872ca289b49 291 void SnakevsBlock::_setVelocity(int srn)
AhmedPlaymaker 62:ebf6ecf8a6d5 292 {
AhmedPlaymaker 65:2872ca289b49 293 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 65:2872ca289b49 294 velocity = 0; //the block and food have to stop as if length of snake is 15 and it reaches 10 one by one, it stays at the same place, as max virtual length
AhmedPlaymaker 65:2872ca289b49 295 } else { //is 10.
AhmedPlaymaker 65:2872ca289b49 296 velocity = 1;
AhmedPlaymaker 65:2872ca289b49 297 }
AhmedPlaymaker 39:210ac915e0a0 298 _b.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 299 _f.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 300 _ff.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 301 _fff.velocity.y = velocity;
AhmedPlaymaker 27:3b4775a0d0bb 302 }
AhmedPlaymaker 62:ebf6ecf8a6d5 303
AhmedPlaymaker 62:ebf6ecf8a6d5 304 int SnakevsBlock::CheckBlock(int block)
AhmedPlaymaker 62:ebf6ecf8a6d5 305 {
AhmedPlaymaker 29:c6358c39a70e 306 int srn;
AhmedPlaymaker 62:ebf6ecf8a6d5 307 if((block>=0)&&(block<=18)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 308 srn = 0;
AhmedPlaymaker 62:ebf6ecf8a6d5 309 }
AhmedPlaymaker 62:ebf6ecf8a6d5 310 if((block>=19)&&(block<=34)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 311 srn = 1;
AhmedPlaymaker 62:ebf6ecf8a6d5 312 }
AhmedPlaymaker 62:ebf6ecf8a6d5 313 if((block>=35)&&(block<=50)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 314 srn = 2;
AhmedPlaymaker 62:ebf6ecf8a6d5 315 }
AhmedPlaymaker 62:ebf6ecf8a6d5 316 if((block>=51)&&(block<=66)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 317 srn = 3;
AhmedPlaymaker 62:ebf6ecf8a6d5 318 }
AhmedPlaymaker 62:ebf6ecf8a6d5 319 if((block>=67)&&(block<=83)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 320 srn = 4;
AhmedPlaymaker 62:ebf6ecf8a6d5 321 }
AhmedPlaymaker 29:c6358c39a70e 322 return srn;
AhmedPlaymaker 29:c6358c39a70e 323 }
AhmedPlaymaker 62:ebf6ecf8a6d5 324
AhmedPlaymaker 65:2872ca289b49 325 void SnakevsBlock::ImplementSnakeBlockCollision(Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 326 {
AhmedPlaymaker 42:973bb6036f81 327 send_block_number = 1;
AhmedPlaymaker 44:cd10d07ea1e5 328 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 329 _l.MinusLength();
AhmedPlaymaker 63:205f0ca48473 330 pad.tone(432.0,0.1);
AhmedPlaymaker 29:c6358c39a70e 331 wait(0.04);
AhmedPlaymaker 12:1e601b176437 332 }
AhmedPlaymaker 44:cd10d07ea1e5 333 blocknum-=1;
AhmedPlaymaker 13:9785f2404045 334 }
AhmedPlaymaker 13:9785f2404045 335
AhmedPlaymaker 38:30e4e6191762 336 void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad)
AhmedPlaymaker 42:973bb6036f81 337 {
AhmedPlaymaker 42:973bb6036f81 338 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 42:973bb6036f81 339 //of the blocks' sides and then stop the snake moving in x axis
AhmedPlaymaker 63:205f0ca48473 340
AhmedPlaymaker 63:205f0ca48473 341 for(int i=0; i<=9; i++) { //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 65:2872ca289b49 342 SnakevsBlock::CheckSnakeBlockSidesYCollision(i); //checks if the snake and the block are at the same position in y axis.
AhmedPlaymaker 63:205f0ca48473 343 }
AhmedPlaymaker 63:205f0ca48473 344
AhmedPlaymaker 63:205f0ca48473 345 }
AhmedPlaymaker 63:205f0ca48473 346
AhmedPlaymaker 63:205f0ca48473 347 void SnakevsBlock::MakeDefaultMotionFree() //this makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision.
AhmedPlaymaker 63:205f0ca48473 348 {
AhmedPlaymaker 42:973bb6036f81 349 for(int i=0; i<=9; i++) {
AhmedPlaymaker 42:973bb6036f81 350 b[i] = 1;
AhmedPlaymaker 42:973bb6036f81 351 }
AhmedPlaymaker 62:ebf6ecf8a6d5 352 }
AhmedPlaymaker 62:ebf6ecf8a6d5 353
AhmedPlaymaker 65:2872ca289b49 354 void SnakevsBlock::CheckSnakeBlockSidesYCollision(int i) //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 62:ebf6ecf8a6d5 355 {
AhmedPlaymaker 62:ebf6ecf8a6d5 356 //This code checks if the snake and the block overlap in the Y axis.
AhmedPlaymaker 79:35cb65c52d25 357 for(int Y=0; Y<=10; Y++) { //this carries out the next stage if the Y axis collision criterion is met.
AhmedPlaymaker 62:ebf6ecf8a6d5 358 if (
AhmedPlaymaker 79:35cb65c52d25 359 (snake_pos[i].y == b_pos.y + Y) ||
AhmedPlaymaker 79:35cb65c52d25 360 (snake_pos[i].y + 1 == b_pos.y + Y) ||
AhmedPlaymaker 79:35cb65c52d25 361 (snake_pos[i].y + 2 == b_pos.y + Y)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 362
AhmedPlaymaker 65:2872ca289b49 363 SnakevsBlock::CheckSnakeBlockSidesXCollision(i); //checks if the snake and the block are at the same position in x axis.
AhmedPlaymaker 62:ebf6ecf8a6d5 364
AhmedPlaymaker 62:ebf6ecf8a6d5 365 }
AhmedPlaymaker 62:ebf6ecf8a6d5 366 }
AhmedPlaymaker 62:ebf6ecf8a6d5 367 }
AhmedPlaymaker 62:ebf6ecf8a6d5 368
AhmedPlaymaker 65:2872ca289b49 369 void SnakevsBlock::CheckSnakeBlockSidesXCollision(int i) //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 62:ebf6ecf8a6d5 370 {
AhmedPlaymaker 79:35cb65c52d25 371 for(int X=3; X<=83; X+=16) { //this creates a loop in which each barrier is checked for in each loop using the following functions, by running loops
AhmedPlaymaker 79:35cb65c52d25 372 //in relation to where the sides of the blocks are situated in the X axis.
AhmedPlaymaker 62:ebf6ecf8a6d5 373
AhmedPlaymaker 79:35cb65c52d25 374 SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(X, i); //checks if the colliding wall is on east side or west side.
AhmedPlaymaker 79:35cb65c52d25 375 //X is sent because every barrier is in a diffrent position and W/E collision happen at either side of these and therefore cannot have a common X collision.
AhmedPlaymaker 62:ebf6ecf8a6d5 376 }
AhmedPlaymaker 62:ebf6ecf8a6d5 377 }
AhmedPlaymaker 62:ebf6ecf8a6d5 378
AhmedPlaymaker 79:35cb65c52d25 379 void SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(int X, int i) //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 62:ebf6ecf8a6d5 380 {
AhmedPlaymaker 70:7caab8069b9b 381 //For West side of walls
AhmedPlaymaker 62:ebf6ecf8a6d5 382 if(
AhmedPlaymaker 79:35cb65c52d25 383 ((snake_pos[i].x == X + 1) || //W
AhmedPlaymaker 79:35cb65c52d25 384 (snake_pos[i].x + 1 == X + 1))&&(_d != E)&&(_virtualLength > i) //W
AhmedPlaymaker 62:ebf6ecf8a6d5 385 ) {
AhmedPlaymaker 82:c51ae8a501d1 386 SnakevsBlock::ImplementBarrierCollision(i);
AhmedPlaymaker 62:ebf6ecf8a6d5 387 }
AhmedPlaymaker 62:ebf6ecf8a6d5 388 //for East side of walls
AhmedPlaymaker 62:ebf6ecf8a6d5 389 else if (
AhmedPlaymaker 79:35cb65c52d25 390 ((snake_pos[i].x + 1 == X - 1) || //E
AhmedPlaymaker 79:35cb65c52d25 391 (snake_pos[i].x + 2 == X - 1))&&(_d != W)&&(_virtualLength > i) //E
AhmedPlaymaker 62:ebf6ecf8a6d5 392 ) {
AhmedPlaymaker 82:c51ae8a501d1 393 SnakevsBlock::ImplementBarrierCollision(i);
AhmedPlaymaker 62:ebf6ecf8a6d5 394 }
AhmedPlaymaker 62:ebf6ecf8a6d5 395 }
AhmedPlaymaker 62:ebf6ecf8a6d5 396
AhmedPlaymaker 82:c51ae8a501d1 397
AhmedPlaymaker 82:c51ae8a501d1 398 void SnakevsBlock::CheckSnakeBarrierCollision(Gamepad &pad, int bar_sr_no)
AhmedPlaymaker 82:c51ae8a501d1 399 {
AhmedPlaymaker 82:c51ae8a501d1 400 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 82:c51ae8a501d1 401 //of the barrier's sides and then stop the snake moving in x axis
AhmedPlaymaker 82:c51ae8a501d1 402
AhmedPlaymaker 82:c51ae8a501d1 403 for(int i=0; i<=9; i++) { //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 82:c51ae8a501d1 404 SnakevsBlock::CheckSnakeBarrierYCollision(i, bar_sr_no); //checks if the snake and the block are at the same position in y axis.
AhmedPlaymaker 82:c51ae8a501d1 405 }
AhmedPlaymaker 82:c51ae8a501d1 406
AhmedPlaymaker 82:c51ae8a501d1 407 }
AhmedPlaymaker 82:c51ae8a501d1 408
AhmedPlaymaker 82:c51ae8a501d1 409 void SnakevsBlock::CheckSnakeBarrierYCollision(int i, int bar_sr_no) //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 82:c51ae8a501d1 410 {
AhmedPlaymaker 82:c51ae8a501d1 411 //This code checks if the snake and the block overlap in the Y axis.
AhmedPlaymaker 82:c51ae8a501d1 412 for(int Y=0; Y<=21; Y++) { //this carries out the next stage if the Y axis collision criterion is met.
AhmedPlaymaker 82:c51ae8a501d1 413 if (
AhmedPlaymaker 82:c51ae8a501d1 414 (snake_pos[i].y == bar_pos[bar_sr_no].y + Y) ||
AhmedPlaymaker 82:c51ae8a501d1 415 (snake_pos[i].y + 1 == bar_pos[bar_sr_no].y + Y) ||
AhmedPlaymaker 82:c51ae8a501d1 416 (snake_pos[i].y + 2 == bar_pos[bar_sr_no].y + Y)
AhmedPlaymaker 82:c51ae8a501d1 417 ) {
AhmedPlaymaker 82:c51ae8a501d1 418 SnakevsBlock::CheckSnakeBarrierEastWestCollision(i, bar_sr_no); //checks if the colliding barrier is on east side or west side.
AhmedPlaymaker 82:c51ae8a501d1 419
AhmedPlaymaker 82:c51ae8a501d1 420 }
AhmedPlaymaker 82:c51ae8a501d1 421 }
AhmedPlaymaker 82:c51ae8a501d1 422 }
AhmedPlaymaker 82:c51ae8a501d1 423
AhmedPlaymaker 82:c51ae8a501d1 424 void SnakevsBlock::CheckSnakeBarrierEastWestCollision(int i, int bar_sr_no) //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 82:c51ae8a501d1 425 {
AhmedPlaymaker 82:c51ae8a501d1 426 //bar_pos[0].x and bar_pos[1].x are used to confirm collision of snake with the barriers in the X axis.
AhmedPlaymaker 82:c51ae8a501d1 427 //For West side of walls
AhmedPlaymaker 82:c51ae8a501d1 428 if(
AhmedPlaymaker 82:c51ae8a501d1 429 ((snake_pos[i].x == bar_pos[bar_sr_no].x + 1) || //W
AhmedPlaymaker 82:c51ae8a501d1 430 (snake_pos[i].x + 1 == bar_pos[bar_sr_no].x + 1))&&(_d != E)&&(_virtualLength > i) //W
AhmedPlaymaker 82:c51ae8a501d1 431 ) {
AhmedPlaymaker 82:c51ae8a501d1 432 SnakevsBlock::ImplementBarrierCollision(i);
AhmedPlaymaker 82:c51ae8a501d1 433 }
AhmedPlaymaker 82:c51ae8a501d1 434 //for East side of walls
AhmedPlaymaker 82:c51ae8a501d1 435 else if (
AhmedPlaymaker 82:c51ae8a501d1 436 ((snake_pos[i].x + 1 == bar_pos[bar_sr_no].x - 1) || //E
AhmedPlaymaker 82:c51ae8a501d1 437 (snake_pos[i].x + 2 == bar_pos[bar_sr_no].x - 1))&&(_d != W)&&(_virtualLength > i) //E
AhmedPlaymaker 82:c51ae8a501d1 438 ) {
AhmedPlaymaker 82:c51ae8a501d1 439 SnakevsBlock::ImplementBarrierCollision(i);
AhmedPlaymaker 82:c51ae8a501d1 440 }
AhmedPlaymaker 82:c51ae8a501d1 441 }
AhmedPlaymaker 82:c51ae8a501d1 442
AhmedPlaymaker 82:c51ae8a501d1 443
AhmedPlaymaker 82:c51ae8a501d1 444 ////////////////////////////USED IN BOTH BLOCK SIDES COLLISION AND BARRIER COLLISION////////////////////////////
AhmedPlaymaker 82:c51ae8a501d1 445
AhmedPlaymaker 82:c51ae8a501d1 446 //Also used to send length to snake class as the max length drawn must also be 10
AhmedPlaymaker 64:540aa1602372 447 void SnakevsBlock::makeVirtualLengthMaxTen()
AhmedPlaymaker 64:540aa1602372 448 {
AhmedPlaymaker 64:540aa1602372 449 //this makes the virtual length -> 10 for the side collision implementation because if the length is fifteen and the last beed collides, it still is the 10th beed
AhmedPlaymaker 64:540aa1602372 450 //on screen.
AhmedPlaymaker 64:540aa1602372 451 _virtualLength = _length;
AhmedPlaymaker 71:4bd2b27693f3 452 if(_length >= 10) {
AhmedPlaymaker 64:540aa1602372 453 _virtualLength = 10; //to stop the snake length virtually at 10 when it goes past it.
AhmedPlaymaker 64:540aa1602372 454 }
AhmedPlaymaker 64:540aa1602372 455 }
AhmedPlaymaker 82:c51ae8a501d1 456 //Implements both SnakeBlockSides and SnakeBarrier Collisions.
AhmedPlaymaker 82:c51ae8a501d1 457 void SnakevsBlock::ImplementBarrierCollision(int i) //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 62:ebf6ecf8a6d5 458 {
AhmedPlaymaker 62:ebf6ecf8a6d5 459 //code makes sure that the colliding part doesn't move in x axis.
AhmedPlaymaker 62:ebf6ecf8a6d5 460 for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) {
AhmedPlaymaker 64:540aa1602372 461 if(_virtualLength == snake_beed_num + i) {
AhmedPlaymaker 62:ebf6ecf8a6d5 462 b[snake_beed_num - 1] = 0;
AhmedPlaymaker 24:1c118b071430 463 }
AhmedPlaymaker 15:f4d069da093d 464 }
AhmedPlaymaker 22:ee698f66146f 465 }