Snake game snake library

Revision:
3:b24ef00836c5
Parent:
2:5e04e34a3b66
Child:
4:c74ec3f409f9
--- a/Snake.cpp	Fri May 05 11:55:34 2017 +0000
+++ b/Snake.cpp	Fri May 05 13:56:24 2017 +0000
@@ -6,7 +6,6 @@
 #define CEILING 8
 #define FLOOR 48
 
-//Snake snakePart;
 snakePart snek;
 
 
@@ -18,20 +17,25 @@
 Snake::~Snake()
 {
   
-    
-    //delete things create aboce in construcxtor
 }
 
-void Snake::init(int x, int y, int lenght){
+void Snake::init(int x, int y, int lenght, int _live){
     
     
     
     //initalizing the starting variables
-    snek._x[0]=x;
-    snek._y[0]=y;
-    live=1;
+    //snek._x[0]=x;
+    //snek._y[0]=y;
+    startx=x;
+    starty=y;
+    startl=lenght;
+    live=_live;
+    if (live==0){
+            live=3;
+    }
     _length = lenght;
-    counter=0;
+    
+    _food.init();
     for (int i=0;_length>i;i++)
     {   
         snek._x[i]=x+i;
@@ -51,238 +55,208 @@
     _direction = 1;//1 is East, 2 is South, 3 is West, 4 is North
     
     
-    //putting food at middle of the map before randomizing
+    
     printf("xog is %d  ", snek._x[_length-1]);
     //printf("initalized");
     }
 
 void Snake::draw(N5110 &lcd){
-    checkWallCollision(lcd);
-    if (live==1){
-    lcd.clear();
-    
-    lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);
-    for ( int i=0; _length>i;i++)
-    {
-        if (snek._x!=0)
-            {
-                if (snek._y!=0)
-                    {
-                        lcd.setPixel(snek._x[i],snek._y[i]);
-                    }
-            }
-    
-    //draw the snake array
-    //draw lines from breakpoint to breakpoint to head
-    //draw food
-    //lcd.setPixel(48,28);
-    
-    lcd.refresh();
-    //update(pad);
-    //printf("drAwn");
-    }
-   }//live loop 
+        checkWallCollision(lcd);
+            if (live!=0){
+                lcd.clear();
+                char buffer1[14];
+                char buffer2[14];
+                sprintf(buffer1,"%2d",live);
+                sprintf(buffer2,"%2d",_length);
+                lcd.printString(buffer1,25,0);
+                lcd.printString(buffer2,70,0);
+                _food.draw(lcd);
+                lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);
+                lcd.printString("Life:",0,0);
+                lcd.printString("Pts:",40,0);
+                
+                for ( int i=0; _length>i;i++){
+                    if (snek._x!=0)
+                        {
+                            if (snek._y!=0)
+                                {
+                                    lcd.setPixel(snek._x[i],snek._y[i]);
+                                }
+                        }
+                
+               
+                
+                lcd.refresh();
+                
+                //printf("drawn");
+                }
+       }//live loop 
 }
 void Snake::update(Gamepad &pad){
      
-     if (live==1){
+     if (live!=0){
     
-    d=pad.get_direction();
-        startx=snek._x[0];
-        
-        counter=_length-2;
-        
-           printf("x is %d  ", snek._x[_length-1]);
-            if (snek._dir[_length-1]==1)
+            d=pad.get_direction();
+            startx=snek._x[0];
+            
+            printf("x+1 is %d", snek._x[_length+1]);
+            printf("y+1 is %d", snek._y[_length+1]);
+            printf("dir+1 is %d", snek._dir[_length+1]);
+            printf("length is %d", _length);
+    
+             /*if ( pad.check_event(Gamepad::A_PRESSED) == true)//testing the add_point manually
             {
-                snek._x[_length]++;
-                startx=0;
-                //snek._dir[counter]=1;
-                //counter--;
-                //if (counter>0)
-                //{counter--;}
+                snek._x[_length+1]=snek._x[_length];//-1
+                snek._y[_length+1]=snek._y[_length];
+                snek._dir[_length+1]=snek._dir[_length-1];
+                _length=_length+1;
+                printf("length+1 is %d", _length);
                 
+            }
+            */
+    
+    
+       //printf("x is %d  ", snek._x[_length-1]);
+            if (snek._dir[_length-1]==1)
+                {
+                    snek._x[_length]++;                    
                 }
+            
             if (snek._dir[_length-1]==2)
-            {
-                snek._y[_length]--;
-                //snek._dir[counter]=2;
-                //counter--;
-                //if (i>0)
-                //{counter--;}
-                
-                }
-                if (snek._dir[_length-1]==3)
-            {
-                snek._x[_length]--;
-                //snek._dir[counter]=2;
-                //counter--;
-                //if (i>0)
-                //{counter--;}
+                {
+                    snek._y[_length]--;
                 
                 }
-                if (snek._dir[_length-1]==4)
-            {
-                snek._y[_length]++;
-                //snek._dir[counter]=2;
-                //counter--;
-                //if (i>0)
-                //{counter--;}
-                
+            
+            if (snek._dir[_length-1]==3)
+                {
+                    snek._x[_length]--;                    
                 }
                 
-                for (int i=0 ;_length<i ;i++)
-        {  
-            
-               //printf(" x = %d", snek._x[i]);
-                
-                snek._x[i]=snek._x[i+1];
-                snek._y[i]=snek._y[i+1];
-                //printf("done");
-            
-            
-            }
+            if (snek._dir[_length-1]==4)
+                {
+                    snek._y[_length]++;                                        
+                }
             
-            /*
-            //snek._x[i]=snek._x[i+1];
-           // snek._y[i]=snek._y[i+1];
-                switch (snek._dir[i])
-                        {
-                        case 1:
-                                snek._x[i]++;
-                                
-                        case 4: 
-                                snek._y[i]--;
-                        
-                        }
-        */
-                                                 
+            for (int i=0 ;_length<i ;i++)
+                {                      
+                    snek._x[i]=snek._x[i+1];
+                    snek._y[i]=snek._y[i+1];
+                    //printf("done");                    
+                }
+            
+                                  
                     //check dpad which way it is pointing
                     //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down
                     //set a breakpoint at head by snakeX/Y[i]
                     //switch case to make sure direction is good
                     //if head == food, _length++, and -direction we add 1 length
                     // if head==wall game over
-    
-    
-        
-        //if (counter>0)
-          //      {counter--;}
-        if (d==N)
-            {
+            if (d==N){
+                
+                        if (snek._dir[_length-1]!=4)
+                            {
+                                snek._dir[_length-1] = 2;                                
+                            }
+
+            }
+            
+            if (d==E){
                 
-                if (snek._dir[_length-1]!=4)
+                        if (snek._dir[_length-1]!=3)
                             {
-                                snek._dir[_length-1] = 2;
-                                
-                                //snek._y[5]--;
-                                //return;
-                                
+                                snek._dir[_length-1] = 1;                                
+                            }
+                
+                }
+            if (d==W){
+                
+                        if (snek._dir[_length-1]!=1)
+                            {
+                                snek._dir[_length-1] = 3;
                             }
                 
                 
                 }
-                if (d==E)
-            {
-                
-                if (snek._dir[_length-1]!=3)
-                            {
-                                snek._dir[_length-1] = 1;
-                                
-                                //snek._y[5]--;
-                                //return;
-                                
-                            }
-                
+            if (d==S){
                 
-                }
-                if (d==W)
-            {
-                
-                if (snek._dir[_length-1]!=1)
-                            {
-                                snek._dir[_length-1] = 3;
-                                
-                                //snek._y[5]--;
-                                //return;
-                                
-                            }
-                
-                
-                }
-                if (d==S)
-            {
-                
-                if (snek._dir[_length-1]!=2)
+                        if (snek._dir[_length-1]!=2)
                             {
                                 snek._dir[_length-1] = 4;
-                                
-                                //snek._y[5]--;
-                                //return;
-                                
                             }
                 
                 
                 }
-        /*switch (d)
-            {
-                case N:
-                        
-                
-                /*case E:
-                        if (snek._dir[_length]!=3)
-                            {
-                                snek._dir[_length-1] = 1;
-                                
-                                //snek._y[5]--;
-                                //return;
-                                
-                            }*/
-                
-                        
-                            
-           // }
-        //counter=0;
-    printf("updated ");
-}//live loop
-    }
+        
+            printf("updated ");
+            add_point();
+        }//live loop
+}
+
+void Snake::add_point(){
+    
+    posXY foodPos = _food.returnPos();
+    if(snek._x[_length-1]==foodPos.x && snek._y[_length-1]== foodPos.y)
+        {
+            snek._x[_length+1]=snek._x[_length];//-1
+            snek._y[_length+1]=snek._y[_length];
+            snek._dir[_length+1]=snek._dir[_length-1];
+            _length=_length+1;
+            _food.respawn();
+        }
+}//end of add_point
 
-snakePart Snake::add_point(int x, int y){
-    
-   
-    
-    
-    //snek._x=x;
-    //snek._y=y;
-    
-    
-    return;
-    
-   //_length++;
-    
-}
 void Snake::deadSnake(N5110 &lcd){
-     lcd.clear();
+        
+        /*if (live!=0){
+            lcd.clear();
+            lcd.printString("Game Over",0,1);
+            lcd.printString("Press Start",0,2);
+            lcd.printString("To Continue",0,3);
+            lcd.refresh(); 
+            live--;
+            while ( pad.check_event(Gamepad::START_PRESSED) == false){
+                    pad.leds_on();
+                    wait(0.05);
+                    pad.leds_off();
+                    wait(0.05);
+            }
+            init(startx,starty,startl,live);
+        }*/
+    //live--;
+    init(startx,starty,startl,live);
+        if (live==0){
+            lcd.clear();
+            lcd.printString("Game Over",0,1);
+            lcd.printString("Press Start",0,2);
+            lcd.printString("To restart",0,3);
+            lcd.refresh(); 
+          /*  live--;
+            while ( pad.check_event(Gamepad::START_PRESSED) == false){
+                    pad.leds_on();
+                    wait(0.2);
+                    pad.leds_off();
+                    wait(0.2);
+            }*/
+           // init(startx,starty,startl,live);
+        }
+                lcd.clear();
         lcd.printString("Game Over",0,1);
         lcd.refresh();    
-live=0; 
+        live--; 
         
-            }
+        }//end of deadSnake
+        
 void Snake::checkWallCollision(N5110 &lcd){
     
-    if (snek._x[_length]==WIDTH||snek._x[_length]==0)
-    {
-     
-        deadSnake(lcd);
-    }
-    if (snek._y[_length]==CEILING||snek._y[_length]==FLOOR)
-    {
-     
-        deadSnake(lcd);
-    }
+    if (snek._x[_length]==WIDTH||snek._x[_length]==0)//if snake head hits side walls
+        {     
+            deadSnake(lcd);
+        }
+    if (snek._y[_length]==CEILING||snek._y[_length]==FLOOR)//if snake hits top or bottom walls
+        {     
+            deadSnake(lcd);
+        }
     
-    
-    //return _length;
-    
-}
+}//end of checkWallCollision
     
\ No newline at end of file