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
Diff: SnakeBody/SnakeBody.cpp
- Revision:
- 4:ea3fa51c4386
- Parent:
- 3:fcd6d70e9694
- Child:
- 5:06fa7674622a
--- a/SnakeBody/SnakeBody.cpp Wed May 20 21:25:40 2020 +0000 +++ b/SnakeBody/SnakeBody.cpp Sat May 23 15:31:30 2020 +0000 @@ -1,34 +1,110 @@ #include "SnakeBody.h" -// nothing doing in the constructor and destructor SnakeBody::SnakeBody() { -//constructor - Direction _fsm[5] = { - {0, 0, 0, {0,1,2,3,4}}, // Centred - {1, 0, -1, {1,1,2,1,4}}, // North, will not go to centre or south - {2, 1, 0, {2,1,2,3,2}}, // East, will not go to centre or west - {3, 0, 1, {3,3,2,3,4}}, // South, will not go to centre or north - {4, -1, 0, {4,1,4,3,4}} // West, will not go to centre or east - }; + _x_head = 42; + _y_head = 24; + _length = 40; + _angle = -1.0; + _state = 0; + _d = 0; + _body_x.clear(); + _body_x.push_back(42); + _body_y.clear(); + _body_y.push_back(24); } SnakeBody::~SnakeBody() { -//deconstructor +//destructor +} + +void init(){ + } -void update_movement(int x_max, int y_max, int cell_size, int d, int _state) { - /* - if ((_x_head % cell_size) + (_y_head % cell_size) == 0) { // only allows changing movement when the snake is cell-aligned - d = pad.get_cardinal(); // gets joystick cardinal direction: 0 for centre, 1 - 4 for NESW respectively - state = _fsm[_state].nextState[d]; // adjusts direction fsm state based on direction +int SnakeBody::update_direction(float _angle, int _x_head, int _y_head) { + + if ((_x_head % 2) + (_y_head % 2) == 0) { // only allows changing movement when the snake is cell-aligned (ie x and y are even) + // partition 360 into segments and check which segment the angle is in + if (_angle < 0.0f) { //check for -1.0 angle + _d = 0; + } else if (_angle < 45.0f) { //North quadrant + _d = 1; + } else if (_angle < 135.0f) { //East quadrant + _d = 2; + } else if (_angle < 225.0f) { //South quadrant + _d = 3; + } else if (_angle < 315.0f) { //West quadrant + _d = 4; + } else { //NW half - quadrant + _d = 1; + } } + //printf("D: %d\n", _d); + return _d; //returns quadrant, represented by direction integer +}; + +void SnakeBody::update_position() { + + Direction _fsm[5] = { + {0, 0, {0,1,2,3,4}}, // Centred + {0, -1, {1,1,2,1,4}}, // North, will not go to centre or south + {1, 0, {2,1,2,3,2}}, // East, will not go to centre or west + {0, 1, {3,3,2,3,4}}, // South, will not go to centre or north + {-1, 0, {4,1,4,3,4}} // West, will not go to centre or east + }; + + _state = _fsm[_state].nextState[_d]; // adjusts direction fsm state based on direction + //printf("State: %d\n", _state); _x_head += _fsm[_state].delta_x; // increments x value based on fsm state value _y_head += _fsm[_state].delta_y; // increments y value based on fsm state value - _x_head = ((x_head % X_MAX) + X_MAX)% X_MAX; // wraps x back to within range 0-83 - _y_head = ((y_head % Y_MAX) + Y_MAX)% Y_MAX; // wraps y back to within range 0-83 - */ -}; \ No newline at end of file + _x_head = ((_x_head % 84) + 84) % 84; // wraps x back to within range 0-83 + _y_head = ((_y_head % 48) + 48) % 48; // wraps y back to within range 0-47 + + //printf("x_head: %d\n", _x_head); + //printf("y_head: %d\n", _y_head); +}; + +void SnakeBody::snake_movement(Gamepad &pad) { + + float _angle = pad.get_angle(); //finds joystick angle + //printf("Joystick angle: %f\n", _angle); + _d = update_direction(_angle, _x_head, _y_head); //converts angle into a integer direction + update_position(); //takes integer direction and feeds it into FSM to move snake in correct direction + update_body(); +} + +void SnakeBody::update_body() { + + _body_x.insert(_body_x.begin(), _x_head); //sets first array element to head coordinates + _body_y.insert(_body_y.begin(), _y_head); + + _body_x.erase(_body_x.begin() + _length, _body_x.end()); + _body_y.erase(_body_y.begin() + _length, _body_y.end()); +} + +void SnakeBody::draw_body(N5110 &lcd){ + + for(int i = 0; i < _length; i++) { + lcd.drawRect(_body_x[i],_body_y[i],2,2,FILL_BLACK); + } +} + +void SnakeBody::snake_snake_collision(Gamepad &pad, bool &death){ + for(int i = 1; i < _length; i++) { + if (_x_head == _body_x[i] && _y_head == _body_y[i]) { //checks if head coord is the same as any of the body coords + pad.led(1,0.9); + //printf("DEAD \n"); + _state = 0; + death = true; + //return true; + } else { + //pad.led(1,0.0); + //printf("ALIVE \n"); + //return false; + } + } +} \ No newline at end of file