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:
- 15:130900e5c268
- Parent:
- 12:d3eef5ea3f43
- Child:
- 17:2a909f7da973
--- a/Snek/Snek.cpp Tue May 08 13:15:41 2018 +0000 +++ b/Snek/Snek.cpp Tue May 08 14:06:47 2018 +0000 @@ -13,17 +13,17 @@ void Snek::init(int x, int y) { - memset(_x, 0, sizeof(_x)); - memset(_y, 0, sizeof(_y)); + memset(_x, 0, sizeof(_x)); //clears the x coordinate array + memset(_y, 0, sizeof(_y)); //clears the y coordinate array //Inital values for variables _length = 3; _oldDirection = 'N'; - for (int i = 0; i < _length; i++) { - _x[i] = x; - _y[i] = y + i; + for (int i = 0; i < _length; i++) { //sets the starting values for the snake coordinates + _x[i] = x; //all have the same X coordinate + _y[i] = y + i; //the Y coordinate is incremmented for each tail segment } } @@ -34,9 +34,9 @@ _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'; + if (d == N && _oldDirection != 'S') { //checks the new direction so the snake cannot go back on itself + _y[0] -= 1; //changes a coordinate of the snake's head + _oldDirection = 'N'; //updates the _oldDirection so the snake will travel in the new direction with no play input } else if (d == S && _oldDirection != 'N') { _y[0] += 1; _oldDirection = 'S'; @@ -46,9 +46,9 @@ } else if (d == W && _oldDirection != 'E') { _x[0] -= 1; _oldDirection = 'W'; - } else { - if (_oldDirection == 'N') { - _y[0] -= 1; + } else { //if there is no player input (new direction)... + if (_oldDirection == 'N') { //checks the old direction... + _y[0] -= 1; //and changes a coordinate of the snake's head depending on the _oldDirection } else if (_oldDirection == 'S') { _y[0] += 1; } else if (_oldDirection == 'E') { @@ -76,6 +76,6 @@ void Snek::grow() { - _length+=1; + _length+=1; //increments the length of the snake }