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@15:130900e5c268, 2018-05-08 (annotated)
- Committer:
- Andrew_M
- Date:
- Tue May 08 14:06:47 2018 +0000
- Revision:
- 15:130900e5c268
- Parent:
- 12:d3eef5ea3f43
- Child:
- 17:2a909f7da973
Added a large amount of explanatory comments
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 | 12:d3eef5ea3f43 | 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 | 15:130900e5c268 | 26 | _y[i] = y + i; //the Y coordinate is incremmented for each tail segment |
Andrew_M | 4:6353f829c56c | 27 | } |
Andrew_M | 1:a14415de3ad5 | 28 | } |
Andrew_M | 1:a14415de3ad5 | 29 | |
Andrew_M | 2:9ca5e1c221c3 | 30 | void Snek::update(Direction d) |
Andrew_M | 2:9ca5e1c221c3 | 31 | { |
Andrew_M | 5:a3a9e0417e04 | 32 | for (int i = _length-1; i >= 1; i--) { |
Andrew_M | 7:c1e0593bfc99 | 33 | _x[i] = _x[i-1]; |
Andrew_M | 7:c1e0593bfc99 | 34 | _y[i] = _y[i-1]; |
Andrew_M | 5:a3a9e0417e04 | 35 | } |
Andrew_M | 7:c1e0593bfc99 | 36 | |
Andrew_M | 15:130900e5c268 | 37 | if (d == N && _oldDirection != 'S') { //checks the new direction so the snake cannot go back on itself |
Andrew_M | 15:130900e5c268 | 38 | _y[0] -= 1; //changes a coordinate of the snake's head |
Andrew_M | 15:130900e5c268 | 39 | _oldDirection = 'N'; //updates the _oldDirection so the snake will travel in the new direction with no play input |
Andrew_M | 5:a3a9e0417e04 | 40 | } else if (d == S && _oldDirection != 'N') { |
Andrew_M | 4:6353f829c56c | 41 | _y[0] += 1; |
Andrew_M | 3:6253a2d374fa | 42 | _oldDirection = 'S'; |
Andrew_M | 5:a3a9e0417e04 | 43 | } else if (d == E && _oldDirection != 'W') { |
Andrew_M | 4:6353f829c56c | 44 | _x[0] += 1; |
Andrew_M | 3:6253a2d374fa | 45 | _oldDirection = 'E'; |
Andrew_M | 5:a3a9e0417e04 | 46 | } else if (d == W && _oldDirection != 'E') { |
Andrew_M | 4:6353f829c56c | 47 | _x[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 48 | _oldDirection = 'W'; |
Andrew_M | 15:130900e5c268 | 49 | } else { //if there is no player input (new direction)... |
Andrew_M | 15:130900e5c268 | 50 | if (_oldDirection == 'N') { //checks the old direction... |
Andrew_M | 15:130900e5c268 | 51 | _y[0] -= 1; //and changes a coordinate of the snake's head depending on the _oldDirection |
Andrew_M | 3:6253a2d374fa | 52 | } else if (_oldDirection == 'S') { |
Andrew_M | 4:6353f829c56c | 53 | _y[0] += 1; |
Andrew_M | 3:6253a2d374fa | 54 | } else if (_oldDirection == 'E') { |
Andrew_M | 4:6353f829c56c | 55 | _x[0] += 1; |
Andrew_M | 3:6253a2d374fa | 56 | } else if (_oldDirection == 'W') { |
Andrew_M | 4:6353f829c56c | 57 | _x[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 58 | } |
Andrew_M | 7:c1e0593bfc99 | 59 | } |
Andrew_M | 2:9ca5e1c221c3 | 60 | } |
Andrew_M | 2:9ca5e1c221c3 | 61 | |
Andrew_M | 4:6353f829c56c | 62 | int Snek::getX(int ref) |
Andrew_M | 1:a14415de3ad5 | 63 | { |
Andrew_M | 4:6353f829c56c | 64 | return _x[ref]; |
Andrew_M | 2:9ca5e1c221c3 | 65 | } |
Andrew_M | 1:a14415de3ad5 | 66 | |
Andrew_M | 4:6353f829c56c | 67 | int Snek::getY(int ref) |
Andrew_M | 2:9ca5e1c221c3 | 68 | { |
Andrew_M | 4:6353f829c56c | 69 | return _y[ref]; |
Andrew_M | 2:9ca5e1c221c3 | 70 | } |
Andrew_M | 4:6353f829c56c | 71 | |
Andrew_M | 4:6353f829c56c | 72 | int Snek::getLength() |
Andrew_M | 4:6353f829c56c | 73 | { |
Andrew_M | 4:6353f829c56c | 74 | return _length; |
Andrew_M | 4:6353f829c56c | 75 | } |
Andrew_M | 5:a3a9e0417e04 | 76 | |
Andrew_M | 5:a3a9e0417e04 | 77 | void Snek::grow() |
Andrew_M | 5:a3a9e0417e04 | 78 | { |
Andrew_M | 15:130900e5c268 | 79 | _length+=1; //increments the length of the snake |
Andrew_M | 5:a3a9e0417e04 | 80 | } |
Andrew_M | 7:c1e0593bfc99 | 81 |