Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 09 14:52:19 2019 +0000
Revision:
104:17040265b7b4
Parent:
98:3061aa318fa2
Final Submission. I have read and agreed with Statement of Academic Integrity.

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