ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el17sdl_v2

Dependencies:   mbed

Revision:
4:c5addc5475d3
Parent:
3:36f9e3a75905
--- a/Snake/Snake.cpp	Wed May 06 11:35:18 2020 +0000
+++ b/Snake/Snake.cpp	Fri May 08 08:40:29 2020 +0000
@@ -21,20 +21,20 @@
     int direction = rand() %4;
     
     if (direction == 0) { //snake moves north
-        _velocity.x = -speed;
+        _velocity.x = -_speed;
         _velocity.y = 0;
         }
     else if (direction == 1) { //snake moves east
         _velocity.x = 0;
-        _velocity.y = speed;
+        _velocity.y = _speed;
         }
     else if (direction == 2) { //sake moves south
-        _velocity.x = speed;
+        _velocity.x = _speed;
         _velocity.y = 0;
         }
-    else if (direction == 3) { //snake moves west
+    else { //snake moves west
         _velocity.x = 0;
-        _velocity.y = -speed;
+        _velocity.y = -_speed;
         }
 }
 
@@ -42,20 +42,39 @@
     lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
 }
 
-void Snake::update(Direction d) {
+void Snake::update() {
 
-    if (d == N) {
-        _x -= _speed;
+    _x += _velocity.x;
+    _y += _velocity.y;
+    
+    if (_x < 0) {
+        _x = 1;
+    } else if (_x > 84) {
+        _x = 84 - _size;
+    } else if (_y < 0) {
+        _y = 1;
+    } else if (_y > 48) {
+        _y = 48 - _size;
+    }
+    
+}
+
+void Snake::change_direction(Direction d) {
+        
+        if (d == N) {
+            _velocity.x = 0;
+            _velocity.y = -_speed;
+        } else if (d == E) {
+            _velocity.x = _speed;
+            _velocity.y = 0;
+        } else if (d == S) {
+            _velocity.x = 0;
+            _velocity.y = _speed;
+        } else if (d == W) {
+            _velocity.x = -_speed;
+            _velocity.y = 0;
         }
-    else if (d == E) {
-        _y += _speed;
-        }
-    else if (d == S) {
-        _x += _speed;
-        }
-    else if (d == W) {
-        _y -= _speed;
-    }
+        
 }
 
 void Snake::set_velocity(Vector2D v) {
@@ -63,6 +82,8 @@
     _velocity.y = v.y;
 }
 
+
+
 Vector2D Snake::get_velocity() {
 
     Vector2D v = {_velocity.x, _velocity.y};