Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
65:2872ca289b49
Parent:
64:540aa1602372
Child:
67:39b9ba6019b0
--- a/GameEngine/SnakevsBlock/SnakevsBlock.cpp	Thu May 02 11:30:04 2019 +0000
+++ b/GameEngine/SnakevsBlock/SnakevsBlock.cpp	Fri May 03 19:16:02 2019 +0000
@@ -33,15 +33,6 @@
         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++) {
-        food_pos[i].x = 0;
-        food_pos[i].y = 0;
-    }
-    b_pos.x = 0;
-    b_pos.y = 0;
-    */
 }
 
 void SnakevsBlock::object_initialisations()
@@ -204,32 +195,53 @@
     //If statements check if the snake sprite has collided with any
     //of the three food sprites, if so then the food location is reset and
     //length of the snake is increased using the length variable.
-    for(int y=0; y<=2; y++)  {    //this loop automatically detects each combination of collision in the y postion
-        for(int x=0; x<=2; x++)  {    //this loop automatically detects each combination of collision in the x postion
-            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)) &&
-                    ((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::CheckSnakeFoodYCollision(pad);
+
     _f.set_pos(food_pos[0]);
     _ff.set_pos(food_pos[1]);
     _fff.set_pos(food_pos[2]);
 }
 
+void SnakevsBlock::CheckSnakeFoodYCollision(Gamepad &pad)
+{
+    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);
+    }
+
+}
+
+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);
+    }
+
+}
+
+void SnakevsBlock::ImplementSnakeFoodCollision(Gamepad &pad, int x, int y)
+{
+    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)) &&
+            ((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();
+        }
+    }
+
+}
+
 void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad)
 {
     //Obtains the numbers inside the block.
@@ -248,19 +260,19 @@
             //audio feedback
             srn = CheckBlock(block);  //this tells us which of the 5 blocks we are colliding with
             blocknum = b_number[srn]; //this saves the number inside the colliding block into blocknum.
-            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.
-                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
-            } else {          //is 10.
-                velocity = 1;
-            }
-            ImplementCollision(pad); //this implements the collision once the conditions are met.
-            SnakevsBlock::_set_velocity(); //sets the block and foods free or frezes them depending on snake level.
+            ImplementSnakeBlockCollision(pad); //this implements the collision once the conditions are met.
+            SnakevsBlock::_setVelocity(srn); //sets the block and foods free or frezes them depending on snake length.
         }
     }
 }
 
-void SnakevsBlock::_set_velocity()
+void SnakevsBlock::_setVelocity(int srn)
 {
+    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.
+        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
+    } else {          //is 10.
+        velocity = 1;
+    }
     _b.velocity.y = velocity;
     _f.velocity.y = velocity;
     _ff.velocity.y = velocity;
@@ -288,7 +300,7 @@
     return srn;
 }
 
-void SnakevsBlock::ImplementCollision(Gamepad &pad)
+void SnakevsBlock::ImplementSnakeBlockCollision(Gamepad &pad)
 {
     send_block_number = 1;
     if(blocknum > 0)  {  // to make sure that snake doesn't decrease in _length if number on the block is less than 1;
@@ -307,7 +319,7 @@
     SnakevsBlock::MakeDefaultMotionFree(); //this makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision.
 
     for(int i=0; i<=9; i++)  { //i checks for all possible collisions with the snake respective to it's length.
-        SnakevsBlock::check_Collision_Y_axis(i); //checks if the snake and the block are at the same position in y axis.
+        SnakevsBlock::CheckSnakeBlockSidesYCollision(i); //checks if the snake and the block are at the same position in y axis.
     }
 
 }
@@ -319,7 +331,7 @@
     }
 }
 
-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.
+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.
@@ -328,21 +340,21 @@
             (snake_pos[i].y + 1 == b_pos.y + b_y_combination) ||
             (snake_pos[i].y + 2 == b_pos.y + b_y_combination))  {
 
-            SnakevsBlock::check_Collision_X_axis(i); //checks if the snake and the block are at the same position in x axis.
+            SnakevsBlock::CheckSnakeBlockSidesXCollision(i); //checks if the snake and the block are at the same position in x axis.
 
         }
     }
 }
 
-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.
+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.
 
-        SnakevsBlock::checkCollision_EastorWest(b_x_combination, i); //checks if the colliding wall is on east side or west side.
+        SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(b_x_combination, i); //checks if the colliding wall is on east side or west side.
     }
 }
 
-void SnakevsBlock::checkCollision_EastorWest(int b_x_combination, int i) //i checks for all possible collisions with the snake respective to it's length.
+void SnakevsBlock::CheckSnakeBlockSidesEastWestCollision(int b_x_combination, int i) //i checks for all possible collisions with the snake respective to it's length.
 {
     SnakevsBlock::makeVirtualLengthMaxTen(); //stops the length at 10 for collision purposes.
 
@@ -351,14 +363,14 @@
         ((snake_pos[i].x == b_pos.x + b_x_combination+2) ||  //W
          (snake_pos[i].x + 1 == b_x_combination+2))&&(_d != E)&&(_virtualLength > i) //W
     ) {
-        SnakevsBlock::StopX_AxisMotion(i);
+        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
     ) {
-        SnakevsBlock::StopX_AxisMotion(i);
+        SnakevsBlock::ImplementSnakeBlockSidesCollision(i);
     }
 }
 
@@ -372,7 +384,7 @@
     }
 }
 
-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.
+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.
 {
     //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++)  {