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@17:2a909f7da973, 2018-05-08 (annotated)
- Committer:
- Andrew_M
- Date:
- Tue May 08 14:52:29 2018 +0000
- Revision:
- 17:2a909f7da973
- Parent:
- 15:130900e5c268
More comments and debugging tools added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Andrew_M | 1:a14415de3ad5 | 1 | #include "Snek.h" |
Andrew_M | 1:a14415de3ad5 | 2 | |
Andrew_M | 1:a14415de3ad5 | 3 | // nothing doing in the constructor and destructor |
Andrew_M | 1:a14415de3ad5 | 4 | Snek::Snek() |
Andrew_M | 1:a14415de3ad5 | 5 | { |
Andrew_M | 1:a14415de3ad5 | 6 | |
Andrew_M | 1:a14415de3ad5 | 7 | } |
Andrew_M | 1:a14415de3ad5 | 8 | |
Andrew_M | 1:a14415de3ad5 | 9 | Snek::~Snek() |
Andrew_M | 1:a14415de3ad5 | 10 | { |
Andrew_M | 1:a14415de3ad5 | 11 | |
Andrew_M | 1:a14415de3ad5 | 12 | } |
Andrew_M | 1:a14415de3ad5 | 13 | |
Andrew_M | 2:9ca5e1c221c3 | 14 | void Snek::init(int x, int y) |
Andrew_M | 1:a14415de3ad5 | 15 | { |
Andrew_M | 15:130900e5c268 | 16 | memset(_x, 0, sizeof(_x)); //clears the x coordinate array |
Andrew_M | 15:130900e5c268 | 17 | memset(_y, 0, sizeof(_y)); //clears the y coordinate array |
Andrew_M | 17:2a909f7da973 | 18 | |
Andrew_M | 1:a14415de3ad5 | 19 | //Inital values for variables |
Andrew_M | 4:6353f829c56c | 20 | _length = 3; |
Andrew_M | 7:c1e0593bfc99 | 21 | |
Andrew_M | 5:a3a9e0417e04 | 22 | _oldDirection = 'N'; |
Andrew_M | 4:6353f829c56c | 23 | |
Andrew_M | 15:130900e5c268 | 24 | for (int i = 0; i < _length; i++) { //sets the starting values for the snake coordinates |
Andrew_M | 15:130900e5c268 | 25 | _x[i] = x; //all have the same X coordinate |
Andrew_M | 17:2a909f7da973 | 26 | _y[i] = y + i; //the Y coordinate is incremmented for each tail segment |
Andrew_M | 4:6353f829c56c | 27 | } |
Andrew_M | 17:2a909f7da973 | 28 | |
Andrew_M | 17:2a909f7da973 | 29 | printf("Snake Initialised\n"); |
Andrew_M | 1:a14415de3ad5 | 30 | } |
Andrew_M | 1:a14415de3ad5 | 31 | |
Andrew_M | 2:9ca5e1c221c3 | 32 | void Snek::update(Direction d) |
Andrew_M | 2:9ca5e1c221c3 | 33 | { |
Andrew_M | 5:a3a9e0417e04 | 34 | for (int i = _length-1; i >= 1; i--) { |
Andrew_M | 7:c1e0593bfc99 | 35 | _x[i] = _x[i-1]; |
Andrew_M | 7:c1e0593bfc99 | 36 | _y[i] = _y[i-1]; |
Andrew_M | 5:a3a9e0417e04 | 37 | } |
Andrew_M | 7:c1e0593bfc99 | 38 | |
Andrew_M | 15:130900e5c268 | 39 | if (d == N && _oldDirection != 'S') { //checks the new direction so the snake cannot go back on itself |
Andrew_M | 15:130900e5c268 | 40 | _y[0] -= 1; //changes a coordinate of the snake's head |
Andrew_M | 15:130900e5c268 | 41 | _oldDirection = 'N'; //updates the _oldDirection so the snake will travel in the new direction with no play input |
Andrew_M | 17:2a909f7da973 | 42 | printf("Snake has changed North\n"); |
Andrew_M | 5:a3a9e0417e04 | 43 | } else if (d == S && _oldDirection != 'N') { |
Andrew_M | 4:6353f829c56c | 44 | _y[0] += 1; |
Andrew_M | 3:6253a2d374fa | 45 | _oldDirection = 'S'; |
Andrew_M | 17:2a909f7da973 | 46 | printf("Snake has changed South\n"); |
Andrew_M | 5:a3a9e0417e04 | 47 | } else if (d == E && _oldDirection != 'W') { |
Andrew_M | 4:6353f829c56c | 48 | _x[0] += 1; |
Andrew_M | 3:6253a2d374fa | 49 | _oldDirection = 'E'; |
Andrew_M | 17:2a909f7da973 | 50 | printf("Snake has changed East\n"); |
Andrew_M | 5:a3a9e0417e04 | 51 | } else if (d == W && _oldDirection != 'E') { |
Andrew_M | 4:6353f829c56c | 52 | _x[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 53 | _oldDirection = 'W'; |
Andrew_M | 17:2a909f7da973 | 54 | printf("Snake has changed West\n"); |
Andrew_M | 15:130900e5c268 | 55 | } else { //if there is no player input (new direction)... |
Andrew_M | 15:130900e5c268 | 56 | if (_oldDirection == 'N') { //checks the old direction... |
Andrew_M | 15:130900e5c268 | 57 | _y[0] -= 1; //and changes a coordinate of the snake's head depending on the _oldDirection |
Andrew_M | 3:6253a2d374fa | 58 | } else if (_oldDirection == 'S') { |
Andrew_M | 4:6353f829c56c | 59 | _y[0] += 1; |
Andrew_M | 3:6253a2d374fa | 60 | } else if (_oldDirection == 'E') { |
Andrew_M | 4:6353f829c56c | 61 | _x[0] += 1; |
Andrew_M | 3:6253a2d374fa | 62 | } else if (_oldDirection == 'W') { |
Andrew_M | 4:6353f829c56c | 63 | _x[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 64 | } |
Andrew_M | 17:2a909f7da973 | 65 | printf("Snake hasn't changed direction\n"); |
Andrew_M | 7:c1e0593bfc99 | 66 | } |
Andrew_M | 2:9ca5e1c221c3 | 67 | } |
Andrew_M | 2:9ca5e1c221c3 | 68 | |
Andrew_M | 4:6353f829c56c | 69 | int Snek::getX(int ref) |
Andrew_M | 1:a14415de3ad5 | 70 | { |
Andrew_M | 4:6353f829c56c | 71 | return _x[ref]; |
Andrew_M | 2:9ca5e1c221c3 | 72 | } |
Andrew_M | 1:a14415de3ad5 | 73 | |
Andrew_M | 4:6353f829c56c | 74 | int Snek::getY(int ref) |
Andrew_M | 2:9ca5e1c221c3 | 75 | { |
Andrew_M | 4:6353f829c56c | 76 | return _y[ref]; |
Andrew_M | 2:9ca5e1c221c3 | 77 | } |
Andrew_M | 4:6353f829c56c | 78 | |
Andrew_M | 4:6353f829c56c | 79 | int Snek::getLength() |
Andrew_M | 4:6353f829c56c | 80 | { |
Andrew_M | 4:6353f829c56c | 81 | return _length; |
Andrew_M | 4:6353f829c56c | 82 | } |
Andrew_M | 5:a3a9e0417e04 | 83 | |
Andrew_M | 5:a3a9e0417e04 | 84 | void Snek::grow() |
Andrew_M | 5:a3a9e0417e04 | 85 | { |
Andrew_M | 15:130900e5c268 | 86 | _length+=1; //increments the length of the snake |
Andrew_M | 17:2a909f7da973 | 87 | printf("Snake has grown\n"); |
Andrew_M | 5:a3a9e0417e04 | 88 | } |
Andrew_M | 7:c1e0593bfc99 | 89 |