Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Diff: GameEngine/SnakevsBlock/SnakevsBlock.cpp
- Revision:
- 79:35cb65c52d25
- Parent:
- 77:5c6bd659c32d
- Child:
- 80:51ca38c5dcdf
--- a/GameEngine/SnakevsBlock/SnakevsBlock.cpp Sun May 05 17:22:48 2019 +0000 +++ b/GameEngine/SnakevsBlock/SnakevsBlock.cpp Sun May 05 23:48:57 2019 +0000 @@ -111,7 +111,7 @@ { _length = _l._getLength(); //saves the snake length into a private variable. SnakevsBlock::makeVirtualLengthMaxTen(); //stops the length at 10 for collision and drawing purposes. - _s.draw(lcd, _virtualLength); //Draws the Snake. + _s.draw(lcd, _virtualLength); //Draws the Snake and sends the value of length to the snake class, which is capped at 10. if(_dropbuff >= 0) { _f.draw(lcd); //Draws the first food after a loop delay of 0. } @@ -121,11 +121,11 @@ if(_dropbuff >= 80) { _fff.draw(lcd); //Draws the first food after a loop delay of 80. } + if(_dropbuff >= 8) { + _b.draw(lcd, _length); //Draws the first set of blocks after a loop delay of 8. + } _dropbuff +=1; - if(_dropbuff >= 8) { - _b.draw(lcd, _length); - } - //Code to print length on game screen. + //Code to print length of snake on nokia screen. _l.print_length_on_screen(lcd); } @@ -197,52 +197,59 @@ //of the three food sprites, if so then the food location is reset and //length of the snake is increased using the length variable. - SnakevsBlock::CheckSnakeFoodYCollision(pad); + for(int food_sr=0; food_sr<=2; food_sr++) { //this loop runs 3 times to detect collision with all the three food objects. + SnakevsBlock::CheckSnakeFoodYCollision(pad, food_sr); + } _f.set_pos(food_pos[0]); _ff.set_pos(food_pos[1]); _fff.set_pos(food_pos[2]); } -void SnakevsBlock::CheckSnakeFoodYCollision(Gamepad &pad) +void SnakevsBlock::CheckSnakeFoodYCollision(Gamepad &pad, int food_sr) { - for(int y=0; y<=2; y++) { //this loop automatically creates each coordinate of collision in the y postion that will be used in ImplementSnakeFoodCollision(pad, x, y). - SnakevsBlock::CheckSnakeFoodXCollision(pad, y); - } + for(int y=0; y<=2; y++) { //this loop automatically detects for each collision of food and snake in the y axis. -} - -void SnakevsBlock::CheckSnakeFoodXCollision(Gamepad &pad, int y) -{ - - for(int x=0; x<=2; x++) { //this loop automatically creates each coordinate of collision in the x postion that will be used in ImplementSnakeFoodCollision(pad, x, y). - SnakevsBlock::ImplementSnakeFoodCollision(pad, x, y); + if( + ((snakey + y == food_pos[food_sr].y) || + (snakey + y == food_pos[food_sr].y + 1) || + (snakey + y == food_pos[food_sr].y + 2)) + ) { + SnakevsBlock::CheckSnakeFoodXCollision(pad, food_sr); //checks X collision only if Y collisison satisfies. + } } } -void SnakevsBlock::ImplementSnakeFoodCollision(Gamepad &pad, int x, int y) +void SnakevsBlock::CheckSnakeFoodXCollision(Gamepad &pad, int food_sr) { - for(int food_sr=0; food_sr<=2; food_sr++) { //this loop automatically detects which food we are interacting with. - if ( - ((snakey + y == food_pos[food_sr].y) || - (snakey + y == food_pos[food_sr].y + 1) || - (snakey + y == food_pos[food_sr].y + 2)) && + + for(int x=0; x<=2; x++) { //this loop automatically detects for each collision of food and snake in the x axis. + if( ((snakex + x == food_pos[food_sr].x) || (snakex + x == food_pos[food_sr].x + 1) || (snakex + x == food_pos[food_sr].x + 2)) ) { - //printf("snake feast working \n"); - //audio feedback - pad.tone(786.0,0.1); - food_pos[food_sr].x = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis. - food_pos[food_sr].y = -3; - _l.PlusLength(); + SnakevsBlock::ImplementSnakeFoodCollision(pad, food_sr); } + } } +void SnakevsBlock::ImplementSnakeFoodCollision(Gamepad &pad, int food_sr) +{ + //printf("snake feast working \n"); + //audio feedback + pad.tone(786.0,0.1); + _l.PlusLength(); + + ////////////RESET FOOD POSITION//////////// + food_pos[food_sr].x = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis. + food_pos[food_sr].y = -3; + +} + void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad) { //Obtains the numbers inside the block. @@ -333,11 +340,11 @@ 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. { //This code checks if the snake and the block overlap in the Y axis. - 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. + for(int Y=0; Y<=10; Y++) { //this carries out the next stage if the Y axis collision criterion is met. if ( - (snake_pos[i].y == b_pos.y + b_y_combination) || - (snake_pos[i].y + 1 == b_pos.y + b_y_combination) || - (snake_pos[i].y + 2 == b_pos.y + b_y_combination)) { + (snake_pos[i].y == b_pos.y + Y) || + (snake_pos[i].y + 1 == b_pos.y + Y) || + (snake_pos[i].y + 2 == b_pos.y + Y)) { SnakevsBlock::CheckSnakeBlockSidesXCollision(i); //checks if the snake and the block are at the same position in x axis. @@ -347,25 +354,27 @@ 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. { - 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. + 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 + //in relation to where the sides of the blocks are situated in the X axis. - SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(b_x_combination, i); //checks if the colliding wall is on east side or west side. + SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(X, i); //checks if the colliding wall is on east side or west side. + //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. } } -void SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(int b_x_combination, int i) //i checks for all possible collisions with the snake respective to it's length. +void SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(int X, int i) //i checks for all possible collisions with the snake respective to it's length. { //For West side of walls if( - ((snake_pos[i].x == b_x_combination + 2) || //W - (snake_pos[i].x + 1 == b_x_combination + 2))&&(_d != E)&&(_virtualLength > i) //W + ((snake_pos[i].x == X + 1) || //W + (snake_pos[i].x + 1 == X + 1))&&(_d != E)&&(_virtualLength > i) //W ) { SnakevsBlock::ImplementSnakeBlockSidesCollision(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)&&(_virtualLength > i) //E + ((snake_pos[i].x + 1 == X - 1) || //E + (snake_pos[i].x + 2 == X - 1))&&(_d != W)&&(_virtualLength > i) //E ) { SnakevsBlock::ImplementSnakeBlockSidesCollision(i); }