ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el17sdl_v2

Dependencies:   mbed

Revision:
3:36f9e3a75905
Child:
4:c5addc5475d3
diff -r 6792f99c3c0d -r 36f9e3a75905 Snake/Snake.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Snake/Snake.cpp	Wed May 06 11:35:18 2020 +0000
@@ -0,0 +1,83 @@
+#include "Snake.h"
+
+Snake::Snake()
+{
+    
+}
+
+Snake::~Snake()
+{
+    
+}
+
+void Snake::init(int size, int speed) {
+    _size = size*2;
+    _speed = speed;
+    
+    _x = WIDTH/2 - _size/2;
+    _y = HEIGHT/2 - _size/2;
+    
+    srand(time(NULL));
+    int direction = rand() %4;
+    
+    if (direction == 0) { //snake moves north
+        _velocity.x = -speed;
+        _velocity.y = 0;
+        }
+    else if (direction == 1) { //snake moves east
+        _velocity.x = 0;
+        _velocity.y = speed;
+        }
+    else if (direction == 2) { //sake moves south
+        _velocity.x = speed;
+        _velocity.y = 0;
+        }
+    else if (direction == 3) { //snake moves west
+        _velocity.x = 0;
+        _velocity.y = -speed;
+        }
+}
+
+void Snake::draw(N5110 &lcd) {
+    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
+}
+
+void Snake::update(Direction d) {
+
+    if (d == N) {
+        _x -= _speed;
+        }
+    else if (d == E) {
+        _y += _speed;
+        }
+    else if (d == S) {
+        _x += _speed;
+        }
+    else if (d == W) {
+        _y -= _speed;
+    }
+}
+
+void Snake::set_velocity(Vector2D v) {
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+Vector2D Snake::get_velocity() {
+
+    Vector2D v = {_velocity.x, _velocity.y};
+    return v;
+}
+
+Vector2D Snake::get_pos() {
+        
+        Vector2D p = {_x, _y};
+        return p;
+}
+
+void Snake::set_pos(Vector2D p) {
+    
+        _x = p.x;
+        _y = p.y;
+}        
+    
\ No newline at end of file