Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Revision:
2:44e4a6ecdbef
Parent:
1:897160a1a3ae
Child:
3:d17d712c2e45
--- a/snake.cpp	Tue May 26 12:17:59 2020 +0000
+++ b/snake.cpp	Tue May 26 18:35:44 2020 +0000
@@ -14,8 +14,8 @@
 void Snake::init ()
 {
 
-    //WIDTH =84 HEIGHT = 42 //snakebody[0] is initialised may have to initilaise the other 2 og snake parts
-    _length = 1;
+    //WIDTH =84 HEIGHT = 42 //snakebody[0] is initalised may have to initilaise the other 2 og snake parts
+    _length = 2;                                                // ROB how do i initalise
     _gameover = false;
     _score = 0;
     _direction = up;
@@ -31,74 +31,82 @@
 
 
     if (pad.A_pressed()) {
-
         _direction = right;
-
-
     }
 
     if (pad.B_pressed()) {
-
         _direction = left;     //check these are orrecrt
-
     }
 
     if (pad.X_pressed()) {
-
         _direction = up;
-
     }
-
     if (pad.Y_pressed()) {
-
-
         _direction = down;
-
     }
-
     else {
-
         _direction = _direction;
     }
     return _direction;
 }
 
 
-void Snake::move_and_draw_snake(N5110 &lcd)
+void Snake::move_and_draw_snake(Gamepad &pad, N5110 &lcd)
 {
-    Vector2D Snakehead = _engine.get_Snakehead();           //initialises Snakehead value
-    Vector2D *snakebody = new Vector2D [_length];
+    Vector2D Snakehead = _engine.get_Snakehead();
+    Vector2D oldSnakehead = _engine.get_oldSnakehead();
+
+    Vector2D *_snakebody = new Vector2D [_length];            // ROB im unsure that this code would work- creates dynamic array - each bit having 2 parts (x,y) - number of bits of snakebody = length
 
-    snakebody[0].x = Snakehead.x;
-    snakebody[0].y = Snakehead.y;
+    _snakebody[0].x = Snakehead.x;                            // snakebody[0] = Snakehead
+    _snakebody[0].y = Snakehead.y;
+    _snakebody[1].x = oldSnakehead.x;
+    _snakebody[1].y = oldSnakehead.y;
 
     while(1) {
-        if (_direction == up) {
-            Snakehead.y++;                              // alters Snakehead initial value
+        if (_direction == up) {           //           /-/
+            Snakehead.y++;                           ///// alters Snakehead initial value
+            oldSnakehead.y = (Snakehead.y - 1);
+            oldSnakehead.x = (Snakehead.x);                                          //    Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function
         }
-        if (_direction == down) {             //this part is needed to keep the snake running continually
+        if (_direction == down) {             // Direction should remain  after button is pressed - should not just change the snakehead coordinate once
             Snakehead.y--;
+            oldSnakehead.y = (Snakehead.y + 1);
+            oldSnakehead.x = (Snakehead.x);                 //
         }
         if (_direction == left) {
             Snakehead.x--;
+            oldSnakehead.x = (Snakehead.x + 1);
+            oldSnakehead.y = (Snakehead.y);
         }
         if (_direction == right) {
-            Snakehead.x++;                         //updates the head position before rendering so when it draws i=1 the 0 position will be new
+            Snakehead.x++;
+            oldSnakehead.x = (Snakehead.x - 1);         //updates the head position before rendering so when it draws i=1 the 0 position will be new
+            oldSnakehead.y = (Snakehead.y);
         }
 
-        for(int i=1; i<=_length; i++) {                     //0 being head of snake  so snakepos[1]=snakepos[0] which is the head - moving up one place
-            snakebody[i].x = snakebody[i-1].x;
-            snakebody[i].y = snakebody[i-1].y;
-            lcd.setPixel(snakebody[i].x, snakebody[i].y, 1);
-            if(snakebody[i].x == Snakehead.x && snakebody[i].y == Snakehead.y) {  //is snakebody[0] being plotted
+
+        for(int i=2; i<=_length; i++) {                     //0 being head of snake  so snakepos[1]=snakepos[0] which is the head - moving up one place
+            _snakebody[i].x = _snakebody[i-1].x;              //Snake needs to follow itself - low numbers lead the way
+            _snakebody[i].y = _snakebody[i-1].y;
+            if(_snakebody[i].x == Snakehead.x && _snakebody[i].y == Snakehead.y) {  // ROB Unsure how to initially define the snake with (3 bits, when score =0) starting position  *without* redeclaring Vector2D *snakebody = new Vector2D [_length];
                 _gameover = true;
 
             }
         }
+
+        for(int i = 0; i <= _length; i++) {
+            lcd.setPixel(_snakebody[i].x, _snakebody[i].y, 1);
+
+            //ROB is snakebody[0] being plotted? do you think this would work?
+            // ideally would define snakebody[0], snakebody[1], snake[2] so the snake starts as 3 bits and bit 1 knows to go to bit zero, bit 2 know to go to bit 1 position
+
+        }
     }
+}
 
 
-}
+
 
 
 void Snake::gameover_true(N5110 &lcd)      //taking action if crash has occured
@@ -108,7 +116,7 @@
         lcd.clear();
         lcd.refresh();
         lcd.printString( "  Game Over L ", 0, 2 );
-        lcd.printString ("score: _score ",15, 15);    //Need to add button to return to main screen / restart
+        lcd.printString ("score: _score ",15, 15);    //Need to add button to return to main screen / restart  //NEED to return score without ruining structure of lcd.PrintString( "string", x, y)
 
     }
 
@@ -120,6 +128,7 @@
 
 
 
+
 void Snake::check_if_scored(N5110 &lcd, Gamepad &pad)
 {
     Vector2D Snakehead = _engine.get_Snakehead();
@@ -127,8 +136,8 @@
 
     if(Snakehead.x == Applepos.x && Snakehead.y == Applepos.y) {
 
-
-        _score++;
+        lcd.setPixel(Applepos.x, Applepos.y, 0);   // Problem:: Wanted to make sure that this didnt clear the pixel of the snake as it passes through it
+        _score++;                                 //by waiting unil the snake has passed through before clearing the old apple however the new apple spawn needs to be ASAP after collection
         _length = _length++;
         _engine.set_Applepos(lcd);    //randomises new values for _apx,_apy and draws on lcd -- the position is not needed until we compare it to Snakehead
         pad.tone(1500.0,0.5);          //need to clear apple