Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
43:233f93860d08
Parent:
42:973bb6036f81
Child:
44:cd10d07ea1e5
--- a/SnakevsBlock/SnakevsBlock.cpp	Tue Apr 23 15:22:09 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Thu Apr 25 18:12:17 2019 +0000
@@ -101,6 +101,12 @@
         level += 1;
         _statset.write(level, sd);
         SnakevsBlock::reset();
+        food_pos[0].x = 0;
+        food_pos[0].y = 0;
+        food_pos[1].x = 0;
+        food_pos[1].y = 0;
+        food_pos[2].x = 0;
+        food_pos[2].y = 0;
         lcd.clear();
         lcd.printString("Level Complete",0,1);
         lcd.printString("Press Start",10,3);
@@ -127,8 +133,8 @@
 {
     send_block_number = 0;  //this is for the game to decide wether to remember the number on the block for the current itteration.
                             //we dont need to remember if it has already gone past the screen.
+    CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
     CheckSnakeBlockCollision(pad); //Function checks for when the snake collides with any of the blocks.
-    CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
     CheckSnakeBlockSidesCollision(pad); //Function checks for when the snake collides with any of the blocks' sides.
     _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();
@@ -156,8 +162,8 @@
     // 81.000000 0.000000 top right
     // 0.000000 0.000000 is top left
     // 81.000000 45.000000 bottom right
-    snakex = _s.get_pos().x;
-    snakey = _s.get_pos().y;
+    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. 
+    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.
     //printf("snakexy in GAME = %d %d \n", snakex, snakey);
     //Obtains all required coordinates.
     food_pos[0] = _f.get_pos();
@@ -216,7 +222,8 @@
     //of the blocks which are a maximum of 5, if so then the snake length reduces and the block number reduces
     //the block has to move slower and come down after every 2/3 iterations(dependent on the snake size.(think about this)
     for(int block=0; block<=83; block+=1)  {    //this loop automatically detects for each section of block and each combination of collision
-        if ((snakey == b_pos.y + 10) && (snakex + 1 == b_pos.x + block))  {
+        if (((snakey == b_pos.y + 11)||(snakey == b_pos.y + 10)||(snakey == b_pos.y + 9)) && (snakex + 1 == b_pos.x + block))  {
+            //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.
             //printf("snake collision working \n");
             //audio feedback
             if(blocknum > 0)  {b_pos.y = 0;}  //change this to speed y = 0 when length = 10.