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
Snake/Snake.cpp
- Committer:
- JRM1986
- Date:
- 2018-04-06
- Revision:
- 11:e260c17a0489
- Parent:
- 10:62d8cb7742c3
- Child:
- 12:9f2f64016f56
File content as of revision 11:e260c17a0489:
#include "Snake.h" Snake::Snake() { } Snake::~Snake() { } void Snake::init() { _x = 41; _y = 23; _next = W; } void Snake::update(Direction in, Direction cur) { set_snake_direction(in, cur); Direction next = get_snake_direction(); //set_current_direction(next); cur = get_current_direction(); set_snake_position(next); Vector2D pos = get_snake_position(); } void Snake::draw(N5110 &lcd) { lcd.setPixel(_x, _y, true); } void Snake::set_snake_direction(Direction input, Direction current) { switch(current) { case CENTRE: switch(input) { case CENTRE: _next = current; break; case N: _next = input; break; case E: _next = input; break; case S: _next = input; break; case W: _next = input; break; default: _next = current; error("Invalid state"); break; } break; case N: switch(input) { case CENTRE: _next = current; break; case N: _next = input; break; case E: _next = input; break; case S: _next = current; break; case W: _next = input; break; default: _next = current; error("Invalid state"); break; } break; case E: switch(input) { case CENTRE: _next = current; break; case N: _next = input; break; case E: _next = current; break; case S: _next = current; break; case W: _next = input; break; default: _next = current; error("Invalid state"); break; } break; case S: switch(input) { case CENTRE: _next = current; break; case N: _next = current; break; case E: _next = input; break; case S: _next = current; break; case W: _next = input; break; default: _next = current; error("Invalid state"); break; } break; case W: switch(input) { case CENTRE: _next = current; break; case N: _next = input; break; case E: _next = current; break; case S: _next = input; break; case W: _next = current; break; default: _next = current; error("Invalid state (input)"); break; } break; default: error("Invalid state (current)"); break; } } Direction Snake::get_snake_direction() { Direction d = _next; return d; } /* void Snake::set_current_direction(Direction new_cur) { _next = new_cur; }*/ Direction Snake::get_current_direction() { Direction d = _next; return d; } void Snake::set_snake_position(Direction next) { next = get_snake_direction(); Vector2D p; switch(next) { case(N): p.x = p.x; --p.y; break; case(E): ++p.x; p.y = p.y; break; case(S): p.x = p.x; ++p.y; break; case(W): --p.x; p.y = p.y; break; default: p.x = p.x; p.y = p.y; break; } _x = p.x; _y = p.y; } Vector2D Snake::get_snake_position() { Vector2D pos = {_x, _y}; return pos; } // FSM Attempt /* int state = N; StateDirection fsm[4] = { {N,{N,E,N,W}}, {E,{N,E,S,E}}, {S,{S,E,S,W}}, {W,{N,W,S,W}} }; _direction = fsm[state].output; // set ouput depending on current state state = fsm[state].nextState[input]; // read input and update curent state */