Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Diff: GameEngine/SnakevsBlock/SnakevsBlock.cpp
- Revision:
- 64:540aa1602372
- Parent:
- 63:205f0ca48473
- Child:
- 65:2872ca289b49
diff -r 205f0ca48473 -r 540aa1602372 GameEngine/SnakevsBlock/SnakevsBlock.cpp --- a/GameEngine/SnakevsBlock/SnakevsBlock.cpp Wed May 01 22:35:40 2019 +0000 +++ b/GameEngine/SnakevsBlock/SnakevsBlock.cpp Thu May 02 11:30:04 2019 +0000 @@ -16,7 +16,7 @@ level = 1; _maxLength = 10; // this makes us go to the next level if if maxLength is achieved; garbage = 0; //this is to allow the user to change the position of reference for motion control by saving the absolute angle. - foodbuff = 0; //this makes the food fall at diffrent times when a particular level starts. + _dropbuff = 0; //this makes the food fall at diffrent times when a particular level starts. 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). blockgap = 300; //this is the number of itterations the block will reccur after in the first level. for(int i=0; i<=14; i++) { @@ -28,10 +28,11 @@ void SnakevsBlock::reset() { //This prepares the game for the next level by reseting certain variables. - foodbuff = 0; + _dropbuff = 0; if(blockgap >= 50) { blockgap -= 10; //to make progressive levels harder by making the blocks drop more frequently. } + SnakevsBlock::object_initialisations(); /* //to clear all the memory buffer of the game object positions from the previous level. for(int i = 0; i <=2 ; i++) { @@ -41,7 +42,6 @@ b_pos.x = 0; b_pos.y = 0; */ - SnakevsBlock::object_initialisations(); } void SnakevsBlock::object_initialisations() @@ -114,17 +114,17 @@ { _length = _l._getLength(); //saves the snake length into a private variable. _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 - if(foodbuff >= 0) { - _f.draw(lcd); //Draws the first food. + if(_dropbuff >= 0) { + _f.draw(lcd); //Draws the first food after a loop delay of 0. } - if(foodbuff >= 50) { - _ff.draw(lcd); //Draws the second food. + if(_dropbuff >= 50) { + _ff.draw(lcd); //Draws the first food after a loop delay of 50. } - if(foodbuff >= 80) { - _fff.draw(lcd); //Draws the third food. + if(_dropbuff >= 80) { + _fff.draw(lcd); //Draws the first food after a loop delay of 80. } - foodbuff +=1; - if(foodbuff >= 8) { + _dropbuff +=1; + if(_dropbuff >= 8) { _b.draw(lcd, _length); } //Code to print length on game screen. @@ -344,34 +344,39 @@ void SnakevsBlock::checkCollision_EastorWest(int b_x_combination, int i) //i checks for all possible collisions with the snake respective to it's length. { - //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 - //on screen. - int length = _length; - if(_length>=10) { - length = 10; //to stop the snake length virtually at 10 when it goes past it. - } + SnakevsBlock::makeVirtualLengthMaxTen(); //stops the length at 10 for collision purposes. //For West side of walls if( ((snake_pos[i].x == b_pos.x + b_x_combination+2) || //W - (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(length > i) //W + (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(_virtualLength > i) //W ) { - SnakevsBlock::StopX_AxisMotion(length, i); + SnakevsBlock::StopX_AxisMotion(i); } //for East side of walls else if ( ((snake_pos[i].x + 1 == b_x_combination) || //E - (snake_pos[i].x + 2 == b_x_combination))&&(_d != W)&&(length > i) //E + (snake_pos[i].x + 2 == b_x_combination))&&(_d != W)&&(_virtualLength > i) //E ) { - SnakevsBlock::StopX_AxisMotion(length, i); + SnakevsBlock::StopX_AxisMotion(i); } } -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. +void SnakevsBlock::makeVirtualLengthMaxTen() +{ + //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 + //on screen. + _virtualLength = _length; + if(_length>=10) { + _virtualLength = 10; //to stop the snake length virtually at 10 when it goes past it. + } +} + +void SnakevsBlock::StopX_AxisMotion(int i) //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length. { //code makes sure that the colliding part doesn't move in x axis. for(int snake_beed_num=0; snake_beed_num<=10; snake_beed_num++) { - if(length == snake_beed_num + i) { + if(_virtualLength == snake_beed_num + i) { b[snake_beed_num - 1] = 0; } }