Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
41:4edac50f010d
Parent:
39:210ac915e0a0
Child:
43:233f93860d08
diff -r 1debed7ec01c -r 4edac50f010d Snake/Snake.cpp
--- a/Snake/Snake.cpp	Mon Apr 22 07:10:52 2019 +0000
+++ b/Snake/Snake.cpp	Tue Apr 23 12:25:49 2019 +0000
@@ -19,15 +19,14 @@
 
 void Snake::init()
 {
-
     _speed = 1;// change this according to the options selected
     m = 0; //Variable used to allow a starting location for the player.
-    
 }
 
 
-int Snake::draw(Gamepad &pad, N5110 &lcd, int length, int level)
+void Snake::draw(Gamepad &pad, N5110 &lcd, int length, int level)
 {
+    _length = length;
     if(m == 0){
         _x[0] = WIDTH/2;  //Spawns player sprite near the middle of the screen.
         _y[0] = HEIGHT - 3;
@@ -41,7 +40,7 @@
         m = m+1;
     }
     //printf("SPRITE %d %d \n", _x[0], _y[0]); 
-    if(length == 0)  {
+    if(_length == 0)  {
         pad.init();
         while ((pad.check_event(Gamepad::BACK_PRESSED) == false)) {
             lcd.clear();
@@ -55,54 +54,31 @@
     }
     
     for(int a=1; a<=10; a++)  {
-        if(length == a)  {
+        if(_length == a)  {
+            for(int i=0; i<=a-1; i++)  {
+                lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite.
+            }
+        }
+    }
+    if(_length >= 10) {
+        for(int a=1; a<=10; a++)  {
             for(int i=0; i<=a-1; i++)  {
                 lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite.
             }
         }
     }
-    if(length>=10) {
-        for(int a=1; a<=10; a++)  {
-            for(int i=0; i<=a-1; i++)  {
-                lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite.
-            }
-    }
-    }
-    
-    if(length >= 20)  {
-        lcd.clear();
-        lcd.printString("Level Complete",0,1);
-        lcd.printString("Press Start",10,3);
-        lcd.printString("to Proceed",12,4);
-        lcd.refresh();
-        while (pad.check_event(Gamepad::START_PRESSED) == false) {
-            pad.leds_on();
-            pad.tone(rand()%1001,0.1); //helps to set max freq for random sound
-            wait(0.1);
-            pad.leds_off();
-            wait(0.1);
-        }
-        lcd.clear();
-        sprintf(bufferlevel,"%d",level);
-        lcd.printString("Level:",25,1);
-        lcd.printString(bufferlevel,40,3);
-        lcd.refresh();
-        wait(2);
-        length = 3;
-    }
-    return length;
 }
 
 
-Vector2D Snake::get_pos(int length)
+Vector2D Snake::get_pos()
 {
     for(int i=1; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos = {_x[i-1],_y[i-1]}; //Obtains the snake position.
             return snakepos;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
             Vector2D snakepos = {_x[9],_y[9]}; //Obtains the snake position.
             return snakepos;
     }
@@ -111,15 +87,15 @@
     //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y);
 }
 
-Vector2D Snake::get_pos_before1(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before1()  //this sends data of following sprites.
 {
     for(int i=2; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b1 = {_x[i-2],_y[i-2]}; //Obtains the snake position.
             return snakepos_b1;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
             Vector2D snakepos_b1 = {_x[8],_y[8]}; //Obtains the snake position.
             return snakepos_b1;
     }
@@ -128,15 +104,15 @@
     //printf("snakepos_b1 from player = %f %f \n", snakepos_b1.x, snakepos_b1.y);
 }
 
-Vector2D Snake::get_pos_before2(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before2()  //this sends data of following sprites.
 {
     for(int i=3; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b2 = {_x[i-3],_y[i-3]}; //Obtains the snake position.
             return snakepos_b2;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
             Vector2D snakepos_b2 = {_x[7],_y[7]}; //Obtains the snake position.
             return snakepos_b2;
     }
@@ -145,15 +121,15 @@
     //printf("snakepos_b2 from player = %f %f \n", snakepos_b2.x, snakepos_b2.y);
 }
 
-Vector2D Snake::get_pos_before3(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before3()  //this sends data of following sprites.
 {
     for(int i=4; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b3 = {_x[i-4],_y[i-4]}; //Obtains the snake position.
             return snakepos_b3;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
             Vector2D snakepos_b3 = {_x[6],_y[6]}; //Obtains the snake position.
             return snakepos_b3;
     }
@@ -162,15 +138,15 @@
     //printf("snakepos_b3 from player = %f %f \n", snakepos_b3.x, snakepos_b3.y);
 }
 
-Vector2D Snake::get_pos_before4(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before4()  //this sends data of following sprites.
 {
     for(int i=5; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b4 = {_x[i-5],_y[i-5]}; //Obtains the snake position.
             return snakepos_b4;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
             Vector2D snakepos_b4 = {_x[5],_y[5]}; //Obtains the snake position.
             return snakepos_b4;
     }
@@ -179,15 +155,15 @@
     //printf("snakepos_b4 from player = %f %f \n", snakepos_b4.x, snakepos_b4.y);
 }
 
-Vector2D Snake::get_pos_before5(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before5()  //this sends data of following sprites.
 {
     for(int i=6; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b5 = {_x[i-6],_y[i-6]}; //Obtains the snake position.
             return snakepos_b5;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
             Vector2D snakepos_b5 = {_x[4],_y[4]}; //Obtains the snake position.
             return snakepos_b5;
     }
@@ -196,15 +172,15 @@
     //printf("snakepos_b5 from player = %f %f \n", snakepos_b5.x, snakepos_b5.y);
 }
 
-Vector2D Snake::get_pos_before6(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before6()  //this sends data of following sprites.
 {
     for(int i=7; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b6 = {_x[i-7],_y[i-7]}; //Obtains the snake position.
             return snakepos_b6;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
             Vector2D snakepos_b6 = {_x[3],_y[3]}; //Obtains the snake position.
             return snakepos_b6;
     }
@@ -213,15 +189,15 @@
     //printf("snakepos_b6 from player = %f %f \n", snakepos_b6.x, snakepos_b6.y);
 }
 
-Vector2D Snake::get_pos_before7(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before7()  //this sends data of following sprites.
 {
     for(int i=8; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b7 = {_x[i-8],_y[i-8]}; //Obtains the snake position.
             return snakepos_b7;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
         Vector2D snakepos_b7 = {_x[2],_y[2]}; //Obtains the snake position.
         return snakepos_b7;
     }
@@ -230,15 +206,15 @@
     //printf("snakepos_b7 from player = %f %f \n", snakepos_b7.x, snakepos_b7.y);
 }
 
-Vector2D Snake::get_pos_before8(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before8()  //this sends data of following sprites.
 {
     for(int i=9; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b8 = {_x[i-9],_y[i-9]}; //Obtains the snake position.
             return snakepos_b8;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
         Vector2D snakepos_b8 = {_x[1],_y[1]}; //Obtains the snake position.
         return snakepos_b8;
     }
@@ -247,15 +223,15 @@
     //printf("snakepos_b8 from player = %f %f \n", snakepos_b8.x, snakepos_b8.y);
 }
 
-Vector2D Snake::get_pos_before9(int length)  //this sends data of following sprites.
+Vector2D Snake::get_pos_before9()  //this sends data of following sprites.
 {
     for(int i=10; i<=10; i++)  {
-        if(length == i)  {
+        if(_length == i)  {
             Vector2D snakepos_b9 = {_x[i-10],_y[i-10]}; //Obtains the snake position.
             return snakepos_b9;
         }
     }
-    if(length >= 10)  {
+    if(_length >= 10)  {
         Vector2D snakepos_b9 = {_x[0],_y[0]}; //Obtains the snake position.
         return snakepos_b9;
     }
@@ -264,11 +240,10 @@
     //printf("snakepos_b9 from player = %f %f \n", snakepos_b9.x, snakepos_b9.y);
 }
 
-void Snake::update(Direction d,int length, int speed, int* b)
+void Snake::update(Direction d, int* b)
 {
-    _speed = speed;  //Speed changes depending on how much you push the joystick.(As Of Now)
-    if(length<=10) {_length = length;} //to stop the snake length virtually at 10 when it goes past it.
-    else {_length = 10;}
+    if(_length >= 10) {_length = 10;} //to stop the snake length virtually at 10 when it goes past it.
+    
     //this makes all of the snake beeds chained together by making the lower ones drag towards where the top one was in the previous loop
     //the b[i] makes sure that the snake beed doesn't move if that beed is deactivated by colliding with a barrier.
     for(int i=0; i<=13; i++)  {