ZIYI CHEN ml17z4c 201214999
Dependencies: mbed
Diff: Game/Game.cpp
- Revision:
- 9:a8b2086a46e5
- Parent:
- 8:52e0506e98b8
--- a/Game/Game.cpp Mon May 06 00:06:15 2019 +0000 +++ b/Game/Game.cpp Wed May 08 20:48:33 2019 +0000 @@ -1,111 +1,99 @@ #include "Game.h" -Snake::Snake() +Game::Game() { } -Snake::~Snake() +Game::~Game() { } -void Snake::init(int x, int y) +void Game::init(int x, int y) { - _length = 5; - _oldDirection = 'N'; - memset(_x, 0, sizeof(_x)); - memset(_y, 0, sizeof(_y)); + memset(_x, 0, sizeof(_x)); + memset(_y, 0, sizeof(_y)); + + _length = 5; + Previous_direction = 'N'; - for (int i = 0; i < _length; i++) { - _x[i] = x; - _y[i] = y + i; + for (int i = 0; i < _length; i++) { + _x[i] = x; + _y[i] = y + i; } - - printf("Snake init\n"); + } -void Snake::update(Direction d) +void Game::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){ + + switch(d) { case N: - if(_oldDirection !='S') - { - _y[0] = _y[0] - 1; - _oldDirection = 'N'; - printf("_oldDirection change to North\n"); - + if(Previous_direction !='S') { + _y[0] = _y[0] - 1; + Previous_direction = 'N'; + } + break; + + case S: + if(Previous_direction !='N') { + _y[0] = _y[0] + 1; + Previous_direction = 'S'; + } + break; + + case E: + if(Previous_direction != 'W') { + _x[0] = _x[0] + 1; + Previous_direction = 'E'; } 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"); - - } + case W: + if(Previous_direction != 'E') { + _x[0] = _x[0]- 1; + Previous_direction = 'W'; + } break; - case W: - if(_oldDirection != 'E'){ - _x[0] = _x[0]- 1; - _oldDirection = 'W'; - printf("_oldDirection change to West\n"); - - }break; + } + if (Previous_direction == 'N') { + _y[0] = _y[0] - 1; + } else if (Previous_direction == 'S') { + _y[0] = _y[0] + 1; + } else if (Previous_direction == 'E') { + _x[0] = _x[0] + 1; + } else if (Previous_direction == 'W') { + _x[0] = _x[0] - 1; + } } - - } - -int Snake::getLength() +int Game::getLength() { return _length; - printf("return _length\n"); } -void Snake::grow() +void Game::grow() { _length =_length + 1; printf("length + 1\n"); } -int Snake::getX(int head) + +int Game::xcoordinate(int now) { - return _x[head]; - printf("return _x[head]\n"); + return _x[now]; } -int Snake::getY(int head) +int Game::ycoordinate(int now) { - return _y[head]; - printf("return _y[head]\n"); + return _y[now]; }