这个程序太简单,w,a,s,d控制方向就完事了,直接上代码,谁都能看懂。
#include<easyx.h>
#include<vector>
#include<ctime>
#include<conio.h>
using namespace std;
#define random(a,b) (rand()%(b-a)+a)
class Snake
{
public:
void runing();//跑起来
protected:
struct body
{
int x = 0, y = 0;
};
vector<body>Body;//蛇的胴体
char flag = 'd';//初始方向“右”
body Food, dele;//食物和要删去的尾巴
int flashtime = 100, score = 0;//刷新速度即蛇的移速,越大速度越慢and得分
void drawmap();//画地图
void initsnake();//初始化蛇和食物
bool game_end();//判断游戏是否结束
bool food();//判断是否吃到食物以及生成新食物
void drawbody();//画蛇
};
void Snake::drawmap()/*画地图*/
{
setfillcolor(LIGHTRED);
solidrectangle(10, 10, 510, 510);
setfillcolor(BLACK);
solidrectangle(20, 20, 500, 500);
outtextxy(560, 60, _T("得分"));//初始化计分板
char s[521]; sprintf_s(s, ("%d"), score);
outtextxy(565, 120, _T(s));
outtextxy(560, 180, _T("W 上"));
outtextxy(560, 240, _T("A 左"));
outtextxy(560, 300, _T("S 下"));
outtextxy(560, 360, _T("D 右"));
outtextxy(560, 420, _T("E 暂停"));
initsnake();//初始化蛇和食物
}
void Snake::initsnake()/*初始化蛇和食物*/
{
body n;
n.x = n.y = 95;
Body.push_back(n);
n.x -= 10;
Body.push_back(n);
n.x -= 10;
Body.push_back(n);
Food.x = random(0, 49) * 10 + 25;
Food.y = random(0, 49) * 10 + 25;
setfillcolor(YELLOW);
solidcircle(Food.x, Food.y, 5);
}
bool Snake::game_end()/*判断游戏是否结束*/
{
body head = Body.front();
if (head.x <= 20 || head.x >= 500 || head.y <= 20 || head.y >= 500)
return true;//碰边界死了
for (int i = 1; i < Body.size(); i++)
if (head.x == Body[i].x && head.y == Body[i].y)
return true;//吃到自己死了
return false;//没死
}
bool Snake::food()/*判断是否吃到食物以及生成新食物*/
{
body head = Body.front();
if (head.x == Food.x && head.y == Food.y)//吃到食物了
{
clearcircle(Food.x, Food.y, 5);//消除原来的食物
Food.x = random(0, 49) * 10 + 25;
Food.y = random(0, 49) * 10 + 25;
for (int i = 0; i < Body.size(); i++)//食物不能生成在蛇身体上
if ((Food.x == Body[i].x && Food.y == Body[i].y) || (Food.x == dele.x && Food.y == dele.y))
{
Food.x = random(0, 49) * 10 + 25;
Food.y = random(0, 49) * 10 + 25;
i = -1;
}
setfillcolor(YELLOW);
solidcircle(Food.x, Food.y, 5);
return true;
}
return false;
}
void Snake::drawbody()/*画蛇*/
{
setfillcolor(MAGENTA);//先画头后画身子,不同色
solidcircle(Body.front().x, Body.front().y, 5);
setfillcolor(CYAN);
for (int i = 1; i < Body.size(); i++)
solidcircle(Body[i].x, Body[i].y, 5);
}
void Snake::runing()/*跑起来*/
{
time_t ct = clock() - time_t(100);//计时器
outtextxy(140, 230, _T("按任意键开始")), _getch();//开始的抉择
drawmap();//画地图
while (!game_end())//直到游戏结束
{
if (clock() - ct > flashtime)//刷新状态
{
ct = clock();
if (_kbhit())//获取键盘操作控制方向
{
char tflag = _getch();
if (tflag == 'E' || tflag == 'e')//E键暂停,任意键恢复
tflag = _getch();
if ((tflag == 'w' || tflag == 'W') && flag != 's' && flag != 'S')
flag = tflag;
else if ((tflag == 's' || tflag == 'S') && flag != 'w' && flag != 'W')
flag = tflag;
else if ((tflag == 'a' || tflag == 'A') && flag != 'd' && flag != 'D')
flag = tflag;
else if ((tflag == 'd' || tflag == 'D') && flag != 'a' && flag != 'A')
flag = tflag;
}
if (food())//吃到食物就尾巴加长一段,分数加一
{
Body.push_back(Body.back()), score++;
char s[521]; sprintf_s(s, ("%d"), score);
outtextxy(565, 120, _T(s));
}
drawbody();//重绘蛇
clearcircle(dele.x, dele.y, 5);//删去上一条蛇留下的尾巴
dele = Body.back();//定位成这一条蛇的尾巴
for (int i = Body.size() - 1; i > 0; i--)//身体前进
Body[i] = Body[i - 1];
if (flag == 'd' || flag == 'a' || flag == 'D' || flag == 'A')//蛇头前进
Body.front().x += (flag == 'd' || flag == 'D') ? 10 : -10;
else Body.front().y += (flag == 'w' || flag == 'W') ? -10 : 10;
}
}
}
int main()
{
Snake snake;//新建一个对象
initgraph(700, 520);//打开绘图界面
settextstyle(40, 0, _T("宋体"));
snake.runing();//跑起来
outtextxy(180, 230, _T("游戏结束")),_getch();//结束了
closegraph();//关闭图形界面
}