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
Game/Game.cpp
- Committer:
- ziyi11
- Date:
- 2019-05-06
- Revision:
- 8:52e0506e98b8
- Parent:
- 7:8b6f175fcb0e
- Child:
- 9:a8b2086a46e5
File content as of revision 8:52e0506e98b8:
#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");
}