Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Wed May 01 22:35:40 2019 +0000
Revision:
63:205f0ca48473
Parent:
62:ebf6ecf8a6d5
Child:
64:540aa1602372
Sorted out some functions, specifically related to their functionality

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 62:ebf6ecf8a6d5 22 for(int i=0; i<=14; i++) {
AhmedPlaymaker 62:ebf6ecf8a6d5 23 b[i] = 1; //makes all the snake beads move by default.
AhmedPlaymaker 62:ebf6ecf8a6d5 24 }
AhmedPlaymaker 49:441c32f6603e 25 SnakevsBlock::object_initialisations();
AhmedPlaymaker 7:48ba87cd79b5 26 }
AhmedPlaymaker 7:48ba87cd79b5 27
AhmedPlaymaker 41:4edac50f010d 28 void SnakevsBlock::reset()
AhmedPlaymaker 41:4edac50f010d 29 {
AhmedPlaymaker 41:4edac50f010d 30 //This prepares the game for the next level by reseting certain variables.
AhmedPlaymaker 41:4edac50f010d 31 foodbuff = 0;
AhmedPlaymaker 62:ebf6ecf8a6d5 32 if(blockgap >= 50) {
AhmedPlaymaker 62:ebf6ecf8a6d5 33 blockgap -= 10; //to make progressive levels harder by making the blocks drop more frequently.
AhmedPlaymaker 62:ebf6ecf8a6d5 34 }
AhmedPlaymaker 60:d3a9e0e4a0de 35 /*
AhmedPlaymaker 60:d3a9e0e4a0de 36 //to clear all the memory buffer of the game object positions from the previous level.
AhmedPlaymaker 60:d3a9e0e4a0de 37 for(int i = 0; i <=2 ; i++) {
AhmedPlaymaker 60:d3a9e0e4a0de 38 food_pos[i].x = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 39 food_pos[i].y = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 40 }
AhmedPlaymaker 60:d3a9e0e4a0de 41 b_pos.x = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 42 b_pos.y = 0;
AhmedPlaymaker 60:d3a9e0e4a0de 43 */
AhmedPlaymaker 49:441c32f6603e 44 SnakevsBlock::object_initialisations();
AhmedPlaymaker 49:441c32f6603e 45 }
AhmedPlaymaker 49:441c32f6603e 46
AhmedPlaymaker 49:441c32f6603e 47 void SnakevsBlock::object_initialisations()
AhmedPlaymaker 49:441c32f6603e 48 {
AhmedPlaymaker 49:441c32f6603e 49 _l.init(); //length calc object initialisation.
AhmedPlaymaker 49:441c32f6603e 50 _s.init(); //snake object initialisation.
AhmedPlaymaker 49:441c32f6603e 51 _f.init(); //food 1 object initialisation.
AhmedPlaymaker 49:441c32f6603e 52 _ff.init(); //food 2 object initialisation.
AhmedPlaymaker 49:441c32f6603e 53 _fff.init(); //food 3 object initialisation.
AhmedPlaymaker 49:441c32f6603e 54 _b.init(); //block object initialisation.
AhmedPlaymaker 41:4edac50f010d 55 }
AhmedPlaymaker 7:48ba87cd79b5 56
AhmedPlaymaker 39:210ac915e0a0 57 void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int g_mode)
AhmedPlaymaker 7:48ba87cd79b5 58 {
AhmedPlaymaker 63:205f0ca48473 59 SnakevsBlock::calculateTilt(pad, device);
AhmedPlaymaker 63:205f0ca48473 60
AhmedPlaymaker 56:142e9fdb77a8 61 switch (g_mode) {
AhmedPlaymaker 62:ebf6ecf8a6d5 62 case 1:
AhmedPlaymaker 62:ebf6ecf8a6d5 63 _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
AhmedPlaymaker 62:ebf6ecf8a6d5 64 break;
AhmedPlaymaker 62:ebf6ecf8a6d5 65 case 2:
AhmedPlaymaker 63:205f0ca48473 66 if (_tiltAngle >= 6) {
AhmedPlaymaker 62:ebf6ecf8a6d5 67 _d = E;
AhmedPlaymaker 63:205f0ca48473 68 } else if (_tiltAngle <= -6) {
AhmedPlaymaker 62:ebf6ecf8a6d5 69 _d = W;
AhmedPlaymaker 62:ebf6ecf8a6d5 70 } else {
AhmedPlaymaker 62:ebf6ecf8a6d5 71 _d = CENTRE;
AhmedPlaymaker 62:ebf6ecf8a6d5 72 }
AhmedPlaymaker 62:ebf6ecf8a6d5 73 break;
AhmedPlaymaker 38:30e4e6191762 74 }
AhmedPlaymaker 63:205f0ca48473 75 SnakevsBlock::lightTheLEDS(pad); //This function ligths the LEDS dependent on the direction of travel.
AhmedPlaymaker 38:30e4e6191762 76 //printf("%d",gm);
AhmedPlaymaker 63:205f0ca48473 77 //printf("%f",_tiltAngle);
AhmedPlaymaker 63:205f0ca48473 78 //printf("%f",device.get_roll_angle());
AhmedPlaymaker 63:205f0ca48473 79 }
AhmedPlaymaker 63:205f0ca48473 80
AhmedPlaymaker 63:205f0ca48473 81 void SnakevsBlock::calculateTilt(Gamepad &pad, FXOS8700CQ &device) //this just reads the angle of tilt required for motion contol and also processes it if A is pressed.
AhmedPlaymaker 63:205f0ca48473 82 {
AhmedPlaymaker 63:205f0ca48473 83 device.get_values();
AhmedPlaymaker 63:205f0ca48473 84 _tiltAngle = -device.get_roll_angle();
AhmedPlaymaker 63:205f0ca48473 85 //if button A is pressed then reset that particular position to center
AhmedPlaymaker 63:205f0ca48473 86 if (pad.check_event(Gamepad::A_PRESSED) == true) {
AhmedPlaymaker 63:205f0ca48473 87 garbage = _tiltAngle;
AhmedPlaymaker 63:205f0ca48473 88 }
AhmedPlaymaker 63:205f0ca48473 89 _tiltAngle = -device.get_roll_angle() - garbage;
AhmedPlaymaker 63:205f0ca48473 90
AhmedPlaymaker 63:205f0ca48473 91 //device.get_values();
AhmedPlaymaker 63:205f0ca48473 92 //printf("%f",_tiltAngle);
AhmedPlaymaker 38:30e4e6191762 93 //printf("%f",device.get_roll_angle());
AhmedPlaymaker 7:48ba87cd79b5 94 }
AhmedPlaymaker 7:48ba87cd79b5 95
AhmedPlaymaker 63:205f0ca48473 96 void SnakevsBlock::lightTheLEDS(Gamepad &pad) //This function ligths the LEDS dependent on the direction of travel.
AhmedPlaymaker 63:205f0ca48473 97 {
AhmedPlaymaker 63:205f0ca48473 98 if (_d == E) {
AhmedPlaymaker 63:205f0ca48473 99 for(int led = 4; led <= 6; led++) {
AhmedPlaymaker 63:205f0ca48473 100 pad.led(led,1);
AhmedPlaymaker 63:205f0ca48473 101 }
AhmedPlaymaker 63:205f0ca48473 102 } else if (_d == W) {
AhmedPlaymaker 63:205f0ca48473 103 for(int led = 1; led <= 3; led++) {
AhmedPlaymaker 63:205f0ca48473 104 pad.led(led,1);
AhmedPlaymaker 63:205f0ca48473 105 }
AhmedPlaymaker 63:205f0ca48473 106 } else {
AhmedPlaymaker 63:205f0ca48473 107 for(int led = 1; led <= 6; led++) {
AhmedPlaymaker 63:205f0ca48473 108 pad.led(led,0);
AhmedPlaymaker 63:205f0ca48473 109 }
AhmedPlaymaker 63:205f0ca48473 110 }
AhmedPlaymaker 63:205f0ca48473 111 }
AhmedPlaymaker 63:205f0ca48473 112
AhmedPlaymaker 62:ebf6ecf8a6d5 113 void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 114 {
AhmedPlaymaker 55:df0825049171 115 _length = _l._getLength(); //saves the snake length into a private variable.
AhmedPlaymaker 53:527cf297b088 116 _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 117 if(foodbuff >= 0) {
AhmedPlaymaker 56:142e9fdb77a8 118 _f.draw(lcd); //Draws the first food.
AhmedPlaymaker 39:210ac915e0a0 119 }
AhmedPlaymaker 41:4edac50f010d 120 if(foodbuff >= 50) {
AhmedPlaymaker 56:142e9fdb77a8 121 _ff.draw(lcd); //Draws the second food.
AhmedPlaymaker 9:d1d79d4ee673 122 }
AhmedPlaymaker 41:4edac50f010d 123 if(foodbuff >= 80) {
AhmedPlaymaker 56:142e9fdb77a8 124 _fff.draw(lcd); //Draws the third food.
AhmedPlaymaker 39:210ac915e0a0 125 }
AhmedPlaymaker 39:210ac915e0a0 126 foodbuff +=1;
AhmedPlaymaker 41:4edac50f010d 127 if(foodbuff >= 8) {
AhmedPlaymaker 48:d774bb162c61 128 _b.draw(lcd, _length);
AhmedPlaymaker 19:05cc9f801468 129 }
AhmedPlaymaker 7:48ba87cd79b5 130 //Code to print length on game screen.
AhmedPlaymaker 41:4edac50f010d 131 _l.print_length_on_screen(lcd);
AhmedPlaymaker 62:ebf6ecf8a6d5 132 }
AhmedPlaymaker 39:210ac915e0a0 133
AhmedPlaymaker 63:205f0ca48473 134 void SnakevsBlock::update(N5110 &lcd, Gamepad &pad) //Updates objects on screen.
AhmedPlaymaker 39:210ac915e0a0 135 {
AhmedPlaymaker 42:973bb6036f81 136 send_block_number = 0; //this is for the game to decide wether to remember the number on the block for the current itteration.
AhmedPlaymaker 62:ebf6ecf8a6d5 137 //we dont need to remember if it has already gone past the screen.
AhmedPlaymaker 63:205f0ca48473 138 SnakevsBlock::CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
AhmedPlaymaker 63:205f0ca48473 139 SnakevsBlock::CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
AhmedPlaymaker 63:205f0ca48473 140 SnakevsBlock::CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
AhmedPlaymaker 41:4edac50f010d 141 _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 142 _f.update();
AhmedPlaymaker 9:d1d79d4ee673 143 _ff.update();
AhmedPlaymaker 62:ebf6ecf8a6d5 144 _fff.update();
AhmedPlaymaker 48:d774bb162c61 145 _b.update(blocknum, blockgap, srn, send_block_number);
AhmedPlaymaker 63:205f0ca48473 146
AhmedPlaymaker 63:205f0ca48473 147 }
AhmedPlaymaker 63:205f0ca48473 148
AhmedPlaymaker 63:205f0ca48473 149 int SnakevsBlock::CheckGameProgression(N5110 &lcd, Gamepad &pad, SDFileSystem &sd)
AhmedPlaymaker 63:205f0ca48473 150 {
AhmedPlaymaker 63:205f0ca48473 151 //Function handles level progression and level failure operations by using the class WinLoose.
AhmedPlaymaker 49:441c32f6603e 152 if(_length == 0) {
AhmedPlaymaker 49:441c32f6603e 153 _wl.GameOver(lcd,pad);
AhmedPlaymaker 49:441c32f6603e 154 }
AhmedPlaymaker 63:205f0ca48473 155 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 156 back = 1;
AhmedPlaymaker 25:e827f1a8fadc 157 SnakevsBlock::init();
AhmedPlaymaker 62:ebf6ecf8a6d5 158 } else {
AhmedPlaymaker 25:e827f1a8fadc 159 back = 0;
AhmedPlaymaker 25:e827f1a8fadc 160 }
AhmedPlaymaker 52:c2faa96cf293 161 //printf("%d\n",_length);
AhmedPlaymaker 59:c65a2e933c47 162 if(_length >= _maxLength) {
AhmedPlaymaker 59:c65a2e933c47 163 _maxLength++;
AhmedPlaymaker 50:3cf9a94a264e 164 level = _wl.LevelComplete(lcd, pad, level);
AhmedPlaymaker 50:3cf9a94a264e 165 _Setstats.write(level, sd);
AhmedPlaymaker 63:205f0ca48473 166 //_statset.read(sd); //to read the currently stored value.
AhmedPlaymaker 63:205f0ca48473 167 SnakevsBlock::reset(); //reset prepares the game for the next level.
AhmedPlaymaker 50:3cf9a94a264e 168 }
AhmedPlaymaker 63:205f0ca48473 169 //_statset.read(sd); //to read the currently stored value.
AhmedPlaymaker 25:e827f1a8fadc 170 return back;
AhmedPlaymaker 7:48ba87cd79b5 171 }
AhmedPlaymaker 7:48ba87cd79b5 172
AhmedPlaymaker 7:48ba87cd79b5 173 void SnakevsBlock::get_pos()
AhmedPlaymaker 7:48ba87cd79b5 174 {
AhmedPlaymaker 7:48ba87cd79b5 175 //printf("player pos = %f %f \n", player_pos.x, player_pos.y); //top left of player sprite
AhmedPlaymaker 7:48ba87cd79b5 176 // 81.000000 0.000000 top right
AhmedPlaymaker 7:48ba87cd79b5 177 // 0.000000 0.000000 is top left
AhmedPlaymaker 7:48ba87cd79b5 178 // 81.000000 45.000000 bottom right
AhmedPlaymaker 62:ebf6ecf8a6d5 179 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 180 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 181 //printf("snakexy in GAME = %d %d \n", snakex, snakey);
AhmedPlaymaker 41:4edac50f010d 182 //Obtains all required coordinates.
AhmedPlaymaker 41:4edac50f010d 183 food_pos[0] = _f.get_pos();
AhmedPlaymaker 41:4edac50f010d 184 food_pos[1] = _ff.get_pos();
AhmedPlaymaker 41:4edac50f010d 185 food_pos[2] = _fff.get_pos();
AhmedPlaymaker 41:4edac50f010d 186 //obtains origin cordinates of block.
AhmedPlaymaker 41:4edac50f010d 187 b_pos = _b.get_pos();
AhmedPlaymaker 41:4edac50f010d 188 //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 189 snake_pos[0] = _s.get_pos(); //gets the position of the top beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 190 snake_pos[1] = _s.get_pos_before1(); //gets the position of the second beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 191 snake_pos[2] = _s.get_pos_before2(); //gets the position of the third beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 192 snake_pos[3] = _s.get_pos_before3(); //gets the position of the fourth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 193 snake_pos[4] = _s.get_pos_before4(); //gets the position of the fifth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 194 snake_pos[5] = _s.get_pos_before5(); //gets the position of the sixth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 195 snake_pos[6] = _s.get_pos_before6(); //gets the position of the seventh beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 196 snake_pos[7] = _s.get_pos_before7(); //gets the position of the eight beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 197 snake_pos[8] = _s.get_pos_before8(); //gets the position of the ninth beed and saves in array.
AhmedPlaymaker 41:4edac50f010d 198 snake_pos[9] = _s.get_pos_before9(); //gets the position of the last beed and saves in array.
AhmedPlaymaker 62:ebf6ecf8a6d5 199 }
AhmedPlaymaker 62:ebf6ecf8a6d5 200
AhmedPlaymaker 9:d1d79d4ee673 201
AhmedPlaymaker 62:ebf6ecf8a6d5 202 void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 203 {
AhmedPlaymaker 32:3a3bdeffdf62 204 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 32:3a3bdeffdf62 205 //of the three food sprites, if so then the food location is reset and
AhmedPlaymaker 32:3a3bdeffdf62 206 //length of the snake is increased using the length variable.
AhmedPlaymaker 29:c6358c39a70e 207 for(int y=0; y<=2; y++) { //this loop automatically detects each combination of collision in the y postion
AhmedPlaymaker 29:c6358c39a70e 208 for(int x=0; x<=2; x++) { //this loop automatically detects each combination of collision in the x postion
AhmedPlaymaker 29:c6358c39a70e 209 for(int food_sr=0; food_sr<=2; food_sr++) { //this loop automatically detects which food we are interacting with.
AhmedPlaymaker 29:c6358c39a70e 210 if (
AhmedPlaymaker 41:4edac50f010d 211 ((snakey + y == food_pos[food_sr].y) ||
AhmedPlaymaker 62:ebf6ecf8a6d5 212 (snakey + y == food_pos[food_sr].y + 1) ||
AhmedPlaymaker 62:ebf6ecf8a6d5 213 (snakey + y == food_pos[food_sr].y + 2)) &&
AhmedPlaymaker 41:4edac50f010d 214 ((snakex + x == food_pos[food_sr].x) ||
AhmedPlaymaker 62:ebf6ecf8a6d5 215 (snakex + x == food_pos[food_sr].x + 1) ||
AhmedPlaymaker 62:ebf6ecf8a6d5 216 (snakex + x == food_pos[food_sr].x + 2))
AhmedPlaymaker 29:c6358c39a70e 217 ) {
AhmedPlaymaker 29:c6358c39a70e 218 //printf("snake feast working \n");
AhmedPlaymaker 29:c6358c39a70e 219 //audio feedback
AhmedPlaymaker 46:dc7dccae9f9e 220 pad.tone(786.0,0.1);
AhmedPlaymaker 38:30e4e6191762 221 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 222 food_pos[food_sr].y = -3;
AhmedPlaymaker 41:4edac50f010d 223 _l.PlusLength();
AhmedPlaymaker 29:c6358c39a70e 224 }
AhmedPlaymaker 25:e827f1a8fadc 225 }
AhmedPlaymaker 25:e827f1a8fadc 226 }
AhmedPlaymaker 9:d1d79d4ee673 227 }
AhmedPlaymaker 29:c6358c39a70e 228 _f.set_pos(food_pos[0]);
AhmedPlaymaker 29:c6358c39a70e 229 _ff.set_pos(food_pos[1]);
AhmedPlaymaker 29:c6358c39a70e 230 _fff.set_pos(food_pos[2]);
AhmedPlaymaker 29:c6358c39a70e 231 }
AhmedPlaymaker 62:ebf6ecf8a6d5 232
AhmedPlaymaker 62:ebf6ecf8a6d5 233 void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 234 {
AhmedPlaymaker 41:4edac50f010d 235 //Obtains the numbers inside the block.
AhmedPlaymaker 39:210ac915e0a0 236 b_number = _b.get_number();
AhmedPlaymaker 39:210ac915e0a0 237 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 39:210ac915e0a0 238 //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces
AhmedPlaymaker 39:210ac915e0a0 239 //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 240 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 241 if (((snakey == b_pos.y + 11)||
AhmedPlaymaker 62:ebf6ecf8a6d5 242 (snakey == b_pos.y + 10)||
AhmedPlaymaker 62:ebf6ecf8a6d5 243 (snakey == b_pos.y + 9)||
AhmedPlaymaker 62:ebf6ecf8a6d5 244 (snakey == b_pos.y + 8))&&
AhmedPlaymaker 62:ebf6ecf8a6d5 245 (snakex + 1 == b_pos.x + block)) {
AhmedPlaymaker 43:233f93860d08 246 //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 247 //printf("snake collision working \n");
AhmedPlaymaker 39:210ac915e0a0 248 //audio feedback
AhmedPlaymaker 39:210ac915e0a0 249 srn = CheckBlock(block); //this tells us which of the 5 blocks we are colliding with
AhmedPlaymaker 63:205f0ca48473 250 blocknum = b_number[srn]; //this saves the number inside the colliding block into blocknum.
AhmedPlaymaker 41:4edac50f010d 251 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 63:205f0ca48473 252 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 63:205f0ca48473 253 } else { //is 10.
AhmedPlaymaker 39:210ac915e0a0 254 velocity = 1;
AhmedPlaymaker 39:210ac915e0a0 255 }
AhmedPlaymaker 63:205f0ca48473 256 ImplementCollision(pad); //this implements the collision once the conditions are met.
AhmedPlaymaker 63:205f0ca48473 257 SnakevsBlock::_set_velocity(); //sets the block and foods free or frezes them depending on snake level.
AhmedPlaymaker 12:1e601b176437 258 }
AhmedPlaymaker 39:210ac915e0a0 259 }
AhmedPlaymaker 39:210ac915e0a0 260 }
AhmedPlaymaker 62:ebf6ecf8a6d5 261
AhmedPlaymaker 62:ebf6ecf8a6d5 262 void SnakevsBlock::_set_velocity()
AhmedPlaymaker 62:ebf6ecf8a6d5 263 {
AhmedPlaymaker 39:210ac915e0a0 264 _b.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 265 _f.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 266 _ff.velocity.y = velocity;
AhmedPlaymaker 39:210ac915e0a0 267 _fff.velocity.y = velocity;
AhmedPlaymaker 27:3b4775a0d0bb 268 }
AhmedPlaymaker 62:ebf6ecf8a6d5 269
AhmedPlaymaker 62:ebf6ecf8a6d5 270 int SnakevsBlock::CheckBlock(int block)
AhmedPlaymaker 62:ebf6ecf8a6d5 271 {
AhmedPlaymaker 29:c6358c39a70e 272 int srn;
AhmedPlaymaker 62:ebf6ecf8a6d5 273 if((block>=0)&&(block<=18)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 274 srn = 0;
AhmedPlaymaker 62:ebf6ecf8a6d5 275 }
AhmedPlaymaker 62:ebf6ecf8a6d5 276 if((block>=19)&&(block<=34)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 277 srn = 1;
AhmedPlaymaker 62:ebf6ecf8a6d5 278 }
AhmedPlaymaker 62:ebf6ecf8a6d5 279 if((block>=35)&&(block<=50)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 280 srn = 2;
AhmedPlaymaker 62:ebf6ecf8a6d5 281 }
AhmedPlaymaker 62:ebf6ecf8a6d5 282 if((block>=51)&&(block<=66)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 283 srn = 3;
AhmedPlaymaker 62:ebf6ecf8a6d5 284 }
AhmedPlaymaker 62:ebf6ecf8a6d5 285 if((block>=67)&&(block<=83)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 286 srn = 4;
AhmedPlaymaker 62:ebf6ecf8a6d5 287 }
AhmedPlaymaker 29:c6358c39a70e 288 return srn;
AhmedPlaymaker 29:c6358c39a70e 289 }
AhmedPlaymaker 62:ebf6ecf8a6d5 290
AhmedPlaymaker 62:ebf6ecf8a6d5 291 void SnakevsBlock::ImplementCollision(Gamepad &pad)
AhmedPlaymaker 62:ebf6ecf8a6d5 292 {
AhmedPlaymaker 42:973bb6036f81 293 send_block_number = 1;
AhmedPlaymaker 44:cd10d07ea1e5 294 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 295 _l.MinusLength();
AhmedPlaymaker 63:205f0ca48473 296 pad.tone(432.0,0.1);
AhmedPlaymaker 29:c6358c39a70e 297 wait(0.04);
AhmedPlaymaker 12:1e601b176437 298 }
AhmedPlaymaker 44:cd10d07ea1e5 299 blocknum-=1;
AhmedPlaymaker 13:9785f2404045 300 }
AhmedPlaymaker 13:9785f2404045 301
AhmedPlaymaker 38:30e4e6191762 302 void SnakevsBlock::CheckSnakeBlockSidesCollision(Gamepad &pad)
AhmedPlaymaker 42:973bb6036f81 303 {
AhmedPlaymaker 42:973bb6036f81 304 //If statements check if the snake sprite has collided with any
AhmedPlaymaker 42:973bb6036f81 305 //of the blocks' sides and then stop the snake moving in x axis
AhmedPlaymaker 63:205f0ca48473 306
AhmedPlaymaker 63:205f0ca48473 307 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 308
AhmedPlaymaker 63:205f0ca48473 309 for(int i=0; i<=9; i++) { //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 63:205f0ca48473 310 SnakevsBlock::check_Collision_Y_axis(i); //checks if the snake and the block are at the same position in y axis.
AhmedPlaymaker 63:205f0ca48473 311 }
AhmedPlaymaker 63:205f0ca48473 312
AhmedPlaymaker 63:205f0ca48473 313 }
AhmedPlaymaker 63:205f0ca48473 314
AhmedPlaymaker 63:205f0ca48473 315 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 316 {
AhmedPlaymaker 42:973bb6036f81 317 for(int i=0; i<=9; i++) {
AhmedPlaymaker 42:973bb6036f81 318 b[i] = 1;
AhmedPlaymaker 42:973bb6036f81 319 }
AhmedPlaymaker 62:ebf6ecf8a6d5 320 }
AhmedPlaymaker 62:ebf6ecf8a6d5 321
AhmedPlaymaker 62:ebf6ecf8a6d5 322 void SnakevsBlock::check_Collision_Y_axis(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 323 {
AhmedPlaymaker 62:ebf6ecf8a6d5 324 //This code checks if the snake and the block overlap in the Y axis.
AhmedPlaymaker 63:205f0ca48473 325 for(int b_y_combination=0; b_y_combination<=10; b_y_combination++) { //this carries out the next stage if the Y axis collision criterion is met.
AhmedPlaymaker 62:ebf6ecf8a6d5 326 if (
AhmedPlaymaker 62:ebf6ecf8a6d5 327 (snake_pos[i].y == b_pos.y + b_y_combination) ||
AhmedPlaymaker 62:ebf6ecf8a6d5 328 (snake_pos[i].y + 1 == b_pos.y + b_y_combination) ||
AhmedPlaymaker 62:ebf6ecf8a6d5 329 (snake_pos[i].y + 2 == b_pos.y + b_y_combination)) {
AhmedPlaymaker 62:ebf6ecf8a6d5 330
AhmedPlaymaker 62:ebf6ecf8a6d5 331 SnakevsBlock::check_Collision_X_axis(i); //checks if the snake and the block are at the same position in x axis.
AhmedPlaymaker 62:ebf6ecf8a6d5 332
AhmedPlaymaker 62:ebf6ecf8a6d5 333 }
AhmedPlaymaker 62:ebf6ecf8a6d5 334 }
AhmedPlaymaker 62:ebf6ecf8a6d5 335 }
AhmedPlaymaker 62:ebf6ecf8a6d5 336
AhmedPlaymaker 62:ebf6ecf8a6d5 337 void SnakevsBlock::check_Collision_X_axis(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 338 {
AhmedPlaymaker 63:205f0ca48473 339 for(int b_x_combination=2; b_x_combination<=82; b_x_combination+=16) { //this carries out the next stage if the X axis collision criterion is met.
AhmedPlaymaker 62:ebf6ecf8a6d5 340
AhmedPlaymaker 62:ebf6ecf8a6d5 341 SnakevsBlock::checkCollision_EastorWest(b_x_combination, i); //checks if the colliding wall is on east side or west side.
AhmedPlaymaker 62:ebf6ecf8a6d5 342 }
AhmedPlaymaker 62:ebf6ecf8a6d5 343 }
AhmedPlaymaker 62:ebf6ecf8a6d5 344
AhmedPlaymaker 62:ebf6ecf8a6d5 345 void SnakevsBlock::checkCollision_EastorWest(int b_x_combination, int i) //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 62:ebf6ecf8a6d5 346 {
AhmedPlaymaker 62:ebf6ecf8a6d5 347 //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 62:ebf6ecf8a6d5 348 //on screen.
AhmedPlaymaker 62:ebf6ecf8a6d5 349 int length = _length;
AhmedPlaymaker 62:ebf6ecf8a6d5 350 if(_length>=10) {
AhmedPlaymaker 62:ebf6ecf8a6d5 351 length = 10; //to stop the snake length virtually at 10 when it goes past it.
AhmedPlaymaker 62:ebf6ecf8a6d5 352 }
AhmedPlaymaker 62:ebf6ecf8a6d5 353
AhmedPlaymaker 62:ebf6ecf8a6d5 354 //For West side of walls
AhmedPlaymaker 62:ebf6ecf8a6d5 355 if(
AhmedPlaymaker 62:ebf6ecf8a6d5 356 ((snake_pos[i].x == b_pos.x + b_x_combination+2) || //W
AhmedPlaymaker 62:ebf6ecf8a6d5 357 (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(length > i) //W
AhmedPlaymaker 62:ebf6ecf8a6d5 358 ) {
AhmedPlaymaker 62:ebf6ecf8a6d5 359 SnakevsBlock::StopX_AxisMotion(length, i);
AhmedPlaymaker 62:ebf6ecf8a6d5 360 }
AhmedPlaymaker 62:ebf6ecf8a6d5 361 //for East side of walls
AhmedPlaymaker 62:ebf6ecf8a6d5 362 else if (
AhmedPlaymaker 62:ebf6ecf8a6d5 363 ((snake_pos[i].x + 1 == b_x_combination) || //E
AhmedPlaymaker 62:ebf6ecf8a6d5 364 (snake_pos[i].x + 2 == b_x_combination))&&(_d != W)&&(length > i) //E
AhmedPlaymaker 62:ebf6ecf8a6d5 365 ) {
AhmedPlaymaker 62:ebf6ecf8a6d5 366 SnakevsBlock::StopX_AxisMotion(length, i);
AhmedPlaymaker 62:ebf6ecf8a6d5 367 }
AhmedPlaymaker 62:ebf6ecf8a6d5 368 }
AhmedPlaymaker 62:ebf6ecf8a6d5 369
AhmedPlaymaker 62:ebf6ecf8a6d5 370 void SnakevsBlock::StopX_AxisMotion(int length, 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 371 {
AhmedPlaymaker 62:ebf6ecf8a6d5 372 //code makes sure that the colliding part doesn't move in x axis.
AhmedPlaymaker 62:ebf6ecf8a6d5 373 for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) {
AhmedPlaymaker 62:ebf6ecf8a6d5 374 if(length == snake_beed_num + i) {
AhmedPlaymaker 62:ebf6ecf8a6d5 375 b[snake_beed_num - 1] = 0;
AhmedPlaymaker 24:1c118b071430 376 }
AhmedPlaymaker 15:f4d069da093d 377 }
AhmedPlaymaker 22:ee698f66146f 378 }