Final Commit

Dependencies:   mbed

Revision:
17:94dd8a691d4a
Parent:
16:85ca9feccf3f
Child:
18:406fc298a7c4
--- a/SnakeEngine/SnakeEngine.cpp	Wed Apr 18 10:38:10 2018 +0000
+++ b/SnakeEngine/SnakeEngine.cpp	Wed Apr 25 10:21:54 2018 +0000
@@ -10,41 +10,131 @@
 {
 
 }
-/*
-void SnakeEngine::draw_tail(N5110 &lcd)
+
+
+
+void SnakeEngine::set_even_array(Gamepad &pad)
 {
     
-    if(detect_food_collision()) {
+    int i;
+    int length = g_tl;
+    
+    extern int g_odd_array_x[100];
+    extern int g_odd_array_y[100];
+    
+    Vector2D pos = _snake.get_snake_position();
+       
+    g_odd_array_x[0] = pos.x;
+    g_odd_array_y[0] = pos.y;
+    
+    for(i = length; i >= 0; i--) {
+        
+        if(i > 0) {
+            
+        g_even_array_x[i] = g_odd_array_x[i-1];
+        g_even_array_y[i] = g_odd_array_y[i-1];
         
-    Vector2D tail_pos[100];
-    Vector2D pre;
-    Vector2D pre_pre;
-    tail_pos[0] = _snake.get_snake_position();
+        }
+
+        else if(i == 0) {
+            
+            g_even_array_x[0] = pos.x;
+            g_even_array_y[0] = pos.y;
+            
+            }
+     
+
+    }
+    
+}
+
+void SnakeEngine::set_odd_array(Gamepad &pad)
+{
+    
     int i;
     int length = g_tl;
     
-    for(i = 0; i <= length; i++) { // i may need to be 1
+    extern int g_even_array_x[100];
+    extern int g_even_array_y[100];
+    
+    Vector2D pos = _snake.get_snake_position();
+    
+    g_even_array_x[0] = pos.x;
+    g_even_array_y[0] = pos.y;
     
-    pre_pre = tail_pos[i];
-    tail_pos[i] = pre;
-    pre_pre = pre_pre;
-    
-    lcd.setPixel(pre.x,pre.y,true);
-    
+    for(i = length; i >= 0; i--) {
+        
+        if(i > 0) {
+            
+        g_odd_array_x[i] = g_even_array_x[i-1];
+        g_odd_array_y[i] = g_even_array_y[i-1];
+        
+        }
+
+        else if(i == 0) {
+            
+            g_odd_array_x[0] = pos.x;
+            g_odd_array_y[0] = pos.y;
+            
+            }
+        
     }
     
-    }
+}
+
+
+void SnakeEngine::set_tail_array(Gamepad &pad)
+{
     
-    else {
+    int c = g_engine_fc % 2;
+    
+    if(c == 1) {
         
-        g_tl = g_tl;
+        set_odd_array(pad);
         
         }
-    
-}*/
+        
+        else if (c == 0) {
+            
+            set_even_array(pad);
+            
+            }
+            
+}
 
-
-
+void SnakeEngine::draw_tail(N5110 &lcd)
+{
+    int length = g_tl;
+    int c;
+    c = g_engine_fc % 2;
+    int i;
+    int x;
+    int y;
+    
+    printf("Odd/Even %i \n", c);
+    
+    for(i=0; i<=length; i++) {
+            
+    if(c == 1) {
+        
+        x = g_odd_array_x[i];
+        y = g_odd_array_y[i];
+        
+        lcd.setPixel(x,y,true);
+                
+        }
+        
+        else if (c == 0) {
+        
+            x = g_even_array_x[i];
+            y = g_even_array_y[i];
+            
+            lcd.setPixel(x,y,true);
+                        
+            }
+            
+        }
+}
 
 int g_tail_length()
 {
@@ -55,6 +145,7 @@
     
 }
 
+
 void SnakeEngine::init(int snake_position_x, int snake_position_y)
 {
     _snake_position_x = snake_position_x;
@@ -66,6 +157,7 @@
 
 void SnakeEngine::get_input(Gamepad &pad)
 {
+    
     _in = pad.get_direction();
     
 }
@@ -74,12 +166,15 @@
 {
     int length;
     bool food_col;
+    ++g_engine_fc;
+    
     
     _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); 
+    printf("Tail Length = %i \n", length);
+    set_tail_array(pad);
 
 }
 
@@ -93,6 +188,7 @@
     _food.draw(lcd);
     _snake.update(_in, _cur);
     _snake.draw(lcd);
+    draw_tail(lcd);
     
 }