Snake game snake library

Revision:
7:a2f426a37e60
Parent:
6:cc8d2088f490
Child:
8:721a8dca7a25
--- a/Snake.cpp	Fri May 05 15:06:54 2017 +0000
+++ b/Snake.cpp	Fri May 05 15:20:17 2017 +0000
@@ -21,11 +21,8 @@
 
 /************************Functions************************/
 
-void Snake::init(int x, int y, int lenght, int _live){
-    
-    
-    
-    //initalizing the starting variables    
+void Snake::init(int x, int y, int lenght, int _live){//initalizing the starting variables     
+       
     startx=x;//saving these variables for later init
     starty=y;
     startl=lenght;
@@ -39,12 +36,9 @@
         snek._dir[i]=1;
     }
     snek._x[_length]=x+_length;
+    snek._x[_length+1]=x+_length+1;
     snek._y[_length]=y;
-    
-    _direction = 1;//1 is East, 2 is South, 3 is West, 4 is North
-    
-    
-    
+    snek._dir[_length]=1;
     printf("xog is %d  ", snek._x[_length-1]);
     //printf("initalized");
 }//end of init
@@ -56,20 +50,11 @@
                 lcd.clear();
                 waitCount=((float)_length/3)+5;//set wait so the game is speeding up
                 waitTime=(1/waitCount);
-                wait(waitTime);
-                char buffer1[14];
-                char buffer2[14];
-                sprintf(buffer1,"%2d",live);
-                sprintf(buffer2,"%2d",_length);
-                lcd.printString(buffer1,25,0);
-                lcd.printString(buffer2,70,0);
-                lcd.printString("Life:",0,0);
-                lcd.printString("Pts:",40,0);//display life and points
+                wait(waitTime);                
                 _food.draw(lcd);//make first food
-                lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);//
-                
-                
-                for ( int i=0; _length>i;i++){
+                lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);//draw arena
+                drawScore(lcd);
+                for ( int i=0; _length>i;i++){//draw snake
                     if (snek._x!=0)
                         {
                             if (snek._y!=0)
@@ -106,7 +91,7 @@
             }
             */
             //printf("x is %d  ", snek._x[_length-1]);
-            if (snek._dir[_length-1]==1)
+            if (snek._dir[_length-1]==1)//set direction according _dir
                 {
                     snek._x[_length]++;                    
                 }
@@ -194,11 +179,11 @@
 void Snake::deadSnake(N5110 &lcd){
         
        
-        live--; 
-        while (live==0){
+        live--; //take a life away
+        while (live==0){//dead reset game
             lcd.clear();
             lcd.printString("Game Over",0,1);
-            lcd.printString("Press Start",0,2);
+            lcd.printString("Press Reset",0,2);
             lcd.printString("To restart",0,3);
             lcd.refresh(); 
           
@@ -224,7 +209,7 @@
     
 }//end of checkWallCollision
 
-void Snake::checkTailCollision(N5110 &lcd){
+void Snake::checkTailCollision(N5110 &lcd){//if snake eats itself
     for (int i=0 ;_length<i ;i++){
                                           
                     if (snek._x[_length-1]==snek._x[i] && snek._y[_length-1]==snek._y[i])
@@ -234,4 +219,16 @@
                 }
     
     
-}//end of checkTailCollision
\ No newline at end of file
+}//end of checkTailCollision
+
+void Snake::drawScore(N5110 &lcd){
+        char buffer1[14];
+        char buffer2[14];
+        sprintf(buffer1,"%2d",live);
+        sprintf(buffer2,"%2d",_length-5);
+        lcd.printString(buffer1,25,0);
+        lcd.printString(buffer2,70,0);
+        lcd.printString("Life:",0,0);
+        lcd.printString("Pts:",40,0);//display life and points
+    
+}//end of drawScore
\ No newline at end of file