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:
- 8:bcc3403d7e79
- Parent:
- 7:dd84e0fab346
- Child:
- 9:0571880085cc
diff -r dd84e0fab346 -r bcc3403d7e79 SnakeBody/SnakeBody.cpp --- a/SnakeBody/SnakeBody.cpp Sun May 24 22:18:55 2020 +0000 +++ b/SnakeBody/SnakeBody.cpp Mon May 25 14:45:32 2020 +0000 @@ -4,7 +4,7 @@ { _x_head = 42; _y_head = 24; - _length = 10; + _length = 4; _angle = -1.0; _state = 0; _d = 0; @@ -78,31 +78,37 @@ 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); + 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); - _body_x.erase(_body_x.begin() + _length, _body_x.end()); - _body_y.erase(_body_y.begin() + _length, _body_y.end()); + _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){ + lcd.drawRect(_x_head,_y_head,2,2,FILL_BLACK); 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){ - 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; - //return true; - } else { - //pad.led(1,0.0); - //printf("ALIVE \n"); - //return false; +bool SnakeBody::snake_snake_collision(Gamepad &pad){ + if (_state > 0) { + for(int i = 3; 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; + return true; + } else { + //pad.led(1,0.0); + //printf("ALIVE \n"); + //return false; + } } } } \ No newline at end of file