Balint Bogdan 2645 project, 200966741

Dependents:   2645Game_el15bb

Files at this revision

API Documentation at this revision

Comitter:
Nefos
Date:
Fri May 05 15:20:17 2017 +0000
Parent:
6:cc8d2088f490
Commit message:
Final code for releasing

Changed in this revision

Snake.cpp Show annotated file Show diff for this revision Revisions of this file
Snake.h Show annotated file Show diff for this revision Revisions of this file
--- 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
--- a/Snake.h	Fri May 05 15:06:54 2017 +0000
+++ b/Snake.h	Fri May 05 15:20:17 2017 +0000
@@ -31,6 +31,7 @@
         void addPoint();
         void checkWallCollision(N5110 &lcd);
         void checkTailCollision(N5110 &lcd);
+        void drawScore(N5110 &lcd);
         int _length;
         int live;
         float waitTime;