ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_ll16j23s

Dependencies:   mbed ll16j23s_test_docs

Revision:
9:0571880085cc
Parent:
8:bcc3403d7e79
Child:
10:a2d643b3c782
--- a/SnakeBody/SnakeBody.cpp	Mon May 25 14:45:32 2020 +0000
+++ b/SnakeBody/SnakeBody.cpp	Mon May 25 20:31:52 2020 +0000
@@ -10,8 +10,7 @@
     _d = 0;
 }
 
-SnakeBody::~SnakeBody()
-{
+SnakeBody::~SnakeBody() {
 //destructor
 }
 
@@ -85,30 +84,40 @@
         _body_x.erase(_body_x.begin() + _length, _body_x.end());
         _body_y.erase(_body_y.begin() + _length, _body_y.end());
     }
+    if(_length_increase > 0){
+        _length++;
+        _length_increase--;
+        //_body_y.at(_length + 1) = 0;
+    } 
 }
 
 
 void SnakeBody::draw_body(N5110 &lcd){
     
     lcd.drawRect(_x_head,_y_head,2,2,FILL_BLACK);
-    for(int i = 0; i < _length; i++) {
+    for(int i = 0; i < _length - 3; i++) {
         lcd.drawRect(_body_x[i],_body_y[i],2,2,FILL_BLACK);
     }
 }
 
-bool SnakeBody::snake_snake_collision(Gamepad &pad){
-   if (_state > 0) { 
-        for(int i = 3; i < _length; i++) {
+void SnakeBody::snake_snake_collision(Gamepad &pad, bool &_death){
+   if (_state > 0) {
+        for(int i = 3; i < _length - 3; i++) {
             if (_x_head == _body_x[i] && _y_head == _body_y[i]) { //checks if head coord is the same as any of the body coords
-                pad.led(1,0.9);
-                //printf("DEAD \n");
+                printf("DEAD \n");
                 _state = 0;
-                return true;
-            } else {
-                //pad.led(1,0.0);
-                //printf("ALIVE \n");
-                //return false;
+                _death = true;
             }
         }
     }    
+}
+
+void SnakeBody::add_length(int increase){
+        _length_increase += increase;   
+}
+
+void SnakeBody::run(Gamepad &pad, N5110 &lcd, bool &_death){
+    snake_movement(pad);
+    draw_body(lcd);
+    snake_snake_collision(pad, _death);
 }
\ No newline at end of file