Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
10:751bd953fa27
Child:
11:d6ceff1ff6d7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Blocks/Blocks.cpp	Sat Mar 30 17:09:18 2019 +0000
@@ -0,0 +1,434 @@
+#include "Blocks.h"
+
+Blocks::Blocks()
+{
+
+}
+
+Blocks::~Blocks()
+{
+
+}
+
+//Frame Sprite.
+int Frame[11][1] = {
+    {0},
+    {0},
+    {1},
+    {1},
+    {1},
+    {1},
+    {1},
+    {1},
+    {1},
+    {0},
+    {0},
+    };
+int One[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Two[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
+    {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Three[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Four[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Five[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
+    {0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Six[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
+    {0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Seven[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Eight[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Nine[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Ten[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
+    {0,0,0,1,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,1,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,1,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,1,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,1,1,1,0,0,1,1,1,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Eleven[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,0,1,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,1,1,0,0,0,1,1,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,0,1,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,1,1,1,0,0,1,1,1,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Twelve[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
+    {0,0,0,1,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,1,1,1,0,1,1,1,1,1,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Thirteen[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,1,1,1,1,1,0,0,0},
+    {0,0,0,1,1,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,1,1,1,0,0,1,1,1,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Fourteen[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
+    {0,0,0,0,1,0,0,0,1,0,1,0,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,1,0,0,0,0},
+    {0,0,0,0,1,0,0,1,1,1,1,1,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,1,1,1,0,0,0,0,1,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Fifteen[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,1,1,1,1,1,0,0,0},
+    {0,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
+    {0,0,0,0,1,0,0,1,1,1,1,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,1,1,1,0,0,1,1,1,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Sixteen[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,0,0,0,1,1,0,0,0},
+    {0,0,0,1,1,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,1,0,0,1,1,1,1,0,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,1,1,1,0,0,1,1,1,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Seventeen[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,1,1,1,1,1,0,0,0},
+    {0,0,0,1,1,0,0,0,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
+    {0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
+    {0,0,0,1,1,1,0,0,1,0,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Eighteen[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
+    {0,0,0,1,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,1,1,1,0,0,1,1,1,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+int Nineteen[11][15] = {
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
+    {0,0,0,1,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,1,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,0,1,1,1,1,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,0,1,0,0,0},
+    {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0},
+    {0,0,0,1,1,1,0,0,1,1,0,0,0,0,0},
+    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
+    {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
+    };
+
+
+int k = 0;
+void Blocks::init()
+{
+    blockdrop=0;
+}
+
+
+void Blocks::draw(N5110 &lcd, int length)
+{   
+
+    _velocity.x = 0;
+    _velocity.y = 1;
+    
+    
+    //Resets SnakeFood position if it reaches the bottom of the screen.
+    //Also prevents SnakeFood from going out of the side of the screen.
+    if(_bx < 0){
+        _bx = 0;
+        }
+    if(_bx > 84){
+        _bx = 84;
+        }
+    if(_by >= 48){
+        k = 0;
+        }
+     if(k == 0){
+        _bx = 0;  //change the number here so that there is always an unsurity(a little) if the food will come or not(change the limits for this)
+        _by = -11;
+        k = k+1;
+        pos = 4;
+        times = 1;
+        i=0;// to save the positions of the random numbers generated in a block
+        while(pos<=68)  {
+            if(times == rand()%6)  {
+                double round = length/2;
+                caseselect[i] = floor(round);
+            }
+            else    {
+                caseselect[i] = rand()%19;
+            }
+            pos+=16;
+            times+=1;
+            if (times == 5)  {
+                times = 0;
+            }
+            i++;
+        }
+        double roundagain = length/2;  //to make sure at least one is smaller than the snake size
+        caseselect[rand()%5] = floor(roundagain);
+          
+     }
+    lcd.drawSprite(_bx+3,_by,11,1,(int *)Frame); //Function to draw the sprite.
+    lcd.drawSprite(_bx+19,_by,11,1,(int *)Frame);
+    lcd.drawSprite(_bx+35,_by,11,1,(int *)Frame);
+    lcd.drawSprite(_bx+51,_by,11,1,(int *)Frame);
+    lcd.drawSprite(_bx+67,_by,11,1,(int *)Frame);
+    lcd.drawSprite(_bx+83,_by,11,1,(int *)Frame);
+    i = 0;
+    pos = 4;
+    while((i<5)&&(pos<=68))  {
+        state = caseselect[i];
+        switch(state)  {
+            case 0:
+                lcd.drawSprite(pos,_by,11,15,(int *)One);
+                break;
+            case 1:
+                lcd.drawSprite(pos,_by,11,15,(int *)Two);
+                break;
+            case 2:
+                lcd.drawSprite(pos,_by,11,15,(int *)Three);
+                break;
+            case 3:
+                lcd.drawSprite(pos,_by,11,15,(int *)Four);
+                break;
+            case 4:
+                lcd.drawSprite(pos,_by,11,15,(int *)Five);
+                break;
+            case 5:
+                lcd.drawSprite(pos,_by,11,15,(int *)Six);
+                break;
+            case 6:
+                lcd.drawSprite(pos,_by,11,15,(int *)Seven);
+                break;
+            case 7:
+                lcd.drawSprite(pos,_by,11,15,(int *)Eight);
+                break;
+            case 8:
+                lcd.drawSprite(pos,_by,11,15,(int *)Nine);
+                break;
+            case 9:
+                lcd.drawSprite(pos,_by,11,15,(int *)Ten);
+                break;
+            case 10:
+                lcd.drawSprite(pos,_by,11,15,(int *)Eleven);
+                break;
+            case 11:
+                lcd.drawSprite(pos,_by,11,15,(int *)Twelve);
+                break;
+            case 12:
+                lcd.drawSprite(pos,_by,11,15,(int *)Thirteen);
+                break;
+            case 13:
+                lcd.drawSprite(pos,_by,11,15,(int *)Fourteen);
+                break;
+            case 14:
+                lcd.drawSprite(pos,_by,11,15,(int *)Fifteen);
+                break;
+            case 15:
+                lcd.drawSprite(pos,_by,11,15,(int *)Sixteen);
+                break;
+            case 16:
+                lcd.drawSprite(pos,_by,11,15,(int *)Seventeen);
+                break;
+            case 17:
+                lcd.drawSprite(pos,_by,11,15,(int *)Eighteen);
+                break;
+            case 18:
+                lcd.drawSprite(pos,_by,11,15,(int *)Nineteen);
+                break;
+            default:
+                error("Invalid State");
+                break;
+            }
+            i++;
+            pos+=16;
+        }
+}
+
+
+
+Vector2D Blocks::get_pos() //Obtains the X and Y coordinate of the target.
+{
+    Vector2D blockpos = {_bx,_by};
+    //printf("blockpos is = %f %f \n", blockpos.x, blockpos.y);
+    return blockpos;
+}
+
+
+
+
+void Blocks::update()
+{
+    if(blockdrop == 0)  {
+        _bx += _velocity.x;
+        _by += _velocity.y;
+    }
+    blockdrop += 1;
+    
+    if(blockdrop == 2)  {  //make this a variable multiple of game speed
+        blockdrop = 0;
+    }
+}
+
+
+void Blocks::set_pos(Vector2D p)
+{
+    _bx = p.x;
+    _by = p.y;
+}