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:
- 13:7b7ec5db56b2
- Parent:
- 10:a2d643b3c782
- Child:
- 14:2dfe04ced21c
--- a/SnakeBody/SnakeBody.cpp Tue May 26 23:57:47 2020 +0000 +++ b/SnakeBody/SnakeBody.cpp Wed May 27 00:59:14 2020 +0000 @@ -2,8 +2,8 @@ SnakeBody::SnakeBody() { - _x_head = 42; - _y_head = 24; + head_x = 42; + head_y = 24; _length = 4; _length_increase = 4; _angle = -1.0; @@ -18,16 +18,16 @@ void SnakeBody::init() { - _body_x.clear(); - _body_x.push_back(42); - _body_y.clear(); - _body_y.push_back(24); + body_x.clear(); + body_x.push_back(42); + body_y.clear(); + body_y.push_back(24); } void SnakeBody::update_direction() { - if ((_x_head % 2) + (_y_head % 2) == 0) { // only allows changing movement when the snake is cell-aligned (ie x and y are even) + if ((head_x % 2) + (head_y % 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 printf("D: %d\n", _d); if (_angle < 0.0f) { //check for -1.0 angle @@ -60,14 +60,14 @@ _move_state = _fsm[_move_state].nextState[_d]; // adjusts fsm state based on direction //printf("State: %d\n", _move_state); - _x_head += _fsm[_move_state].delta_x; // increments x value based on fsm state value - _y_head += _fsm[_move_state].delta_y; // increments y value based on fsm state value + head_x += _fsm[_move_state].delta_x; // increments x value based on fsm state value + head_y += _fsm[_move_state].delta_y; // increments y value based on fsm state value - _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 + head_x = ((head_x % 84) + 84) % 84; // wraps x back to within range 0-83 + head_y = ((head_y % 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); + //printf("head_x: %d\n", head_x); + //printf("head_y: %d\n", head_y); }; void SnakeBody::snake_movement(Gamepad &pad) @@ -82,12 +82,12 @@ void SnakeBody::update_body() { - if ((_x_head % 2) + (_y_head % 2) == 0) { //only updates body when cell aligned - _body_x.insert(_body_x.begin(), _x_head); //sets first array element to head coordinates - _body_y.insert(_body_y.begin(), _y_head); + if ((head_x % 2) + (head_y % 2) == 0) { //only updates body when cell aligned + body_x.insert(body_x.begin(), head_x); //sets first array element to head coordinates + body_y.insert(body_y.begin(), head_y); - _body_x.erase(_body_x.begin() + _length, _body_x.end()); //erases all elements from position after tail (ie [_length]) - _body_y.erase(_body_y.begin() + _length, _body_y.end()); //up to end of vector + body_x.erase(body_x.begin() + _length, body_x.end()); //erases all elements from position after tail (ie [_length]) + body_y.erase(body_y.begin() + _length, body_y.end()); //up to end of vector } if(_length_increase > 0) { //converts length increase into length, one unit at a time _length++; @@ -98,9 +98,9 @@ void SnakeBody::draw_body(N5110 &lcd) { - lcd.drawRect(_x_head,_y_head,2,2,FILL_BLACK); //draws square at head (so new head coords displayed even when not cell-aligned) + lcd.drawRect(head_x, head_y, 2, 2, FILL_BLACK); //draws square at head (so new head coords displayed even when not cell-aligned) for(int i = 0; i < _length - 3; i++) { //iterates across vector to draw sqaures at every position - lcd.drawRect(_body_x[i],_body_y[i],2,2,FILL_BLACK); + lcd.drawRect(body_x[i], body_y[i], 2, 2, FILL_BLACK); } } @@ -108,7 +108,7 @@ { if (_move_state > 0) { //if body has started to move for(int i = 3; i < _length - 3; i++) { //only checks from 3rd cell onwards since head can't collide with very start of body - if (_x_head == _body_x[i] && _y_head == _body_y[i]) { //checks if head coord is the same as any of the body coords + if (head_x == body_x[i] && head_y == body_y[i]) { //checks if head coord is the same as any of the body coords //printf("S-S Collison \n"); _move_state = 0; _death = true; @@ -131,10 +131,10 @@ void SnakeBody::reset() { - _x_head = 42; - _y_head = 24; - for(int i = 3; i < _body_y.size(); i++) { - _body_y.at(i) = 50; + head_x = 42; + head_y = 24; + for(int i = 3; i < body_y.size(); i++) { + body_y.at(i) = 50; //moves all body coordinates off-screen } _length = 4; _length_increase = 4;