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.
Diff: Snek/Snek.cpp
- Revision:
- 4:6353f829c56c
- Parent:
- 3:6253a2d374fa
- Child:
- 5:a3a9e0417e04
--- a/Snek/Snek.cpp Sun Apr 29 14:19:38 2018 +0000 +++ b/Snek/Snek.cpp Sun Apr 29 14:59:14 2018 +0000 @@ -14,44 +14,58 @@ void Snek::init(int x, int y) { //Inital values for variables - _x = x; - _y = y; printf ("floats: %i %i \n", _x, _y); + _length = 3; + + for (int i = 0; i < _length; i++) { + _x[i] = x; + _y[i] = y; + } } void Snek::update(Direction d) { if (d == N) { - _y -= 1; + _y[0] -= 1; _oldDirection = 'N'; } else if (d == S) { - _y += 1; + _y[0] += 1; _oldDirection = 'S'; } else if (d == E) { - _x += 1; + _x[0] += 1; _oldDirection = 'E'; } else if (d == W) { - _x -= 1; + _x[0] -= 1; _oldDirection = 'W'; } else { if (_oldDirection == 'N') { - _y -= 1; + _y[0] -= 1; } else if (_oldDirection == 'S') { - _y += 1; + _y[0] += 1; } else if (_oldDirection == 'E') { - _x += 1; + _x[0] += 1; } else if (_oldDirection == 'W') { - _x -= 1; + _x[0] -= 1; } } + + for (int i = _length; i >= 1; i--) { + _x[i] = _x[i-1]; + _y[i] = _y[i-1]; + } } -int Snek::getX() +int Snek::getX(int ref) { - return _x; + return _x[ref]; } -int Snek::getY() +int Snek::getY(int ref) { - return _y; + return _y[ref]; } + +int Snek::getLength() +{ + return _length; +}