Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Diff: GameEngine/SnakevsBlock/SnakevsBlock.cpp
- Revision:
- 82:c51ae8a501d1
- Parent:
- 81:4c1641e10dcd
- Child:
- 83:329da564799a
--- a/GameEngine/SnakevsBlock/SnakevsBlock.cpp Mon May 06 09:05:09 2019 +0000 +++ b/GameEngine/SnakevsBlock/SnakevsBlock.cpp Mon May 06 10:49:50 2019 +0000 @@ -145,6 +145,8 @@ SnakevsBlock::CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food. SnakevsBlock::CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks. SnakevsBlock::CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides. + SnakevsBlock::CheckSnakeBarrierCollision(pad, 0); //Function checks for when the snake collides with barrier A. + SnakevsBlock::CheckSnakeBarrierCollision(pad, 1); //Function checks for when the snake collides with barrier B. _s.update(_d, b); //_d is the direction of joystick and b controls the motion of a section of the snake relative to obstruction _f.update(); _ff.update(); @@ -198,6 +200,8 @@ for(int i = 0; i <= 9; i++) { snake_pos[i] = _s.get_pos(i); //gets the position of the each beed from the snake class and saves in array. } + bar_pos[0] = _barA.get_pos(); //gets the position of the barrier A's origin from barriers class and saves in array. + bar_pos[1] = _barB.get_pos(); //gets the position of the barrier B's origin from barriers class and saves in array. } @@ -379,17 +383,67 @@ ((snake_pos[i].x == X + 1) || //W (snake_pos[i].x + 1 == X + 1))&&(_d != E)&&(_virtualLength > i) //W ) { - SnakevsBlock::ImplementSnakeBlockSidesCollision(i); + SnakevsBlock::ImplementBarrierCollision(i); } //for East side of walls else if ( ((snake_pos[i].x + 1 == X - 1) || //E (snake_pos[i].x + 2 == X - 1))&&(_d != W)&&(_virtualLength > i) //E ) { - SnakevsBlock::ImplementSnakeBlockSidesCollision(i); + SnakevsBlock::ImplementBarrierCollision(i); } } + +void SnakevsBlock::CheckSnakeBarrierCollision(Gamepad &pad, int bar_sr_no) +{ + //If statements check if the snake sprite has collided with any + //of the barrier's sides and then stop the snake moving in x axis + + for(int i=0; i<=9; i++) { //i checks for all possible collisions with the snake respective to it's length. + SnakevsBlock::CheckSnakeBarrierYCollision(i, bar_sr_no); //checks if the snake and the block are at the same position in y axis. + } + +} + +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. +{ + //This code checks if the snake and the block overlap in the Y axis. + for(int Y=0; Y<=21; Y++) { //this carries out the next stage if the Y axis collision criterion is met. + if ( + (snake_pos[i].y == bar_pos[bar_sr_no].y + Y) || + (snake_pos[i].y + 1 == bar_pos[bar_sr_no].y + Y) || + (snake_pos[i].y + 2 == bar_pos[bar_sr_no].y + Y) + ) { + SnakevsBlock::CheckSnakeBarrierEastWestCollision(i, bar_sr_no); //checks if the colliding barrier is on east side or west side. + + } + } +} + +void SnakevsBlock::CheckSnakeBarrierEastWestCollision(int i, int bar_sr_no) //i checks for all possible collisions with the snake respective to it's length. +{ + //bar_pos[0].x and bar_pos[1].x are used to confirm collision of snake with the barriers in the X axis. + //For West side of walls + if( + ((snake_pos[i].x == bar_pos[bar_sr_no].x + 1) || //W + (snake_pos[i].x + 1 == bar_pos[bar_sr_no].x + 1))&&(_d != E)&&(_virtualLength > i) //W + ) { + SnakevsBlock::ImplementBarrierCollision(i); + } + //for East side of walls + else if ( + ((snake_pos[i].x + 1 == bar_pos[bar_sr_no].x - 1) || //E + (snake_pos[i].x + 2 == bar_pos[bar_sr_no].x - 1))&&(_d != W)&&(_virtualLength > i) //E + ) { + SnakevsBlock::ImplementBarrierCollision(i); + } +} + + +////////////////////////////USED IN BOTH BLOCK SIDES COLLISION AND BARRIER COLLISION//////////////////////////// + +//Also used to send length to snake class as the max length drawn must also be 10 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 @@ -399,8 +453,8 @@ _virtualLength = 10; //to stop the snake length virtually at 10 when it goes past it. } } - -void SnakevsBlock::ImplementSnakeBlockSidesCollision(int i) //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length. +//Implements both SnakeBlockSides and SnakeBarrier Collisions. +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. { //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++) {