Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Snek/Snek.cpp
- Committer:
- Andrew_M
- Date:
- 2018-05-07
- Revision:
- 12:d3eef5ea3f43
- Parent:
- 7:c1e0593bfc99
- Child:
- 15:130900e5c268
File content as of revision 12:d3eef5ea3f43:
#include "Snek.h"
// nothing doing in the constructor and destructor
Snek::Snek()
{
}
Snek::~Snek()
{
}
void Snek::init(int x, int y)
{
memset(_x, 0, sizeof(_x));
memset(_y, 0, sizeof(_y));
//Inital values for variables
_length = 3;
_oldDirection = 'N';
for (int i = 0; i < _length; i++) {
_x[i] = x;
_y[i] = y + i;
}
}
void Snek::update(Direction d)
{
for (int i = _length-1; i >= 1; i--) {
_x[i] = _x[i-1];
_y[i] = _y[i-1];
}
if (d == N && _oldDirection != 'S') { //checks the current direction so the snake cannot go back on itself
_y[0] -= 1;
_oldDirection = 'N';
} else if (d == S && _oldDirection != 'N') {
_y[0] += 1;
_oldDirection = 'S';
} else if (d == E && _oldDirection != 'W') {
_x[0] += 1;
_oldDirection = 'E';
} else if (d == W && _oldDirection != 'E') {
_x[0] -= 1;
_oldDirection = 'W';
} else {
if (_oldDirection == 'N') {
_y[0] -= 1;
} else if (_oldDirection == 'S') {
_y[0] += 1;
} else if (_oldDirection == 'E') {
_x[0] += 1;
} else if (_oldDirection == 'W') {
_x[0] -= 1;
}
}
}
int Snek::getX(int ref)
{
return _x[ref];
}
int Snek::getY(int ref)
{
return _y[ref];
}
int Snek::getLength()
{
return _length;
}
void Snek::grow()
{
_length+=1;
}