Samuel Lashmar 201170334
Dependencies: mbed
Diff: SnakeHead/SnakeHead.cpp
- Revision:
- 5:256e5e0b6cd7
- Child:
- 7:c67a5c6a874f
--- /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