ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el17sdl_v2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
sdlashmar
Date:
Thu May 14 12:06:26 2020 +0000
Parent:
4:c5addc5475d3
Child:
6:964cc4896627
Commit message:
snake tail rev 1

Changed in this revision

Snake/Snake.cpp Show diff for this revision Revisions of this file
Snake/Snake.h Show diff for this revision Revisions of this file
SnakeHead/SnakeHead.cpp Show annotated file Show diff for this revision Revisions of this file
SnakeHead/SnakeHead.h Show annotated file Show diff for this revision Revisions of this file
SnakeTail/SnakeTail.cpp Show annotated file Show diff for this revision Revisions of this file
SnakeTail/SnakeTail.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Snake/Snake.cpp	Fri May 08 08:40:29 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,104 +0,0 @@
-#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 { //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() {
-
-    _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;
-        }
-        
-}
-
-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
--- a/Snake/Snake.h	Fri May 08 08:40:29 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-#ifndef SNAKE_H
-#define SNAKE_H 
-
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
-#include "time.h"
-
-class Snake
-{
-
-public:
-    Snake();
-    ~Snake();
-    void init(int size, int speed);
-    void draw(N5110 &lcd);
-    void update();
-    void change_direction(Direction d);
-    void set_velocity(Vector2D v);
-    Vector2D get_velocity();
-    Vector2D get_pos();
-    void set_pos(Vector2D p);
-    
-    
-private: 
-    Vector2D _velocity;  
-    Gamepad pad; 
-    int _size;
-    int _speed;
-    int _x;
-    int _y;
-};
-
-#endif      
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SnakeHead/SnakeHead.cpp	Thu May 14 12:06:26 2020 +0000
@@ -0,0 +1,104 @@
+#include "SnakeHead.h"
+
+SnakeHead::SnakeHead()
+{
+    
+}
+
+SnakeHead::~SnakeHead()
+{
+    
+}
+
+void SnakeHead::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 { //snake moves west
+        _velocity.x = 0;
+        _velocity.y = -_speed;
+        }
+}
+
+void SnakeHead::draw(N5110 &lcd) {
+    lcd.drawRect(_x,_y,_size,2,FILL_BLACK);
+}
+
+void SnakeHead::update() {
+
+    _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 SnakeHead::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;
+        }
+        
+}
+
+void SnakeHead::set_velocity(Vector2D v) {
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+
+
+Vector2D SnakeHead::get_velocity() {
+
+    Vector2D v = {_velocity.x, _velocity.y};
+    return v;
+}
+
+Vector2D SnakeHead::get_pos() {
+        
+        Vector2D p = {_x, _y};
+        return p;
+}
+
+void SnakeHead::set_pos(Vector2D p) {
+    
+        _x = p.x;
+        _y = p.y;
+}        
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SnakeHead/SnakeHead.h	Thu May 14 12:06:26 2020 +0000
@@ -0,0 +1,34 @@
+#ifndef SNAKEHEAD_H
+#define SNAKEHEAD_H 
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "time.h"
+
+class SnakeHead
+{
+
+public:
+    SnakeHead();
+    ~SnakeHead();
+    void init(int size, int speed);
+    void draw(N5110 &lcd);
+    void update();
+    void change_direction(Direction d);
+    void set_velocity(Vector2D v);
+    Vector2D get_velocity();
+    Vector2D get_pos();
+    void set_pos(Vector2D p);
+    
+    
+private: 
+    Vector2D _velocity;  
+    Gamepad pad; 
+    int _size;
+    int _speed;
+    int _x;
+    int _y;
+};
+
+#endif      
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SnakeTail/SnakeTail.cpp	Thu May 14 12:06:26 2020 +0000
@@ -0,0 +1,43 @@
+#include "SnakeTail.h"
+
+SnakeTail::SnakeTail()
+{
+    
+}
+
+SnakeTail::~SnakeTail()
+{
+    
+}
+
+
+void SnakeTail::set_length(int length)
+{
+     _length = length;
+}
+
+void SnakeTail::draw_tail(N5110 &lcd, Vector2D headPos)
+{
+    tailX[0] = headPos.x;
+    tailY[0] = headPos.y;
+    int prevX = tailX[0];
+    int prevY = tailY[0];
+    int prev2X = prevX;
+    int prev2Y = prevY;
+    
+    
+    for (int i = 1; i<_length; i++) {
+         prev2X = prevX;
+         prev2Y = prevY;
+         prevX = tailX[i];
+         prevY = tailY[i];
+         tailX[i] = headPos.x;
+         tailY[i] = headPos.y;
+         lcd.drawRect(prev2X, prev2Y, 2, 2, FILL_BLACK);
+         };
+}
+
+
+
+
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SnakeTail/SnakeTail.h	Thu May 14 12:06:26 2020 +0000
@@ -0,0 +1,28 @@
+#ifndef SNAKETAIL_H
+#define SNAKETAIL_H
+
+#include "mbed.h"
+#include "Gamepad.h"
+#include "N5110.h"
+
+class SnakeTail 
+
+{
+
+public: 
+    
+    SnakeTail();
+    ~SnakeTail();
+    void set_length(int length);
+    void draw_tail(N5110 &lcd, Vector2D headPos);
+    
+
+private: 
+    
+    int tailX[];
+    int tailY[];
+    int _length;
+};
+
+#endif
+    
\ No newline at end of file
--- a/main.cpp	Fri May 08 08:40:29 2020 +0000
+++ b/main.cpp	Thu May 14 12:06:26 2020 +0000
@@ -14,15 +14,16 @@
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
-#include "Snake.h"
-
+#include "SnakeHead.h"
+#include "SnakeTail.h"
 
 
 // objects
 Gamepad pad;
 N5110 lcd;
 BusIn input(PTC5, PTC9, PTC7);
-Snake snake;
+SnakeHead head;
+SnakeTail tail;
 
 void welcome();
 
@@ -61,15 +62,22 @@
     switch(state) {
         case 3:
             
-            snake.init(1,3);
+            head.init(1,3);
+           
+            
             
             while(1) {
                 lcd.clear();
-                snake.draw(lcd);
+                head.draw(lcd);
                 Direction _d = pad.get_direction();
                 //printf("Stick direction: %s", d);
-                snake.change_direction(_d);
-                snake.update();
+                head.change_direction(_d);
+                head.update();
+                Vector2D headPos = head.get_pos();
+                //printf("pos x = %i\n", headPos.x);
+                tail.set_length(15);
+                tail.draw_tail(lcd, headPos);
+                
                 lcd.refresh();
                 wait(0.5);
             }