Final Commit

Dependencies:   mbed

Revision:
16:85ca9feccf3f
Parent:
14:c3a435597196
Child:
17:94dd8a691d4a
--- a/SnakeEngine/SnakeEngine.cpp	Thu Apr 12 12:41:05 2018 +0000
+++ b/SnakeEngine/SnakeEngine.cpp	Wed Apr 18 10:38:10 2018 +0000
@@ -10,8 +10,51 @@
 {
 
 }
+/*
+void SnakeEngine::draw_tail(N5110 &lcd)
+{
+    
+    if(detect_food_collision()) {
+        
+    Vector2D tail_pos[100];
+    Vector2D pre;
+    Vector2D pre_pre;
+    tail_pos[0] = _snake.get_snake_position();
+    int i;
+    int length = g_tl;
+    
+    for(i = 0; i <= length; i++) { // i may need to be 1
+    
+    pre_pre = tail_pos[i];
+    tail_pos[i] = pre;
+    pre_pre = pre_pre;
+    
+    lcd.setPixel(pre.x,pre.y,true);
+    
+    }
+    
+    }
+    
+    else {
+        
+        g_tl = g_tl;
+        
+        }
+    
+}*/
 
 
+
+
+int g_tail_length()
+{
+    
+    extern int g_tl;
+    
+    return g_tl;
+    
+}
+
 void SnakeEngine::init(int snake_position_x, int snake_position_y)
 {
     _snake_position_x = snake_position_x;
@@ -29,8 +72,14 @@
 
 void SnakeEngine::update(Gamepad &pad)
 {
+    int length;
+    bool food_col;
     
     _snake.update(_in, _cur);
+    food_col = detect_food_collision(pad);
+    set_tail_length(food_col);
+    length = get_tail_length();
+    printf("Tail Length = %i \n", length); 
 
 }
 
@@ -45,4 +94,57 @@
     _snake.update(_in, _cur);
     _snake.draw(lcd);
     
-}
\ No newline at end of file
+}
+
+bool SnakeEngine::detect_food_collision(Gamepad &pad)
+{
+    Vector2D snake_pos = _snake.get_snake_position();
+    Vector2D food_pos = _food.get_food_position();
+    
+    bool success_flag = false;
+    
+    if((snake_pos.x == food_pos.x) && (snake_pos.y == food_pos.y)) {
+        
+        success_flag = true;
+        
+        }
+        
+        else {
+            
+            success_flag = false;
+            
+            }
+        
+    return success_flag;
+    
+}
+
+void SnakeEngine::set_tail_length(bool collision_detected)
+{
+    
+    if(collision_detected) {
+        
+        ++g_tl;
+        
+        }
+        
+        else {
+            
+            g_tl = g_tl;
+            
+            }
+            
+}
+
+int SnakeEngine::get_tail_length()
+{
+    
+    int length = g_tl;
+    
+    return length;
+    
+}
+          
+            
+    
+    
\ No newline at end of file