C++ 一个有bug的贪吃蛇。。。。。。。。

发布时间:2023年12月27日

C++ ? ?一个有bug的贪吃蛇。。。。。。。。

#include <graphics.h>
#include<Windows.h>
#include<Mmsystem.h>
#include<conio.h> 
#include<time.h> 
#include<stdio.h>
#include<easyx.h>
using namespace std;
#pragma warning(disable:4996)
#define WINDOW_LENGTH 640 //窗口长
#define WINDOW_WIDE 480 //串口宽
#define M_TOP 50 //地图左上顶点y坐标
#define M_LEFT 20 //地图左上顶点x坐标
#define M_BOTTOM WINDOW_WIDE-30 //地图右下顶点y坐标
#define M_RIGHT WINDOW_LENGTH-30 //地图右下顶点x坐标
#define SNAKE_UP 72 //蛇向上运动
#define SNAKE_DOWN 80 //蛇向下运动
#define SNAKE_LEFT 75 //蛇向左运动
#define SNAKE_RIGHT 77  //蛇向右运动
#define SNAKE_QUIT 27 //结束游戏ESC键
#define SNAKE_SPEED 500 //蛇运动速度
#define SNAKE_PAUSS 32 //蛇暂停运动


typedef struct FOOD { //定义食物结构体
	int x;
	int y;
	int FOOD_LIVE;
}FOOD;


typedef struct NODE { //定义蛇的节点
	int x;
	int y;
	NODE* PRE;
	NODE* NEXT;
}NODE;


typedef struct SANKE { //定义一个蛇
	NODE* TAIL;
	NODE* HEAD;
	int NODES; //蛇的节数
	int DIRECTION; //蛇的运动方向
	int SANKE_LIVE; //存活状态
}SANKE;


int score_ = 0;
FOOD f;
SANKE s;
int rank;
int t = 1;
int tt;
int biteSelf(); // 判断是否咬到了自己
int snake_life(); //蛇是否存活
void gameover(); //结束界面
int eatfood(); /食物
void score(); //分数
void time(); //时间
void meau(); //菜单
void initGAME(); //初始化游戏
void creatWINDOW(int x, int y); //创建窗口
void closeWINDOW(); //关闭窗口
void creatmap(); //创建地图
void creatfood(); //创建食物
void drawfood(); //画食物
void drawSANKE(); //画蛇
void initSANKE(); //创建蛇
void moveSANKE(); //移动蛇
void speed(); //调整速度
void playgame(); //开始游戏
int direction(int c); //判断操作方向是否与蛇的方向相同
void Data(); //保存分数
void ESC(); //esc结束
void upgrade(); //升级模式




int main()
{
	while (t)
	{
		meau();
		initGAME();
		playgame();
		closeWINDOW();
	}
}


void initGAME()//进入游戏
{
	creatWINDOW(WINDOW_LENGTH, WINDOW_WIDE);
	creatmap();
	creatfood();
	initSANKE();
	score();
	f.FOOD_LIVE = 0;
}


void creatWINDOW(int x, int y) {//创建窗口
	initgraph(WINDOW_LENGTH, WINDOW_WIDE);
	setbkcolor(RGB(166, 135, 100));
	cleardevice();
}


void closeWINDOW() {//关闭窗口
	Data();
	getch();
	closegraph();
}


void creatmap() {
	int i;
	setlinecolor(BLACK);
	rectangle(M_LEFT, M_TOP, M_RIGHT, M_BOTTOM);//画外边界线
	rectangle(M_LEFT - 10, M_TOP - 10, M_RIGHT + 10, M_BOTTOM + 10);//画内边界线
	for (i = M_LEFT; i <= M_RIGHT; i = i + 10)
		line(i, M_TOP, i, M_TOP - 10);
	for (i = M_TOP; i <= M_BOTTOM; i = i + 10)
		line(M_LEFT, i, M_LEFT - 10, i);
	for (i = M_LEFT; i <= M_RIGHT; i = i + 10)
		line(i, M_BOTTOM, i, M_BOTTOM + 10);
	for (i = M_TOP; i <= M_BOTTOM; i = i + 10)
		line(M_RIGHT, i, M_RIGHT + 10, i);
}


void creatfood() {
	srand(time(0));
	int x, y;
	while (1) {
		x = rand() % (600 - 20 + 1) + 20;
		y = rand() % (440 - 50 + 1) + 50;
		if (x % 10 == 0 && y % 10 == 0) {
			f.x = x;
			f.y = y;
			break;
		}
	}
	f.FOOD_LIVE = 1;
}


void drawfood() {
	setfillcolor(YELLOW);
	fillrectangle(f.x, f.y, f.x + 9, f.y + 9);
}


void initSANKE() {
	s.HEAD = (NODE*)malloc(sizeof(NODE));
	s.TAIL = (NODE*)malloc(sizeof(NODE));
	s.HEAD->x = 30;//初始蛇头蛇尾
	s.HEAD->y = 400;
	s.TAIL->x = 30;
	s.TAIL->y = 410;
	s.HEAD->NEXT = s.TAIL;
	s.HEAD->PRE = NULL;
	s.TAIL->NEXT = NULL;
	s.TAIL->PRE = s.HEAD;
	s.DIRECTION = SNAKE_UP;
	s.NODES = 2;
	s.SANKE_LIVE = 1;
}


void drawSANKE() {
	NODE* p;//指针p指向蛇的头部
	p = s.HEAD;
	while (p)
	{
		if (p == s.HEAD)
			setfillcolor(BLUE);//蛇头为蓝色
		else
			setfillcolor(YELLOW);
		fillrectangle(p->x, p->y, p->x + 9, p->y + 9);
		p = p->NEXT;
	}
}


int direction(int c) {//不是相反的方向,返回0.

	if (s.DIRECTION == SNAKE_DOWN && c == SNAKE_UP)
		return 1;
	else if (s.DIRECTION == SNAKE_UP && c == SNAKE_DOWN)
		return 1;
	else if (s.DIRECTION == SNAKE_LEFT && c == SNAKE_RIGHT)
		return 1;
	else if (s.DIRECTION == SNAKE_RIGHT && c == SNAKE_LEFT)
		return 1;
	else
		return 0;
}


void playgame() {
	int c;
	while (true) {
		if (f.FOOD_LIVE == 1)//若食物存在,则画出食物
		{
			drawfood();
		}
		if (f.FOOD_LIVE == 0)//若食物不存在,则生成食物
		{
			creatfood();
			drawfood();
		}
		if (snake_life() == 0)//若蛇死亡,游戏结束
		{
			gameover();
			closeWINDOW();
			break;
		}
		while (!kbhit())//当没有按键的时候
		{
			time();
			if (eatfood() == 1)
			{
				creatfood();
				drawfood();
			}
			if (snake_life() == 0)//若蛇死亡,游戏结束
			{
				break;
			}
			moveSANKE();
			drawSANKE();
			if (tt)
				upgrade();
			speed();
		}
		while (kbhit())//当有按键的时候
		{
			c = getch();
			if (direction(c) == 0)//不是相反的方向
			{
				switch (c)
				{
				case SNAKE_LEFT: s.DIRECTION = SNAKE_LEFT; break;
				case SNAKE_RIGHT: s.DIRECTION = SNAKE_RIGHT; break;
				case SNAKE_UP: s.DIRECTION = SNAKE_UP; break;
				case SNAKE_DOWN: s.DIRECTION = SNAKE_DOWN; break;
				case SNAKE_PAUSS:system("pause"); break;
				case SNAKE_QUIT:ESC();
				default:break;
				}
			}
		}
	}
}


void ESC() {
	gameover();
	_getch();
	exit(0);
}


void moveSANKE() {
	NODE* p;
	p = (NODE*)malloc(sizeof(NODE));
	clearrectangle(s.TAIL->x, s.TAIL->y, s.TAIL->x + 9, s.TAIL->y + 9);//清楚原来的蛇尾
	creatmap();//绘制地图

	p = s.TAIL;

	while (p->PRE != NULL)
	{
		p->x = p->PRE->x;
		p->y = p->PRE->y;
		p = p->PRE;
	}

	switch (s.DIRECTION) {//蛇头变换方向
	case SNAKE_UP:s.HEAD->y -= 10; break;
	case SNAKE_LEFT:s.HEAD->x -= 10; break;
	case SNAKE_DOWN:s.HEAD->y += 10; break;
	case SNAKE_RIGHT:s.HEAD->x += 10; break;
	default:break;
	}
}


void speed() {
	Sleep(rank);
}


int snake_life() {
	int x, y;
	x = s.HEAD->x;
	y = s.HEAD->y;
	if (x < 20 || x > 600 || y > 440 || y < 50)//蛇头出界,蛇死亡
		s.SANKE_LIVE = 0;
	if (biteSelf() == 1)//蛇吃到自己蛇死亡
		s.SANKE_LIVE = 0;
	return s.SANKE_LIVE;
} //蛇是否存活


void gameover() {
	int c, x;
	x = 1;
	//musicforgg();
	settextstyle(50, 0, _T("宋体"));
	settextcolor(BLUE);
	char s1[] = "你死掉了!!!";
	outtextxy(150, 200, s1);
	_getch();
	initgraph(WINDOW_LENGTH, WINDOW_WIDE);
	setbkcolor(BLACK);
	cleardevice();
	settextcolor(YELLOW);
	char s[] = "";
	sprintf(s, "%s%d", "您的分数是:", score_);
	char s3[] = "1.重新挑战";
	char s2[] = "2.退出游戏";
	settextstyle(30, 0, _T("宋体"));
	outtextxy(200, 200, s3);
	outtextxy(200, 250, s2);
	settextstyle(50, 0, _T("宋体"));
	outtextxy(150, 100, s);

	settextcolor(WHITE);
	char s5[] = "按任意键继续。。。";
	char s7[] = "输入有误。。。";
	char s8[] = "谢谢使用!";
	while (x) {
		kbhit();
		c = getch();
		switch (c) {
		case '1':t = 1; x = 0; outtextxy(150, 350, s5);
			score_ = 0; main(); break;//继续生成游戏
		case '2':outtextxy(150, 350, s8); getch(); exit(0);//退出游戏
		default:x = 1; outtextxy(150, 350, s7); break;
		}
	}
}//结束界面

int eatfood() {
	int x1 = s.HEAD->x;
	int y1 = s.HEAD->y;
	int x2 = f.x;
	int y2 = f.y;
	NODE* p;
	if (x1 == x2 && y1 == y2)//蛇的头部和食物重叠
	{
		s.TAIL->NEXT = (NODE*)malloc(sizeof(NODE));//扩大蛇的长度
		s.TAIL->NEXT->x = s.TAIL->x;
		s.TAIL->NEXT->y = s.TAIL->y;
		s.TAIL->NEXT->PRE = s.TAIL;
		s.TAIL->NEXT->NEXT = NULL;
		p = s.TAIL->NEXT;
		s.TAIL = p;//尾部扩展
		s.NODES++;//蛇的长度+1


		f.FOOD_LIVE = 0;
		s.NODES++;
		f.x = 0 - f.x;
		score_ += 10;
		score();
		return 1;
	}
	else
		return 0;
} /食物


void score() {//记录分数
	char str[20];
	settextcolor(YELLOW);//颜色:灰色
	settextstyle(20, 0, _T("宋体"));//大小和字体
	sprintf(str, "得分:%d", score_);//生成字符串
	outtextxy(10, 20, str);
} //分数


void time() {
	char str[20];
	char c[] = "提示:空格键暂停游戏,ESC键结束游戏。";
	time_t t;
	struct tm* lt;
	settextstyle(20, 0, _T("宋体"));//大小和字体
	time(&t);//获取Unix时间戳。
	lt = localtime(&t);//转为时间结构。
	sprintf(str, "时间:%2d:%2d:%2d", lt->tm_hour, lt->tm_min, lt->tm_sec);//生成字符串
	outtextxy(500, 20, str);
	outtextxy(100, 20, c);
}//时间


void meau() {
	int c;
	int x;
	x = 1;
	initgraph(WINDOW_LENGTH, WINDOW_WIDE);
	setbkcolor(BLACK);
	cleardevice();
	settextstyle(50, 0, _T("宋体"));
	settextcolor(YELLOW);
	char s[] = "欢迎来到贪吃蛇!";
	outtextxy(170, 100, s);
	settextstyle(25, 0, _T("宋体"));
	char s1[] = "选择难度:";
	char s2[] = "1.简单模式";
	settextcolor(GREEN);
	outtextxy(250, 240, s2);
	char s3[] = "2.普通模式";
	settextcolor(BLUE);
	outtextxy(250, 280, s3);
	char s4[] = "3.困难模式";
	settextcolor(RED);
	outtextxy(250, 320, s4);
	char s7[] = "输入有误。。。";

	settextcolor(WHITE);
	char s5[] = "按任意键继续。。。";
	while (x)
	{
		kbhit();
		c = getch();
		switch (c)
		{
		case '1':rank = 300; x = 0; tt = 0;
			outtextxy(250, 390, s5);
			break;


		case '2':rank = 200; x = 0; tt = 0;
			outtextxy(250, 390, s5);
			break;


		case '3':rank = 80; x = 0; tt = 0;
			outtextxy(250, 390, s5);
			break;

		default:x = 1; outtextxy(250, 390, s7); break;
		}
	}
	_getch();
} //菜单
void upgrade() {}

void __cdecl fprintf()
{
}


int biteSelf() {
	NODE* self;
	self = s.HEAD->NEXT;
	while (self != NULL)
	{
		if (self->x == s.HEAD->x && self->y == s.HEAD->y)//蛇身坐标与蛇头重合,判断蛇死亡
		{
			return 1;
		}
		self = self->NEXT;
	}
	return(0);
}

void Data()
{
	FILE* pf = fopen("123.txt", "w"); //以只写的方式打开文件
	if (pf == NULL) //打开文件失败
	{
		printf("保存最高得分记录失败\n");
		exit(0);
	}
	fwrite(&score, sizeof(int), 1, pf); //将本局游戏得分写入文件当中
	fclose(pf); //关闭文件
	pf = NULL; //文件指针及时置空
	
}



 

文章来源:https://blog.csdn.net/laocooon/article/details/135247365
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。