一个小时完成贪吃蛇

突然想玩贪吃蛇,所以花了大概个小时写了一个,还可以原地转掉头。主要大概的想法就是,定义一个图片数组,然后用打印,清屏的方法实现显示,并且不断进行按键检测,准备用、c++语法写,但是C风格很严重。希望大神批评指正!

#include 
#include 
#include 

using namespace std;

//按键检测宏
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)

//图像高度,宽度
#define hight 30
#define weight 30
//图像数组
char image[hight][weight];
//小蛇行进方向
char fangxaing = 'D';
//记录得分
int fenshu = 0;
//食物结构体
typedef struct food {
	int x;
	int y;
	bool statu;
}*Food;

//双向链表节点,保存小蛇坐标
typedef struct node {
	node *next;
	node *front;
	int x;
	int y;
}*Node;
//保存小蛇头,尾节点
typedef struct snack {
	Node head;
	Node tail;
	int length;
}*Snack;
//生成最初食物坐标
bool Foodinit(Food &fd1)
{
	fd1 = (Food)malloc(sizeof(food));
	if (!fd1)return false;
	fd1->statu = true;
	fd1->x = (rand() % (weight - 2) + 1);
	fd1->y = (rand() % (hight - 2) + 1);
	image[fd1->y][fd1->x] = '@';
	return true;
}
//判断食物是否被吃下
bool eaten(Snack &SQ, Food &fd)
{
	if (SQ->head->x == fd->x&&SQ->head->y == fd->y)
	{
		fd->statu = false;
		return true;
	}
	return false;
}
//食物吃下后重新创建坐标
void creatfood(Food &fd)
{
	fd->statu = true;
	fd->x = (rand() % (weight - 2) + 1);
	fd->y = (rand() % (hight - 2) + 1);
	image[fd->y][fd->x] = '@';
}
//小蛇坐标初始化
void snackinit(Snack &SQ)
{
	Node p = (Node)malloc(sizeof(node));
	if (!p)return;
	Node q = (Node)malloc(sizeof(node));
	if (!q)return;
	p->next = q;
	q->next = NULL;
	q->front = p;
	p->front = NULL;
	p->x = weight / 2;
	p->y = hight / 2;
	q->x = weight / 2-1;
	q->y = hight / 2;

	SQ = (Snack)malloc(sizeof(snack));
	SQ->head = p;
	SQ->tail = q;
	SQ->length = 2;

	//蛇图加入图像
	image[p->y][p->x] = '*';
	image[q->y][q->x] = '*';

	image[SQ->head->y][SQ->head->x] = 'Q';
}
//图像初始化
void imageinit()
{
	for (int i = 0; i < hight; i++)
	{
		for (int j = 0; j < weight; j++)
		{
			image[i][j] = ' ';
		}
	}
	for (int i = 0; i < weight; i++)
	{
		image[0][i] = '#';
		image[hight-1][i] = '#';
	}
	for (int i = 0; i < hight; i++)
	{
		image[i][0] = '#';
		image[i][hight - 1] = '#';
	}
}
//按键按下后改变记录方向变量值
void changvalue(char ch)
{
	if (KEY_DOWN(ch))
	{
		fangxaing = ch;
	}
}

//按键检测
void keyscan()
{
	changvalue('W');
	changvalue('A');
	changvalue('S');
	changvalue('D');
}
//移动小蛇,并将小蛇的坐标导入图片
void move(Snack &SQ,Food &fd)
{
	Node p = (Node)malloc(sizeof(node));
	if (!p)return;
	if (fangxaing == 'W')
	{
		p->x = SQ->head->x;
		p->y = (SQ->head->y) - 1;
		
		p->next = SQ->head;

		image[SQ->head->y][SQ->head->x] = '*';

		SQ->head->front = p;
		p->front = NULL;
		SQ->head = p;

		image[SQ->head->y][SQ->head->x] = 'Q';

		if (!eaten(SQ, fd))
		{
			Node q;
			q = SQ->tail;
			SQ->tail = SQ->tail->front;
			SQ->tail->next = NULL;

			image[q->y][q->x] = ' ';
			free(q);
		}
		else
		{
			creatfood(fd);
			fenshu += 10;
		}
	}
	if (fangxaing == 'A')
	{
		p->x = (SQ->head->x) - 1;
		p->y = (SQ->head->y);

		p->next = SQ->head;

		image[SQ->head->y][SQ->head->x] = '*';

		SQ->head->front = p;
		p->front = NULL;
		SQ->head = p;

		image[SQ->head->y][SQ->head->x] = 'Q';

		if (!eaten(SQ, fd))
		{
			Node q;
			q = SQ->tail;
			SQ->tail = SQ->tail->front;
			SQ->tail->next = NULL;

			image[q->y][q->x] = ' ';
			free(q);
		}
		else
		{
			creatfood(fd);
			fenshu += 10;
		}
	}
	if (fangxaing == 'S')
	{
		p->x = SQ->head->x;
		p->y = (SQ->head->y) + 1;

		p->next = SQ->head;

		image[SQ->head->y][SQ->head->x] = '*';

		SQ->head->front = p;
		p->front = NULL;
		SQ->head = p;

		image[SQ->head->y][SQ->head->x] = 'Q';

		if (!eaten(SQ, fd))
		{
			Node q;
			q = SQ->tail;
			SQ->tail = SQ->tail->front;
			SQ->tail->next = NULL;

			image[q->y][q->x] = ' ';
			free(q);
		}
		else
		{
			creatfood(fd);
			fenshu += 10;
		}
	}

	if (fangxaing == 'D')
	{
		p->x = (SQ->head->x) + 1;
		p->y = (SQ->head->y);

		p->next = SQ->head;

		image[SQ->head->y][SQ->head->x] = '*';

		SQ->head->front = p;
		p->front = NULL;
		SQ->head = p;

		image[SQ->head->y][SQ->head->x] = 'Q';

		if (!eaten(SQ, fd))
		{
			Node q;
			q = SQ->tail;
			SQ->tail = SQ->tail->front;
			SQ->tail->next = NULL;

			image[q->y][q->x] = ' ';
			free(q);
		}
		else
		{
			creatfood(fd);
			fenshu += 10;
		}
	}
}
//判断小蛇是否死亡
bool death(Snack &SQ)
{
	if (image[SQ->head->x][SQ->head->y] == '#')
	{
		return true;
	}
	else 
		return false;
}
//将数组中的图像显示在窗口上
void display()
{
	for (int i = 0; i < hight; i++)
	{
		for (int j = 0; j < weight; j++)
		{
			cout << image[i][j];
		}
		if (i == 4)
		{
			cout << "    作者:XXX";
		}
		if (i == 5)
		{
			cout << "    XXX年XX月XX日";
		}
		cout << endl;
	}
}

int main()
{
	Snack SQ;
	Food fd;
	//初始化边框
	imageinit();
	//初始化食物
	Foodinit(fd);
	//小蛇坐标初始化
	snackinit(SQ);
	while (1)
	{
		//显示image中的内容
		display();
		Sleep(100);
		//扫面按键
		keyscan();
		//移动小蛇
		move(SQ,fd);
		//清除屏幕显示
		system("cls");
		//如果小蛇死亡就退出程序
		if (death(SQ))
			break;
	}
	cout << endl << "Game Over !" << endl
		 << "您的得分是:" << fenshu << endl;
	return 0;
}

你可能感兴趣的:(C++)