testing documentation
Dependencies: mbed ll16j23s_test_docs
SnakeBody/SnakeBody.cpp@9:0571880085cc, 2020-05-25 (annotated)
- Committer:
- JoeShotton
- Date:
- Mon May 25 20:31:52 2020 +0000
- Revision:
- 9:0571880085cc
- Parent:
- 8:bcc3403d7e79
- Child:
- 10:a2d643b3c782
Post death menus partially implemented, mostly fixed food location issues and issues with tail display
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 | SnakeBody::SnakeBody() |
JoeShotton | 3:fcd6d70e9694 | 4 | { |
JoeShotton | 4:ea3fa51c4386 | 5 | _x_head = 42; |
JoeShotton | 4:ea3fa51c4386 | 6 | _y_head = 24; |
JoeShotton | 8:bcc3403d7e79 | 7 | _length = 4; |
JoeShotton | 4:ea3fa51c4386 | 8 | _angle = -1.0; |
JoeShotton | 4:ea3fa51c4386 | 9 | _state = 0; |
JoeShotton | 4:ea3fa51c4386 | 10 | _d = 0; |
JoeShotton | 3:fcd6d70e9694 | 11 | } |
JoeShotton | 3:fcd6d70e9694 | 12 | |
JoeShotton | 9:0571880085cc | 13 | SnakeBody::~SnakeBody() { |
JoeShotton | 4:ea3fa51c4386 | 14 | //destructor |
JoeShotton | 4:ea3fa51c4386 | 15 | } |
JoeShotton | 4:ea3fa51c4386 | 16 | |
JoeShotton | 6:6c9453397f4a | 17 | void SnakeBody::init() { |
JoeShotton | 6:6c9453397f4a | 18 | _body_x.clear(); |
JoeShotton | 6:6c9453397f4a | 19 | _body_x.push_back(42); |
JoeShotton | 6:6c9453397f4a | 20 | _body_y.clear(); |
JoeShotton | 6:6c9453397f4a | 21 | _body_y.push_back(24); |
JoeShotton | 3:fcd6d70e9694 | 22 | } |
JoeShotton | 3:fcd6d70e9694 | 23 | |
JoeShotton | 4:ea3fa51c4386 | 24 | int SnakeBody::update_direction(float _angle, int _x_head, int _y_head) { |
JoeShotton | 4:ea3fa51c4386 | 25 | |
JoeShotton | 4:ea3fa51c4386 | 26 | if ((_x_head % 2) + (_y_head % 2) == 0) { // only allows changing movement when the snake is cell-aligned (ie x and y are even) |
JoeShotton | 4:ea3fa51c4386 | 27 | // partition 360 into segments and check which segment the angle is in |
JoeShotton | 4:ea3fa51c4386 | 28 | if (_angle < 0.0f) { //check for -1.0 angle |
JoeShotton | 4:ea3fa51c4386 | 29 | _d = 0; |
JoeShotton | 4:ea3fa51c4386 | 30 | } else if (_angle < 45.0f) { //North quadrant |
JoeShotton | 4:ea3fa51c4386 | 31 | _d = 1; |
JoeShotton | 4:ea3fa51c4386 | 32 | } else if (_angle < 135.0f) { //East quadrant |
JoeShotton | 4:ea3fa51c4386 | 33 | _d = 2; |
JoeShotton | 4:ea3fa51c4386 | 34 | } else if (_angle < 225.0f) { //South quadrant |
JoeShotton | 4:ea3fa51c4386 | 35 | _d = 3; |
JoeShotton | 4:ea3fa51c4386 | 36 | } else if (_angle < 315.0f) { //West quadrant |
JoeShotton | 4:ea3fa51c4386 | 37 | _d = 4; |
JoeShotton | 4:ea3fa51c4386 | 38 | } else { //NW half - quadrant |
JoeShotton | 4:ea3fa51c4386 | 39 | _d = 1; |
JoeShotton | 4:ea3fa51c4386 | 40 | } |
JoeShotton | 3:fcd6d70e9694 | 41 | } |
JoeShotton | 4:ea3fa51c4386 | 42 | //printf("D: %d\n", _d); |
JoeShotton | 4:ea3fa51c4386 | 43 | return _d; //returns quadrant, represented by direction integer |
JoeShotton | 4:ea3fa51c4386 | 44 | }; |
JoeShotton | 4:ea3fa51c4386 | 45 | |
JoeShotton | 4:ea3fa51c4386 | 46 | void SnakeBody::update_position() { |
JoeShotton | 4:ea3fa51c4386 | 47 | |
JoeShotton | 4:ea3fa51c4386 | 48 | Direction _fsm[5] = { |
JoeShotton | 4:ea3fa51c4386 | 49 | {0, 0, {0,1,2,3,4}}, // Centred |
JoeShotton | 4:ea3fa51c4386 | 50 | {0, -1, {1,1,2,1,4}}, // North, will not go to centre or south |
JoeShotton | 4:ea3fa51c4386 | 51 | {1, 0, {2,1,2,3,2}}, // East, will not go to centre or west |
JoeShotton | 4:ea3fa51c4386 | 52 | {0, 1, {3,3,2,3,4}}, // South, will not go to centre or north |
JoeShotton | 4:ea3fa51c4386 | 53 | {-1, 0, {4,1,4,3,4}} // West, will not go to centre or east |
JoeShotton | 4:ea3fa51c4386 | 54 | }; |
JoeShotton | 4:ea3fa51c4386 | 55 | |
JoeShotton | 4:ea3fa51c4386 | 56 | _state = _fsm[_state].nextState[_d]; // adjusts direction fsm state based on direction |
JoeShotton | 4:ea3fa51c4386 | 57 | //printf("State: %d\n", _state); |
JoeShotton | 3:fcd6d70e9694 | 58 | |
JoeShotton | 3:fcd6d70e9694 | 59 | _x_head += _fsm[_state].delta_x; // increments x value based on fsm state value |
JoeShotton | 3:fcd6d70e9694 | 60 | _y_head += _fsm[_state].delta_y; // increments y value based on fsm state value |
JoeShotton | 3:fcd6d70e9694 | 61 | |
JoeShotton | 4:ea3fa51c4386 | 62 | _x_head = ((_x_head % 84) + 84) % 84; // wraps x back to within range 0-83 |
JoeShotton | 4:ea3fa51c4386 | 63 | _y_head = ((_y_head % 48) + 48) % 48; // wraps y back to within range 0-47 |
JoeShotton | 4:ea3fa51c4386 | 64 | |
JoeShotton | 4:ea3fa51c4386 | 65 | //printf("x_head: %d\n", _x_head); |
JoeShotton | 4:ea3fa51c4386 | 66 | //printf("y_head: %d\n", _y_head); |
JoeShotton | 4:ea3fa51c4386 | 67 | }; |
JoeShotton | 4:ea3fa51c4386 | 68 | |
JoeShotton | 4:ea3fa51c4386 | 69 | void SnakeBody::snake_movement(Gamepad &pad) { |
JoeShotton | 4:ea3fa51c4386 | 70 | |
JoeShotton | 4:ea3fa51c4386 | 71 | float _angle = pad.get_angle(); //finds joystick angle |
JoeShotton | 4:ea3fa51c4386 | 72 | //printf("Joystick angle: %f\n", _angle); |
JoeShotton | 4:ea3fa51c4386 | 73 | _d = update_direction(_angle, _x_head, _y_head); //converts angle into a integer direction |
JoeShotton | 4:ea3fa51c4386 | 74 | update_position(); //takes integer direction and feeds it into FSM to move snake in correct direction |
JoeShotton | 4:ea3fa51c4386 | 75 | update_body(); |
JoeShotton | 4:ea3fa51c4386 | 76 | } |
JoeShotton | 4:ea3fa51c4386 | 77 | |
JoeShotton | 4:ea3fa51c4386 | 78 | void SnakeBody::update_body() { |
JoeShotton | 4:ea3fa51c4386 | 79 | |
JoeShotton | 8:bcc3403d7e79 | 80 | if ((_x_head % 2) + (_y_head % 2) == 0) { //only updates body when cell aligned |
JoeShotton | 8:bcc3403d7e79 | 81 | _body_x.insert(_body_x.begin(), _x_head); //sets first array element to head coordinates |
JoeShotton | 8:bcc3403d7e79 | 82 | _body_y.insert(_body_y.begin(), _y_head); |
JoeShotton | 4:ea3fa51c4386 | 83 | |
JoeShotton | 8:bcc3403d7e79 | 84 | _body_x.erase(_body_x.begin() + _length, _body_x.end()); |
JoeShotton | 8:bcc3403d7e79 | 85 | _body_y.erase(_body_y.begin() + _length, _body_y.end()); |
JoeShotton | 8:bcc3403d7e79 | 86 | } |
JoeShotton | 9:0571880085cc | 87 | if(_length_increase > 0){ |
JoeShotton | 9:0571880085cc | 88 | _length++; |
JoeShotton | 9:0571880085cc | 89 | _length_increase--; |
JoeShotton | 9:0571880085cc | 90 | //_body_y.at(_length + 1) = 0; |
JoeShotton | 9:0571880085cc | 91 | } |
JoeShotton | 4:ea3fa51c4386 | 92 | } |
JoeShotton | 4:ea3fa51c4386 | 93 | |
JoeShotton | 8:bcc3403d7e79 | 94 | |
JoeShotton | 4:ea3fa51c4386 | 95 | void SnakeBody::draw_body(N5110 &lcd){ |
JoeShotton | 4:ea3fa51c4386 | 96 | |
JoeShotton | 8:bcc3403d7e79 | 97 | lcd.drawRect(_x_head,_y_head,2,2,FILL_BLACK); |
JoeShotton | 9:0571880085cc | 98 | for(int i = 0; i < _length - 3; i++) { |
JoeShotton | 4:ea3fa51c4386 | 99 | lcd.drawRect(_body_x[i],_body_y[i],2,2,FILL_BLACK); |
JoeShotton | 4:ea3fa51c4386 | 100 | } |
JoeShotton | 4:ea3fa51c4386 | 101 | } |
JoeShotton | 4:ea3fa51c4386 | 102 | |
JoeShotton | 9:0571880085cc | 103 | void SnakeBody::snake_snake_collision(Gamepad &pad, bool &_death){ |
JoeShotton | 9:0571880085cc | 104 | if (_state > 0) { |
JoeShotton | 9:0571880085cc | 105 | for(int i = 3; i < _length - 3; i++) { |
JoeShotton | 8:bcc3403d7e79 | 106 | if (_x_head == _body_x[i] && _y_head == _body_y[i]) { //checks if head coord is the same as any of the body coords |
JoeShotton | 9:0571880085cc | 107 | printf("DEAD \n"); |
JoeShotton | 8:bcc3403d7e79 | 108 | _state = 0; |
JoeShotton | 9:0571880085cc | 109 | _death = true; |
JoeShotton | 8:bcc3403d7e79 | 110 | } |
JoeShotton | 4:ea3fa51c4386 | 111 | } |
JoeShotton | 4:ea3fa51c4386 | 112 | } |
JoeShotton | 9:0571880085cc | 113 | } |
JoeShotton | 9:0571880085cc | 114 | |
JoeShotton | 9:0571880085cc | 115 | void SnakeBody::add_length(int increase){ |
JoeShotton | 9:0571880085cc | 116 | _length_increase += increase; |
JoeShotton | 9:0571880085cc | 117 | } |
JoeShotton | 9:0571880085cc | 118 | |
JoeShotton | 9:0571880085cc | 119 | void SnakeBody::run(Gamepad &pad, N5110 &lcd, bool &_death){ |
JoeShotton | 9:0571880085cc | 120 | snake_movement(pad); |
JoeShotton | 9:0571880085cc | 121 | draw_body(lcd); |
JoeShotton | 9:0571880085cc | 122 | snake_snake_collision(pad, _death); |
JoeShotton | 4:ea3fa51c4386 | 123 | } |