|
|
|
|
#include "Snake.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <graphics.h>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
Snake::Snake()
|
|
|
|
|
{
|
|
|
|
|
Point pos0(210, 210);
|
|
|
|
|
Point pos1(190, 210);
|
|
|
|
|
Point pos2(170, 210);
|
|
|
|
|
|
|
|
|
|
this->m_snakelist.push_back(pos0);
|
|
|
|
|
this->m_snakelist.push_back(pos1);
|
|
|
|
|
this->m_snakelist.push_back(pos2);
|
|
|
|
|
|
|
|
|
|
this->m_direction = Dir::DIR_RIGHT;
|
|
|
|
|
this->m_len = 3;
|
|
|
|
|
this->m_speed = this->OrgSpeed;
|
|
|
|
|
}
|
|
|
|
|
Snake::Snake(int x)
|
|
|
|
|
{
|
|
|
|
|
Point pos0(210, x);
|
|
|
|
|
Point pos1(190, x);
|
|
|
|
|
Point pos2(170, x);
|
|
|
|
|
|
|
|
|
|
this->m_snakelist.push_back(pos0);
|
|
|
|
|
this->m_snakelist.push_back(pos1);
|
|
|
|
|
this->m_snakelist.push_back(pos2);
|
|
|
|
|
|
|
|
|
|
this->m_direction = Dir::DIR_RIGHT;
|
|
|
|
|
this->m_len = 3;
|
|
|
|
|
this->m_speed = this->OrgSpeed;
|
|
|
|
|
}
|
|
|
|
|
int Snake::getLen()
|
|
|
|
|
{
|
|
|
|
|
return this->m_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Snake::getSpeed()
|
|
|
|
|
{
|
|
|
|
|
return this->m_speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dir Snake::getDirection()
|
|
|
|
|
{
|
|
|
|
|
return this->m_direction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Snake::setSpeed(int speed)//<2F><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if (speed > MaxSpeed)
|
|
|
|
|
{
|
|
|
|
|
if (m_speed != MaxSpeed)
|
|
|
|
|
{
|
|
|
|
|
m_speed = MaxSpeed;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if (speed < MinSpeed)
|
|
|
|
|
{
|
|
|
|
|
if (m_speed != MinSpeed)
|
|
|
|
|
{
|
|
|
|
|
m_speed =MinSpeed;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_speed = speed;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Snake::Move()
|
|
|
|
|
{
|
|
|
|
|
this->m_tail = this->m_snakelist.back();
|
|
|
|
|
|
|
|
|
|
//<2F>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ㣬<DAB5><E3A3AC><EFBFBD>Ƶ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
this->m_snakelist.pop_back();
|
|
|
|
|
Point headPos = this->m_snakelist.front();
|
|
|
|
|
this->m_snakelist.push_front(headPos);
|
|
|
|
|
|
|
|
|
|
switch (this->m_direction)
|
|
|
|
|
{
|
|
|
|
|
case Dir::DIR_UP:
|
|
|
|
|
this->m_snakelist.begin()->y -= SNAKE_WIDTH;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Dir::DIR_RIGHT:
|
|
|
|
|
this->m_snakelist.begin()->x += SNAKE_WIDTH;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Dir::DIR_DOWN:
|
|
|
|
|
this->m_snakelist.begin()->y += SNAKE_WIDTH;
|
|
|
|
|
break;
|
|
|
|
|
case Dir::DIR_LEFT:
|
|
|
|
|
this->m_snakelist.begin()->x -= SNAKE_WIDTH;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Snake::EatFood()
|
|
|
|
|
{
|
|
|
|
|
//<2F>Ե<EFBFBD>ʳ<EFBFBD><CAB3>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
this->m_snakelist.push_back(this->m_tail);
|
|
|
|
|
|
|
|
|
|
this->m_len += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Snake::ChangeDir(Dir dir)
|
|
|
|
|
{
|
|
|
|
|
switch (dir)
|
|
|
|
|
{
|
|
|
|
|
case Dir::DIR_UP:
|
|
|
|
|
case Dir::DIR_DOWN:
|
|
|
|
|
if (m_direction != Dir::DIR_UP && m_direction != Dir::DIR_DOWN)
|
|
|
|
|
{
|
|
|
|
|
m_direction = dir;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Dir::DIR_RIGHT:
|
|
|
|
|
case Dir::DIR_LEFT:
|
|
|
|
|
if (m_direction != Dir::DIR_RIGHT && m_direction != Dir::DIR_LEFT)
|
|
|
|
|
{
|
|
|
|
|
m_direction = dir;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Snake::Dead()
|
|
|
|
|
{
|
|
|
|
|
//TODO
|
|
|
|
|
int x = 0;
|
|
|
|
|
int y = 0; //x<><78>y<EFBFBD><79>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>ĸñ<C4B8><C3B1><EFBFBD>
|
|
|
|
|
int z = 0; //z<><7A>ʾ<EFBFBD>ı䷽<C4B1><E4B7BD>
|
|
|
|
|
|
|
|
|
|
//ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
|
|
std::list<Point>::iterator it = this->m_snakelist.begin();
|
|
|
|
|
for (; it != this->m_snakelist.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
x = (rand() % 4) * SNAKE_WIDTH;
|
|
|
|
|
y = (rand() % 4) * SNAKE_WIDTH;
|
|
|
|
|
z = (rand() % 8);
|
|
|
|
|
|
|
|
|
|
switch (z)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
//<2F><>
|
|
|
|
|
(*it).x += x;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
//<2F><>
|
|
|
|
|
(*it).y += y;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
//<2F><>
|
|
|
|
|
(*it).x -= x;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
//<2F><>
|
|
|
|
|
(*it).y -= y;
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
(*it).x += x;
|
|
|
|
|
(*it).y += y;
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
(*it).x -= x;
|
|
|
|
|
(*it).y += y;
|
|
|
|
|
break;
|
|
|
|
|
case 6:
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
(*it).x -= x;
|
|
|
|
|
(*it).y -= y;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
(*it).x += x;
|
|
|
|
|
(*it).y -= y;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Snake::ColideWall(int left, int top, int right, int bottom)
|
|
|
|
|
{
|
|
|
|
|
int x = this->m_snakelist.front().x;
|
|
|
|
|
int y = this->m_snakelist.front().y;
|
|
|
|
|
return (x < left || x > right || y < top || y > bottom);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Snake::ColideSnake()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (m_len <= 3) return false;
|
|
|
|
|
std::list<Point>::iterator it = this->m_snakelist.begin();
|
|
|
|
|
|
|
|
|
|
Point pos = *it;
|
|
|
|
|
Point next_pos;
|
|
|
|
|
it++;
|
|
|
|
|
|
|
|
|
|
while (it != this->m_snakelist.end())
|
|
|
|
|
{
|
|
|
|
|
next_pos = *it;
|
|
|
|
|
|
|
|
|
|
if (pos == next_pos)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
bool Snake::ColideSnake2(Snake& snake2)
|
|
|
|
|
{
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB><EFBFBD>ߵ<EFBFBD>ͷ<EFBFBD><CDB7>λ<EFBFBD><CEBB>
|
|
|
|
|
Point headPos = this->m_snakelist.front();
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>壬<EFBFBD><E5A3AC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ߵ<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>бȽ<D0B1>
|
|
|
|
|
std::list<Point>::iterator it = snake2.m_snakelist.begin();
|
|
|
|
|
while (it != snake2.m_snakelist.end())
|
|
|
|
|
{
|
|
|
|
|
if (*it == headPos) {
|
|
|
|
|
return true; // <20><>ײ
|
|
|
|
|
}
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false; // δ<><CEB4>ײ
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Snake::ColideFood(Point point)
|
|
|
|
|
{
|
|
|
|
|
if (this->m_snakelist.front() == point)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Snake::DrawSnake()
|
|
|
|
|
{
|
|
|
|
|
std::list<Point>::iterator it = this->m_snakelist.begin();
|
|
|
|
|
for (; it != this->m_snakelist.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
DrawSnakeNode(*it);
|
|
|
|
|
}
|
|
|
|
|
DrawSnakeHead(this->m_snakelist.front());
|
|
|
|
|
}
|
|
|
|
|
void Snake::DrawSnake2()
|
|
|
|
|
{
|
|
|
|
|
std::list<Point>::iterator it = this->m_snakelist.begin();
|
|
|
|
|
for (; it != this->m_snakelist.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
DrawSnakeNode2(*it);
|
|
|
|
|
}
|
|
|
|
|
DrawSnakeHead2(this->m_snakelist.front());
|
|
|
|
|
}
|
|
|
|
|
void Snake::DrawSnakeHead(Point pos)
|
|
|
|
|
{
|
|
|
|
|
//<2F><>ɫ<EFBFBD><C9AB>ȫ<EFBFBD><C8AB><EFBFBD>䣬<EFBFBD>ޱ߿<DEB1><DFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
setfillcolor(0xAA00AA);
|
|
|
|
|
setfillstyle(BS_SOLID);
|
|
|
|
|
solidrectangle(pos.x - SNAKE_RADIU, pos.y + SNAKE_RADIU, pos.x + SNAKE_RADIU, pos.y - SNAKE_RADIU);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Snake::DrawSnakeNode(Point pos)
|
|
|
|
|
{
|
|
|
|
|
//<2F><>ɫ<EFBFBD><C9AB>ȫ<EFBFBD><C8AB><EFBFBD>䣬<EFBFBD>ޱ߿<DEB1><DFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
setfillcolor(GREEN);
|
|
|
|
|
setfillstyle(BS_SOLID);
|
|
|
|
|
solidrectangle(pos.x - SNAKE_RADIU, pos.y + SNAKE_RADIU, pos.x + SNAKE_RADIU, pos.y - SNAKE_RADIU);
|
|
|
|
|
}
|
|
|
|
|
void Snake::DrawSnakeHead2(Point pos)
|
|
|
|
|
{
|
|
|
|
|
//<2F><>ɫ<EFBFBD><C9AB>ȫ<EFBFBD><C8AB><EFBFBD>䣬<EFBFBD>ޱ߿<DEB1><DFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
setfillcolor(YELLOW);
|
|
|
|
|
setfillstyle(BS_SOLID);
|
|
|
|
|
solidrectangle(pos.x - SNAKE_RADIU, pos.y + SNAKE_RADIU, pos.x + SNAKE_RADIU, pos.y - SNAKE_RADIU);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Snake::DrawSnakeNode2(Point pos)
|
|
|
|
|
{
|
|
|
|
|
//<2F><>ɫ<EFBFBD><C9AB>ȫ<EFBFBD><C8AB><EFBFBD>䣬<EFBFBD>ޱ߿<DEB1><DFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
setfillcolor(RED);
|
|
|
|
|
setfillstyle(BS_SOLID);
|
|
|
|
|
solidrectangle(pos.x - SNAKE_RADIU, pos.y + SNAKE_RADIU, pos.x + SNAKE_RADIU, pos.y - SNAKE_RADIU);
|
|
|
|
|
}
|
|
|
|
|
std::list<Point> Snake::GetSnakeAllNode()
|
|
|
|
|
{
|
|
|
|
|
return this->m_snakelist;
|
|
|
|
|
}
|