EasyX实现贪吃蛇(单文件,详注释)

EasyX实现贪吃蛇(单文件,详注释),第1张

EasyX实现贪吃蛇(单文件,详注释)

        这个程序太简单,w,a,s,d控制方向就完事了,直接上代码,谁都能看懂。

#include
#include
#include
#include
using namespace std;
#define random(a,b) (rand()%(b-a)+a)
class Snake
{
public:
	void runing();//跑起来
protected:
	struct body
	{
		int x = 0, y = 0;
	};
	vectorBody;//蛇的胴体
	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();//关闭图形界面
}

 

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5658635.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存