#include "Snake.h"
void gotoxys(HANDLE houts,int x,int y){
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(houts,c);
}
HANDLE houts = GetStdHandle(STD_OUTPUT_HANDLE);
Snake::Snake(Wall &tempWall,Food &tempFood):wall(tempWall),food(tempFood)
{
pHead = NULL;
isRool = true;
}
Snake::~Snake(void)
{
}
void Snake::destoryHistoryData()
{
Point* tempP = pHead;
//Point* tempP = NULL;
while (pHead!=NULL)
{
tempP = pHead->pointNext;
delete pHead;
pHead = tempP;
}
}
void Snake::snakeInit()
{
destoryHistoryData();
addPoint(5,3);
addPoint(5,4);
addPoint(5,5);
}
bool Snake::move(char key)
{
int x = pHead->point_X;
int y = pHead->point_Y;
switch(key)
{
case LEFT:
y--;
break;
case RIGHT:
y++;
break;
case UP:
x--;
break;
case DOWN:
x++;
break;
}
Point *pCurr = pHead->pointNext;
Point *pPre = pHead;
while(pCurr->pointNext !=NULL)
{
pCurr = pCurr->pointNext;
pPre = pPre->pointNext;
}
if (pCurr->point_X==x && pCurr->point_Y==y)
{
isRool = true;
}
else
{
if (wall.getPoint(x,y)=='*' || wall.getPoint(x,y)=='=')
{
addPoint(x,y);
deletePoint();
//system("cls");
//wall.drawWall();
gotoxys(houts,0,Wall::COL);
cout<<"Game Over!!!!"<<endl;
return false;
}
}
if (wall.getPoint(x,y)=='#')
{
addPoint(x,y);
food.setFood();
}
else
{
addPoint(x,y);
deletePoint();
}
return true;
}
void Snake::addPoint(int x,int y)
{
Point *tempPo = new Point;
tempPo->point_X = x;
tempPo->point_Y = y;
tempPo->pointNext = NULL;
if (pHead!=NULL)
{
wall.setPoint(pHead->point_X,pHead->point_Y,'=');
gotoxys(houts,pHead->point_Y*2,pHead->point_X);
cout<<'=';
}
tempPo->pointNext =pHead;
pHead = tempPo;
wall.setPoint(pHead->point_X,pHead->point_Y,'O');
gotoxys(houts,pHead->point_Y*2,pHead->point_X);
cout<<'O';
}
void Snake::deletePoint()
{
if (pHead ==NULL || pHead->pointNext==NULL)
{
return;
}
Point *pCurr = NULL;
Point *pPre = NULL;
pPre= pHead;
pCurr = pHead->pointNext;
while (pCurr->pointNext !=NULL)
{
pCurr = pCurr->pointNext;
pPre = pPre->pointNext;
}
wall.setPoint(pCurr->point_X,pCurr->point_Y,' ');
gotoxys(houts,pCurr->point_Y*2,pCurr->point_X);
cout<<' ';
pPre->pointNext = NULL;
delete pCurr;
pCurr = NULL;
}