Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
9:d1d79d4ee673
Parent:
8:890b986b16a4
Child:
10:751bd953fa27
--- a/SnakevsBlock/SnakevsBlock.cpp	Tue Mar 26 18:36:50 2019 +0000
+++ b/SnakevsBlock/SnakevsBlock.cpp	Wed Mar 27 18:20:36 2019 +0000
@@ -13,8 +13,8 @@
 void SnakevsBlock::init()
 {
     //The snake length configuration and all the other initial information passing will be done here
-    length=15;
-    
+    length=3;
+    foodbuff=0; 
 }
 
 
@@ -27,7 +27,20 @@
 
 void SnakevsBlock::draw(N5110 &lcd, Gamepad &pad)
 {
-    _s.draw(lcd, length); //Draws the Snake.
+    _s.draw(lcd, length); //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); //Draws the first food.
+        if(foodbuff >=50)  {
+            _ff.draw(lcd); //Draws the second food.
+            if(foodbuff >=80) {
+                _fff.draw(lcd); //Draws the third food.
+            }
+        }
+        foodbuff +=1;
+        if(foodbuff == 90)  {
+            foodbuff = 90;
+        }
+    }
     
     //Code to print length on game screen.
     char bufferscore[14];
@@ -46,12 +59,16 @@
 
 void SnakevsBlock::update(Gamepad &pad) //Updates objects on screen.
 {
-    _s.update(_d,_mag,length); 
+    _s.update(_d,_mag, length);
+    _f.update();
+    _ff.update();
+    _fff.update(); 
+    CheckSnakeFoodCollision(pad); //Function checks for when the snake collides with it's food.
 }
 
 void SnakevsBlock::get_pos()
 {
-    Vector2D snake_pos = _s.get_pos();
+    Vector2D snake_pos = _s.get_pos(length);
     //printf("player pos = %f %f \n", player_pos.x, player_pos.y); //top left of player sprite
     // 81.000000 0.000000 top right
     // 0.000000 0.000000 is top left
@@ -61,4 +78,117 @@
     //printf("snakexy in GAME = %d %d \n", snakex, snakey);
 }   
     
-    
\ No newline at end of file
+
+void SnakevsBlock::CheckSnakeFoodCollision(Gamepad &pad)
+    {
+        //Obtains all required coordinates.
+        
+        
+        Vector2D f_pos = _f.get_pos();
+        Vector2D ff_pos = _ff.get_pos();
+        Vector2D fff_pos = _fff.get_pos();
+        Vector2D snake_pos = _s.get_pos(length);
+        
+        //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.
+         
+    if (
+        ((snake_pos.y == f_pos.y) ||
+        (snake_pos.y == f_pos.y + 1) ||
+        (snake_pos.y == f_pos.y + 2) ||
+        (snake_pos.y + 1== f_pos.y) ||
+        (snake_pos.y  + 1== f_pos.y + 1) ||
+        (snake_pos.y  + 1== f_pos.y + 2) ||
+        (snake_pos.y + 2 == f_pos.y) ||
+        (snake_pos.y + 2 == f_pos.y + 1) ||
+        (snake_pos.y + 2 == f_pos.y + 2)) &&
+        ((snake_pos.x == f_pos.x) ||
+        (snake_pos.x == f_pos.x + 1) ||
+        (snake_pos.x == f_pos.x + 2) ||
+        (snake_pos.x + 1 == f_pos.x) ||
+        (snake_pos.x + 1== f_pos.x + 1) ||
+        (snake_pos.x + 1 == f_pos.x + 2) ||
+        (snake_pos.x + 2 == f_pos.x) ||
+        (snake_pos.x + 2 == f_pos.x + 1) ||
+        (snake_pos.x + 2 == f_pos.x + 2))
+    ) {
+        
+        //printf("snake feast working \n");
+        //audio feedback
+        pad.tone(1000.0,0.1);
+        f_pos.x = rand() % 82;
+        f_pos.y = 0;
+        
+        length+=1;
+        
+    }
+    
+    if (
+        ((snake_pos.y == ff_pos.y) ||
+        (snake_pos.y == ff_pos.y + 1) ||
+        (snake_pos.y == ff_pos.y + 2) ||
+        (snake_pos.y + 1== ff_pos.y) ||
+        (snake_pos.y  + 1== ff_pos.y + 1) ||
+        (snake_pos.y  + 1== ff_pos.y + 2) ||
+        (snake_pos.y + 2 == ff_pos.y) ||
+        (snake_pos.y + 2 == ff_pos.y + 1) ||
+        (snake_pos.y + 2 == ff_pos.y + 2)) &&
+        ((snake_pos.x == ff_pos.x) ||
+        (snake_pos.x == ff_pos.x + 1) ||
+        (snake_pos.x == ff_pos.x + 2) ||
+        (snake_pos.x + 1 == ff_pos.x) ||
+        (snake_pos.x + 1== ff_pos.x + 1) ||
+        (snake_pos.x + 1 == ff_pos.x + 2) ||
+        (snake_pos.x + 2 == ff_pos.x) ||
+        (snake_pos.x + 2 == ff_pos.x + 1) ||
+        (snake_pos.x + 2 == ff_pos.x + 2))
+    ) {
+        
+        //printf("snake feast working \n");
+        // audio feedback
+        pad.tone(1000.0,0.1);
+        ff_pos.x = rand() % 82;
+        ff_pos.y = 0;
+        
+        length+=1;
+        
+    }
+    
+    if (
+        ((snake_pos.y == fff_pos.y) ||
+        (snake_pos.y == fff_pos.y + 1) ||
+        (snake_pos.y == fff_pos.y + 2) ||
+        (snake_pos.y + 1== fff_pos.y) ||
+        (snake_pos.y  + 1== fff_pos.y + 1) ||
+        (snake_pos.y  + 1== fff_pos.y + 2) ||
+        (snake_pos.y + 2 == fff_pos.y) ||
+        (snake_pos.y + 2 == fff_pos.y + 1) ||
+        (snake_pos.y + 2 == fff_pos.y + 2)) &&
+        ((snake_pos.x == fff_pos.x) ||
+        (snake_pos.x == fff_pos.x + 1) ||
+        (snake_pos.x == fff_pos.x + 2) ||
+        (snake_pos.x + 1 == fff_pos.x) ||
+        (snake_pos.x + 1== fff_pos.x + 1) ||
+        (snake_pos.x + 1 == fff_pos.x + 2) ||
+        (snake_pos.x + 2 == fff_pos.x) ||
+        (snake_pos.x + 2 == fff_pos.x + 1) ||
+        (snake_pos.x + 2 == fff_pos.x + 2))
+    ) {
+        
+        //printf("snake feast working \n");
+        // audio feedback
+        pad.tone(1000.0,0.1);
+        fff_pos.x = rand() % 82;
+        fff_pos.y = 0;
+        
+        length+=1;
+        
+    }
+
+
+    _f.set_pos(f_pos);
+    _ff.set_pos(ff_pos);
+    _fff.set_pos(fff_pos);
+    
+    }    
\ No newline at end of file