testing documentation
Dependencies: mbed ll16j23s_test_docs
SnakeBody/SnakeBody.cpp@6:6c9453397f4a, 2020-05-23 (annotated)
- Committer:
- JoeShotton
- Date:
- Sat May 23 20:01:00 2020 +0000
- Revision:
- 6:6c9453397f4a
- Parent:
- 5:06fa7674622a
- Child:
- 7:dd84e0fab346
Implemented screen transition function
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 | 5:06fa7674622a | 7 | _length = 10; |
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 | 3:fcd6d70e9694 | 13 | SnakeBody::~SnakeBody() |
JoeShotton | 3:fcd6d70e9694 | 14 | { |
JoeShotton | 4:ea3fa51c4386 | 15 | //destructor |
JoeShotton | 4:ea3fa51c4386 | 16 | } |
JoeShotton | 4:ea3fa51c4386 | 17 | |
JoeShotton | 6:6c9453397f4a | 18 | void SnakeBody::init() { |
JoeShotton | 6:6c9453397f4a | 19 | _body_x.clear(); |
JoeShotton | 6:6c9453397f4a | 20 | _body_x.push_back(42); |
JoeShotton | 6:6c9453397f4a | 21 | _body_y.clear(); |
JoeShotton | 6:6c9453397f4a | 22 | _body_y.push_back(24); |
JoeShotton | 3:fcd6d70e9694 | 23 | } |
JoeShotton | 3:fcd6d70e9694 | 24 | |
JoeShotton | 4:ea3fa51c4386 | 25 | int SnakeBody::update_direction(float _angle, int _x_head, int _y_head) { |
JoeShotton | 4:ea3fa51c4386 | 26 | |
JoeShotton | 4:ea3fa51c4386 | 27 | 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 | 28 | // partition 360 into segments and check which segment the angle is in |
JoeShotton | 4:ea3fa51c4386 | 29 | if (_angle < 0.0f) { //check for -1.0 angle |
JoeShotton | 4:ea3fa51c4386 | 30 | _d = 0; |
JoeShotton | 4:ea3fa51c4386 | 31 | } else if (_angle < 45.0f) { //North quadrant |
JoeShotton | 4:ea3fa51c4386 | 32 | _d = 1; |
JoeShotton | 4:ea3fa51c4386 | 33 | } else if (_angle < 135.0f) { //East quadrant |
JoeShotton | 4:ea3fa51c4386 | 34 | _d = 2; |
JoeShotton | 4:ea3fa51c4386 | 35 | } else if (_angle < 225.0f) { //South quadrant |
JoeShotton | 4:ea3fa51c4386 | 36 | _d = 3; |
JoeShotton | 4:ea3fa51c4386 | 37 | } else if (_angle < 315.0f) { //West quadrant |
JoeShotton | 4:ea3fa51c4386 | 38 | _d = 4; |
JoeShotton | 4:ea3fa51c4386 | 39 | } else { //NW half - quadrant |
JoeShotton | 4:ea3fa51c4386 | 40 | _d = 1; |
JoeShotton | 4:ea3fa51c4386 | 41 | } |
JoeShotton | 3:fcd6d70e9694 | 42 | } |
JoeShotton | 4:ea3fa51c4386 | 43 | //printf("D: %d\n", _d); |
JoeShotton | 4:ea3fa51c4386 | 44 | return _d; //returns quadrant, represented by direction integer |
JoeShotton | 4:ea3fa51c4386 | 45 | }; |
JoeShotton | 4:ea3fa51c4386 | 46 | |
JoeShotton | 4:ea3fa51c4386 | 47 | void SnakeBody::update_position() { |
JoeShotton | 4:ea3fa51c4386 | 48 | |
JoeShotton | 4:ea3fa51c4386 | 49 | Direction _fsm[5] = { |
JoeShotton | 4:ea3fa51c4386 | 50 | {0, 0, {0,1,2,3,4}}, // Centred |
JoeShotton | 4:ea3fa51c4386 | 51 | {0, -1, {1,1,2,1,4}}, // North, will not go to centre or south |
JoeShotton | 4:ea3fa51c4386 | 52 | {1, 0, {2,1,2,3,2}}, // East, will not go to centre or west |
JoeShotton | 4:ea3fa51c4386 | 53 | {0, 1, {3,3,2,3,4}}, // South, will not go to centre or north |
JoeShotton | 4:ea3fa51c4386 | 54 | {-1, 0, {4,1,4,3,4}} // West, will not go to centre or east |
JoeShotton | 4:ea3fa51c4386 | 55 | }; |
JoeShotton | 4:ea3fa51c4386 | 56 | |
JoeShotton | 4:ea3fa51c4386 | 57 | _state = _fsm[_state].nextState[_d]; // adjusts direction fsm state based on direction |
JoeShotton | 4:ea3fa51c4386 | 58 | //printf("State: %d\n", _state); |
JoeShotton | 3:fcd6d70e9694 | 59 | |
JoeShotton | 3:fcd6d70e9694 | 60 | _x_head += _fsm[_state].delta_x; // increments x value based on fsm state value |
JoeShotton | 3:fcd6d70e9694 | 61 | _y_head += _fsm[_state].delta_y; // increments y value based on fsm state value |
JoeShotton | 3:fcd6d70e9694 | 62 | |
JoeShotton | 4:ea3fa51c4386 | 63 | _x_head = ((_x_head % 84) + 84) % 84; // wraps x back to within range 0-83 |
JoeShotton | 4:ea3fa51c4386 | 64 | _y_head = ((_y_head % 48) + 48) % 48; // wraps y back to within range 0-47 |
JoeShotton | 4:ea3fa51c4386 | 65 | |
JoeShotton | 4:ea3fa51c4386 | 66 | //printf("x_head: %d\n", _x_head); |
JoeShotton | 4:ea3fa51c4386 | 67 | //printf("y_head: %d\n", _y_head); |
JoeShotton | 4:ea3fa51c4386 | 68 | }; |
JoeShotton | 4:ea3fa51c4386 | 69 | |
JoeShotton | 4:ea3fa51c4386 | 70 | void SnakeBody::snake_movement(Gamepad &pad) { |
JoeShotton | 4:ea3fa51c4386 | 71 | |
JoeShotton | 4:ea3fa51c4386 | 72 | float _angle = pad.get_angle(); //finds joystick angle |
JoeShotton | 4:ea3fa51c4386 | 73 | //printf("Joystick angle: %f\n", _angle); |
JoeShotton | 4:ea3fa51c4386 | 74 | _d = update_direction(_angle, _x_head, _y_head); //converts angle into a integer direction |
JoeShotton | 4:ea3fa51c4386 | 75 | update_position(); //takes integer direction and feeds it into FSM to move snake in correct direction |
JoeShotton | 4:ea3fa51c4386 | 76 | update_body(); |
JoeShotton | 4:ea3fa51c4386 | 77 | } |
JoeShotton | 4:ea3fa51c4386 | 78 | |
JoeShotton | 4:ea3fa51c4386 | 79 | void SnakeBody::update_body() { |
JoeShotton | 4:ea3fa51c4386 | 80 | |
JoeShotton | 4:ea3fa51c4386 | 81 | _body_x.insert(_body_x.begin(), _x_head); //sets first array element to head coordinates |
JoeShotton | 4:ea3fa51c4386 | 82 | _body_y.insert(_body_y.begin(), _y_head); |
JoeShotton | 4:ea3fa51c4386 | 83 | |
JoeShotton | 4:ea3fa51c4386 | 84 | _body_x.erase(_body_x.begin() + _length, _body_x.end()); |
JoeShotton | 4:ea3fa51c4386 | 85 | _body_y.erase(_body_y.begin() + _length, _body_y.end()); |
JoeShotton | 4:ea3fa51c4386 | 86 | } |
JoeShotton | 4:ea3fa51c4386 | 87 | |
JoeShotton | 4:ea3fa51c4386 | 88 | void SnakeBody::draw_body(N5110 &lcd){ |
JoeShotton | 4:ea3fa51c4386 | 89 | |
JoeShotton | 4:ea3fa51c4386 | 90 | for(int i = 0; i < _length; i++) { |
JoeShotton | 4:ea3fa51c4386 | 91 | lcd.drawRect(_body_x[i],_body_y[i],2,2,FILL_BLACK); |
JoeShotton | 4:ea3fa51c4386 | 92 | } |
JoeShotton | 4:ea3fa51c4386 | 93 | } |
JoeShotton | 4:ea3fa51c4386 | 94 | |
JoeShotton | 4:ea3fa51c4386 | 95 | void SnakeBody::snake_snake_collision(Gamepad &pad, bool &death){ |
JoeShotton | 4:ea3fa51c4386 | 96 | for(int i = 1; i < _length; i++) { |
JoeShotton | 4:ea3fa51c4386 | 97 | 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 | 4:ea3fa51c4386 | 98 | pad.led(1,0.9); |
JoeShotton | 4:ea3fa51c4386 | 99 | //printf("DEAD \n"); |
JoeShotton | 4:ea3fa51c4386 | 100 | _state = 0; |
JoeShotton | 4:ea3fa51c4386 | 101 | death = true; |
JoeShotton | 4:ea3fa51c4386 | 102 | //return true; |
JoeShotton | 4:ea3fa51c4386 | 103 | } else { |
JoeShotton | 4:ea3fa51c4386 | 104 | //pad.led(1,0.0); |
JoeShotton | 4:ea3fa51c4386 | 105 | //printf("ALIVE \n"); |
JoeShotton | 4:ea3fa51c4386 | 106 | //return false; |
JoeShotton | 4:ea3fa51c4386 | 107 | } |
JoeShotton | 4:ea3fa51c4386 | 108 | } |
JoeShotton | 4:ea3fa51c4386 | 109 | } |