Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
39:210ac915e0a0
Parent:
38:30e4e6191762
Child:
40:1debed7ec01c
--- a/SnakevsBlock/SnakevsBlock.cpp	Mon Apr 22 04:24:38 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Mon Apr 22 05:28:37 2019 +0000
@@ -22,6 +22,7 @@
     speed = 1;
     blockgap = 400;
     blockbuff = -50;
+    velocity = 1;
     for(int i=0; i<=14; i++)  {
         b[i] = 1;
     }
@@ -34,7 +35,7 @@
 
 
 
-void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int gm)
+void SnakevsBlock::read_input(Gamepad &pad, FXOS8700CQ &device, int g_mode)
 {
     device.get_values();
     angle = -device.get_roll_angle();
@@ -44,11 +45,11 @@
     }
     device.get_values();
     angle = -device.get_roll_angle() - garbage;
-    if(gm == 1)  {
+    if(g_mode == 1)  { //this condition returns the relevant working directions if we select joystick in StartScreen.
         _d = pad.get_direction(); //Obtains Direction pushed towards on Joystick.
         _mag = pad.get_mag(); //Obtains Magnitude of Joystick.
     }
-    else if(gm == 2)  {
+    else if(g_mode == 2)  { //this condition returns the relevant working directions if we select motion control in StartScreen.
         if (angle >= 8) {
             _d = E;
         }
@@ -69,14 +70,14 @@
     length = _s.draw(pad, 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, blockbuff); //Draws the first food.
-        if(foodbuff >=50)  {
-            _ff.draw(lcd, blockbuff); //Draws the second food.
-            if(foodbuff >=80) {
-                _fff.draw(lcd, blockbuff); //Draws the third food.
-            }
-        }
-        foodbuff +=1;
+    }
+    if(foodbuff >=50)  {
+        _ff.draw(lcd, blockbuff); //Draws the second food.
     }
+    if(foodbuff >=80) {
+        _fff.draw(lcd, blockbuff); //Draws the third food.
+    }
+    foodbuff +=1;
     if(foodbuff >=8) {
         send = _b.draw(lcd, length, blockgap);
     }
@@ -84,11 +85,15 @@
         blockbuff = -10;
     }
     //Code to print length on game screen.
+    SnakevsBlock::print_length_on_screen(lcd);  
+}
+
+void SnakevsBlock::print_length_on_screen(N5110 &lcd)
+{
     char bufferscore[14];
     sprintf(bufferscore,"%d",length);
-    lcd.printString(bufferscore,1,0);    
-}
-
+    lcd.printString(bufferscore,1,0);  
+}   
 
 int SnakevsBlock::update(Gamepad &pad, SDFileSystem &sd) //Updates objects on screen.
 {
@@ -171,39 +176,39 @@
     _fff.set_pos(food_pos[2]);
 }
     
-    void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad)
-    {
-        //Obtains all required coordinates.
-        Vector2D b_pos = _b.get_pos();
-        int *b_number;
-        b_number = _b.get_number();
-        Vector2D snake_pos = _s.get_pos(length);
-        //If statements check if the snake sprite has collided with any
-        //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 ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + block))  {
-                //printf("snake collision working \n");
-                //audio feedback
-                if(blocknum > 0)  {b_pos.y = 0;}  //change this to speed y = 0 when length = 10.
-                srn = CheckBlock(block);  //this tells us which of the 5 blocks we are colliding with
-                blocknum = b_number[srn];
-                ImplementCollision(pad);
-                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.
-                    _b.velocity.y = 0;
-                    _f.velocity.y = 0;
-                    _ff.velocity.y = 0;
-                    _fff.velocity.y = 0;
-                    break;//change this so that it collides slower.
-                }
-                else {
-                    _b.velocity.y = 1;
-                    _f.velocity.y = 1;
-                    _ff.velocity.y = 1;
-                    _fff.velocity.y = 1;
-                }
+void SnakevsBlock::CheckSnakeBlockCollision(Gamepad &pad)  {
+    //Obtains all required coordinates.
+    Vector2D b_pos = _b.get_pos();
+    int *b_number;
+    b_number = _b.get_number();
+    Vector2D snake_pos = _s.get_pos(length);
+    //If statements check if the snake sprite has collided with any
+    //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 ((snake_pos.y == b_pos.y + 10) && (snake_pos.x + 1 == b_pos.x + block))  {
+            //printf("snake collision working \n");
+            //audio feedback
+            if(blocknum > 0)  {b_pos.y = 0;}  //change this to speed y = 0 when length = 10.
+            srn = CheckBlock(block);  //this tells us which of the 5 blocks we are colliding with
+            blocknum = b_number[srn];
+            ImplementCollision(pad);
+            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;
             }
+            else {
+                velocity = 1;
+            }
+            SnakevsBlock::_set_velocity();
         }
+    }
+}
+ 
+void SnakevsBlock::_set_velocity()  {
+    _b.velocity.y = velocity;
+    _f.velocity.y = velocity;
+    _ff.velocity.y = velocity;
+    _fff.velocity.y = velocity;
 }
  
 int SnakevsBlock::CheckBlock(int block)  {
@@ -240,13 +245,6 @@
         snake_pos[7] = _s.get_pos_before7(length);
         snake_pos[8] = _s.get_pos_before8(length);
         snake_pos[9] = _s.get_pos_before9(length);
-        /*
-        snake_pos[10] = _s.get_pos_before10(length);
-        snake_pos[11] = _s.get_pos_before11(length);
-        snake_pos[12] = _s.get_pos_before12(length);
-        snake_pos[13] = _s.get_pos_before13(length);
-        snake_pos[14] = _s.get_pos_before14(length);
-        */
         //If statements check if the snake sprite has collided with any
         //of the blocks' sides and then stop the snake moving in x axis