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@5:a3a9e0417e04, 2018-04-29 (annotated)
- Committer:
- Andrew_M
- Date:
- Sun Apr 29 18:03:45 2018 +0000
- Revision:
- 5:a3a9e0417e04
- Parent:
- 4:6353f829c56c
- Child:
- 7:c1e0593bfc99
Food class is now a thing, the snake grows when it eats and a game over state is reached when the snake eats it's own tail with a simple animation to demonstrate the game over state has been reached
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 | 1:a14415de3ad5 | 16 | //Inital values for variables |
Andrew_M | 4:6353f829c56c | 17 | _length = 3; |
Andrew_M | 5:a3a9e0417e04 | 18 | |
Andrew_M | 5:a3a9e0417e04 | 19 | _oldDirection = 'N'; |
Andrew_M | 4:6353f829c56c | 20 | |
Andrew_M | 4:6353f829c56c | 21 | for (int i = 0; i < _length; i++) { |
Andrew_M | 4:6353f829c56c | 22 | _x[i] = x; |
Andrew_M | 5:a3a9e0417e04 | 23 | _y[i] = y + i; |
Andrew_M | 4:6353f829c56c | 24 | } |
Andrew_M | 1:a14415de3ad5 | 25 | } |
Andrew_M | 1:a14415de3ad5 | 26 | |
Andrew_M | 2:9ca5e1c221c3 | 27 | void Snek::update(Direction d) |
Andrew_M | 2:9ca5e1c221c3 | 28 | { |
Andrew_M | 5:a3a9e0417e04 | 29 | for (int i = _length-1; i >= 1; i--) { |
Andrew_M | 5:a3a9e0417e04 | 30 | _x[i] = _x[i-1]; |
Andrew_M | 5:a3a9e0417e04 | 31 | _y[i] = _y[i-1]; |
Andrew_M | 5:a3a9e0417e04 | 32 | } |
Andrew_M | 5:a3a9e0417e04 | 33 | |
Andrew_M | 5:a3a9e0417e04 | 34 | if (d == N && _oldDirection != 'S') { //checks the current direction so the snake cannot go back on itself |
Andrew_M | 4:6353f829c56c | 35 | _y[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 36 | _oldDirection = 'N'; |
Andrew_M | 5:a3a9e0417e04 | 37 | } else if (d == S && _oldDirection != 'N') { |
Andrew_M | 4:6353f829c56c | 38 | _y[0] += 1; |
Andrew_M | 3:6253a2d374fa | 39 | _oldDirection = 'S'; |
Andrew_M | 5:a3a9e0417e04 | 40 | } else if (d == E && _oldDirection != 'W') { |
Andrew_M | 4:6353f829c56c | 41 | _x[0] += 1; |
Andrew_M | 3:6253a2d374fa | 42 | _oldDirection = 'E'; |
Andrew_M | 5:a3a9e0417e04 | 43 | } else if (d == W && _oldDirection != 'E') { |
Andrew_M | 4:6353f829c56c | 44 | _x[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 45 | _oldDirection = 'W'; |
Andrew_M | 3:6253a2d374fa | 46 | } else { |
Andrew_M | 3:6253a2d374fa | 47 | if (_oldDirection == 'N') { |
Andrew_M | 4:6353f829c56c | 48 | _y[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 49 | } else if (_oldDirection == 'S') { |
Andrew_M | 4:6353f829c56c | 50 | _y[0] += 1; |
Andrew_M | 3:6253a2d374fa | 51 | } else if (_oldDirection == 'E') { |
Andrew_M | 4:6353f829c56c | 52 | _x[0] += 1; |
Andrew_M | 3:6253a2d374fa | 53 | } else if (_oldDirection == 'W') { |
Andrew_M | 4:6353f829c56c | 54 | _x[0] -= 1; |
Andrew_M | 3:6253a2d374fa | 55 | } |
Andrew_M | 5:a3a9e0417e04 | 56 | } |
Andrew_M | 2:9ca5e1c221c3 | 57 | } |
Andrew_M | 2:9ca5e1c221c3 | 58 | |
Andrew_M | 4:6353f829c56c | 59 | int Snek::getX(int ref) |
Andrew_M | 1:a14415de3ad5 | 60 | { |
Andrew_M | 4:6353f829c56c | 61 | return _x[ref]; |
Andrew_M | 2:9ca5e1c221c3 | 62 | } |
Andrew_M | 1:a14415de3ad5 | 63 | |
Andrew_M | 4:6353f829c56c | 64 | int Snek::getY(int ref) |
Andrew_M | 2:9ca5e1c221c3 | 65 | { |
Andrew_M | 4:6353f829c56c | 66 | return _y[ref]; |
Andrew_M | 2:9ca5e1c221c3 | 67 | } |
Andrew_M | 4:6353f829c56c | 68 | |
Andrew_M | 4:6353f829c56c | 69 | int Snek::getLength() |
Andrew_M | 4:6353f829c56c | 70 | { |
Andrew_M | 4:6353f829c56c | 71 | return _length; |
Andrew_M | 4:6353f829c56c | 72 | } |
Andrew_M | 5:a3a9e0417e04 | 73 | |
Andrew_M | 5:a3a9e0417e04 | 74 | void Snek::grow() |
Andrew_M | 5:a3a9e0417e04 | 75 | { |
Andrew_M | 5:a3a9e0417e04 | 76 | _length+=1; |
Andrew_M | 5:a3a9e0417e04 | 77 | } |