ZIYI CHEN ml17z4c 201214999
Dependencies: mbed
Diff: Game/Game.cpp
- Revision:
- 8:52e0506e98b8
- Parent:
- 7:8b6f175fcb0e
- Child:
- 9:a8b2086a46e5
--- a/Game/Game.cpp Tue Apr 30 09:14:17 2019 +0000 +++ b/Game/Game.cpp Mon May 06 00:06:15 2019 +0000 @@ -0,0 +1,112 @@ +#include "Game.h" + + +Snake::Snake() +{ + +} + +Snake::~Snake() +{ + +} + +void Snake::init(int x, int y) +{ + _length = 5; + _oldDirection = 'N'; + memset(_x, 0, sizeof(_x)); + memset(_y, 0, sizeof(_y)); + + + for (int i = 0; i < _length; i++) { + _x[i] = x; + _y[i] = y + i; + } + + printf("Snake init\n"); +} + +void Snake::update(Direction d) +{ + + for (int i = _length-1; i >= 1; i--) { + _x[i] = _x[i-1]; + _y[i] = _y[i-1]; + } + + if (_oldDirection == 'N') { + _y[0] = _y[0] - 1;} + else if (_oldDirection == 'S') { + _y[0] = _y[0] + 1;} + else if (_oldDirection == 'E') { + _x[0] = _x[0] + 1;} + else if (_oldDirection == 'W') { + _x[0] = _x[0] - 1;} + }; + printf(" Direction changed\n"); + switch(d){ + case N: + if(_oldDirection !='S') + { + _y[0] = _y[0] - 1; + _oldDirection = 'N'; + printf("_oldDirection change to North\n"); + + } + break; + + case S: + if(_oldDirection !='N'){ + _y[0] = _y[0] + 1; + _oldDirection = 'S'; + printf("_oldDirection change to South\n"); + + } + break; + + case E: + if(_oldDirection != 'W'){ + _x[0] = _x[0] + 1; + _oldDirection = 'E'; + printf("_oldDirection change to East\n"); + + } + break; + case W: + if(_oldDirection != 'E'){ + _x[0] = _x[0]- 1; + _oldDirection = 'W'; + printf("_oldDirection change to West\n"); + + }break; +} + + + } + +int Snake::getLength() +{ + return _length; + printf("return _length\n"); +} + +void Snake::grow() +{ + _length =_length + 1; + printf("length + 1\n"); +} +int Snake::getX(int head) +{ + return _x[head]; + printf("return _x[head]\n"); +} + +int Snake::getY(int head) +{ + return _y[head]; + printf("return _y[head]\n"); +} + + +