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.
Dependencies: mbed ll16j23s_test_docs
SnakeBody/SnakeBody.cpp@3:fcd6d70e9694, 2020-05-20 (annotated)
- Committer:
- JoeShotton
- Date:
- Wed May 20 21:25:40 2020 +0000
- Revision:
- 3:fcd6d70e9694
- Child:
- 4:ea3fa51c4386
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JoeShotton | 3:fcd6d70e9694 | 1 | #include "SnakeBody.h" |
JoeShotton | 3:fcd6d70e9694 | 2 | |
JoeShotton | 3:fcd6d70e9694 | 3 | // nothing doing in the constructor and destructor |
JoeShotton | 3:fcd6d70e9694 | 4 | SnakeBody::SnakeBody() |
JoeShotton | 3:fcd6d70e9694 | 5 | { |
JoeShotton | 3:fcd6d70e9694 | 6 | //constructor |
JoeShotton | 3:fcd6d70e9694 | 7 | Direction _fsm[5] = { |
JoeShotton | 3:fcd6d70e9694 | 8 | {0, 0, 0, {0,1,2,3,4}}, // Centred |
JoeShotton | 3:fcd6d70e9694 | 9 | {1, 0, -1, {1,1,2,1,4}}, // North, will not go to centre or south |
JoeShotton | 3:fcd6d70e9694 | 10 | {2, 1, 0, {2,1,2,3,2}}, // East, will not go to centre or west |
JoeShotton | 3:fcd6d70e9694 | 11 | {3, 0, 1, {3,3,2,3,4}}, // South, will not go to centre or north |
JoeShotton | 3:fcd6d70e9694 | 12 | {4, -1, 0, {4,1,4,3,4}} // West, will not go to centre or east |
JoeShotton | 3:fcd6d70e9694 | 13 | }; |
JoeShotton | 3:fcd6d70e9694 | 14 | } |
JoeShotton | 3:fcd6d70e9694 | 15 | |
JoeShotton | 3:fcd6d70e9694 | 16 | SnakeBody::~SnakeBody() |
JoeShotton | 3:fcd6d70e9694 | 17 | { |
JoeShotton | 3:fcd6d70e9694 | 18 | //deconstructor |
JoeShotton | 3:fcd6d70e9694 | 19 | } |
JoeShotton | 3:fcd6d70e9694 | 20 | |
JoeShotton | 3:fcd6d70e9694 | 21 | void update_movement(int x_max, int y_max, int cell_size, int d, int _state) { |
JoeShotton | 3:fcd6d70e9694 | 22 | /* |
JoeShotton | 3:fcd6d70e9694 | 23 | if ((_x_head % cell_size) + (_y_head % cell_size) == 0) { // only allows changing movement when the snake is cell-aligned |
JoeShotton | 3:fcd6d70e9694 | 24 | d = pad.get_cardinal(); // gets joystick cardinal direction: 0 for centre, 1 - 4 for NESW respectively |
JoeShotton | 3:fcd6d70e9694 | 25 | state = _fsm[_state].nextState[d]; // adjusts direction fsm state based on direction |
JoeShotton | 3:fcd6d70e9694 | 26 | } |
JoeShotton | 3:fcd6d70e9694 | 27 | |
JoeShotton | 3:fcd6d70e9694 | 28 | _x_head += _fsm[_state].delta_x; // increments x value based on fsm state value |
JoeShotton | 3:fcd6d70e9694 | 29 | _y_head += _fsm[_state].delta_y; // increments y value based on fsm state value |
JoeShotton | 3:fcd6d70e9694 | 30 | |
JoeShotton | 3:fcd6d70e9694 | 31 | _x_head = ((x_head % X_MAX) + X_MAX)% X_MAX; // wraps x back to within range 0-83 |
JoeShotton | 3:fcd6d70e9694 | 32 | _y_head = ((y_head % Y_MAX) + Y_MAX)% Y_MAX; // wraps y back to within range 0-83 |
JoeShotton | 3:fcd6d70e9694 | 33 | */ |
JoeShotton | 3:fcd6d70e9694 | 34 | }; |