贪吃蛇小游戏

在开始写代码前要先考虑好必要的一些属性
//蛇身节点类型
//贪吃蛇本身
//欢迎界面打印
//创建地图
//创建食物
//撞墙或自身检测 等等

蛇身节点类型

对于身的本身我们运用链表来竞选维护

//蛇身节点类型
typedef struct Snakenode
{
  int x;
  int y;
  struct Snakenode* next;
}Snakenode, * pSnakenode;

将该链表的名称与指针重命名方便后续使用

贪吃蛇本身

贪吃蛇本身的属性可以归纳为
指向蛇头的指针
指向食物节点的指针
蛇的方向
蛇的状态
食物分数
总成绩
速度 越短越快
由于元素过多所以可以将其封装为一个结构体便于使用

//贪吃蛇
typedef struct Snake
{
	pSnakenode _pSnake;//指向蛇头的指针
	pSnakenode _pfood;//指向食物节点的指针
	enum DIRECTION _dir;//蛇的方向
	enum GAME_STATUS _sta;//蛇的状态
	int _food_weight;//食物分数
	int _score;//总成绩
	int _sleep_time;//速度 越短越快
}Snake, * pSnake;

也将其重命名方便使用

蛇的状态与方向

蛇的状态与方向无非解释那几种所以我们可以将其用枚举来初始化

enum DIRECTION
{
	up = 1,
	down,
	left,
	right
};

//蛇的状态  正常 撞墙 撞自己 退出
enum GAME_STATUS
{
	ok,
	kill_by_wall,
	kill_by_self,
	end_normal
};

在开始编写之前我们可以将程序大致分为三个部分来进行分别的编写
GameStart();
GameRun();
GameEnd();

在头文件中线#define一些需要重复运用的元素

#define POS_X 24
#define POS_Y 5 
#define WALL L'□'
#define BODY  L'●'
#define FOOD L'☆'

(一)GameStart();

在开始部分中,没有要实现的是开始界面,操作提示,地图的打印以及蛇身的初始化

打印欢迎界面

在vs中用printf打印的信息默认是在左上角(坐标为(0,0,))的位置打印的,为了美观我们可以将光标定位到页面的中心位置进行打印
首先要完成光标的定位

void SetPos(short x, short y)
{
	COORD pos = { x, y };
	HANDLE hOutput = NULL;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}

将光标定位到中间位置时再打印

void WelcomeToGame()
{
	SetPos(40, 15);
	wprintf(L"欢迎来到贪吃蛇⼩游戏");
	SetPos(40, 25);//让按任意键继续的出现的位置好看点
	system("pause");
	system("cls");
	SetPos(25, 12);
	wprintf(L"用方向键操控");
	SetPos(25, 13);
	wprintf(L"加速获得更高分");
	SetPos(40, 25);//让按任意键继续的出现的位置好看点
	system("pause");
	system("cls");
}

打印地图(58x27的大小)

void CreateMap()
{
	int i = 0;
	//上(0,0)-(56, 0)
	SetPos(0, 0);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//下(0,26)-(56, 26)
	SetPos(0, 26);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	//x是0,y从1开始增⻓
	for (i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//x是56,y从1开始增⻓
	for (i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

运用头插法对蛇进行初始化并耿宇初始的一些状态

void InitSnake(pSnake ps)
{
	pSnakenode cur = NULL;
	int i = 0;
	//创建蛇⾝节点,并初始化坐标
	//头插法
	for (i = 0; i < 5; i++)
	{
		//创建蛇⾝的节点
		cur = (pSnakenode)malloc(sizeof(Snakenode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		//设置坐标
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;
		//头插法
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	//打印蛇的⾝体
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps ->_sleep_time = 200;
	ps->_score = 0;
	ps->_sta = ok;
	ps->_dir = right;
	ps->_food_weight = 10;
}

将食物进行初始化,不过食物的位置需要随机所以要运用随机数来帮助初始化

//要运用时间函数才能得到真正的随机值
	srand((unsigned int)time(NULL));

void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;
again:
	//产⽣的x坐标应该是2的倍数,这样才可能和蛇头坐标对⻬。
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	pSnakenode cur = ps->_pSnake;//获取指向蛇头的指针
	//⻝物不能和蛇⾝冲突
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	pSnakenode pFood = (pSnakenode)malloc(sizeof(Snakenode)); //创建⻝物
	if (pFood == NULL)
	{
		perror("CreateFood::malloc()");
		return;
	}
	else
	{
		pFood->x = x;
		pFood->y = y;
		SetPos(pFood->x, pFood->y);
		wprintf(L"%c", FOOD);
		ps->_pfood = pFood;
	}
}

将这些函数组装到GameStarte里

void GameStart(pSnake ps)
{
	//设置控制台窗⼝的⼤⼩,30⾏,100列
	//mode 为DOS命令
	system("mode con cols=100 lines=30");
	//设置cmd窗⼝名称
	system("title 贪吃蛇");
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//影藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态
	//打印欢迎界⾯
	WelcomeToGame();
	//打印地图
	CreateMap();
	InitSnake(ps);
	//创造第⼀个⻝物
	CreateFood(ps);
}

在打印过程中隐藏光标能提供更高的完成度

(二)GameRun();

在这个函数当中需要进行蛇的移动以及用键盘上的键对其进行操作
以及蛇可能会遇到的情况

在地图旁边打印一些提示信息

void PrintHelpInfo()
{
	//打印提⽰信息
	SetPos(64, 15);
	wprintf(L"不能穿墙不能咬到自己\n");
	SetPos(64, 16);
	wprintf(L"用方向键控制.");
	SetPos(64, 17);
	wprintf(L"F3加速 F4减速");
	SetPos(64, 18);
	wprintf(L"esc退出 空格暂停.");
	
}

运用空格来进行暂停

void pause()//暂停
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

来判断下一个位置是不是食物

int NextIsFood(pSnakenode psn, pSnake ps)
{
	return (psn->x == ps->_pfood->x) && (psn->y == ps->_pfood->y);
}

如果是食物

void EatFood(pSnakenode psn, pSnake ps)
{
	//头插法
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;
	pSnakenode cur = ps->_pSnake;
	//打印蛇
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps->_score+= ps->_food_weight;
	free(ps->_pfood);
	//吃玩后重新生成食物
	CreateFood(ps);
}

如果不是食物

void NoFood(pSnakenode psn, pSnake ps)
{
	//头插法
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;
	pSnakenode cur = ps->_pSnake;
	//打印蛇
	while (cur->next->next != NULL)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	//最后⼀个位置打印空格,然后释放节点
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}

如果撞到墙或自己的情况

int KillByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_sta = kill_by_wall;
		return 1;
	}
	return 0;
}
int KillBySelf(pSnake ps)
{
	pSnakenode cur = ps->_pSnake->next;
	while (cur)
	{
		if ((ps->_pSnake->x == cur->x)
			&& (ps->_pSnake->y == cur->y))
		{
			ps->_sta= kill_by_self;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}

蛇的移动

void SnakeMove(pSnake ps)
{
	//创建下⼀个节点
	pSnakenode pNextNode = (pSnakenode)malloc(sizeof(Snakenode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	//确定下⼀个节点的坐标,下⼀个节点的坐标根据,蛇头的坐标和⽅向确定
	switch (ps->_dir)
	{
	case up:
	{
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
	}
	break;
	case down:
	{
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
	}
	break;
	case left:
	{
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
	}
	break;
	case right:
	{
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
	}
	break;
	}
	//如果下⼀个位置就是⻝物
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else//如果没有⻝物
	{
		NoFood(pNextNode, ps);
	}
	KillByWall(ps);
	KillBySelf(ps);
}

对GameRun()进行结合

#define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )
//对于键盘的状况的判断


void GameRun(pSnake ps)
{
	//打印右侧帮助信息
	PrintHelpInfo();
	do
	{
		SetPos(64, 10);
		printf("得分:%d ", ps->_score);
		printf("每个食物得分:%d分", ps->_food_weight);
		if (KEY_PRESS(VK_UP) && ps->_dir != down)
		{
			ps->_dir = up;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != up)
		{
			ps->_dir = down;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != right)
		{
			ps->_dir = left;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != left)
		{
			ps->_dir = right;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps ->_sta = end_normal;
			break;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_sleep_time >= 50)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_sleep_time < 350)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
				if (ps->_sleep_time == 350)
				{
					ps->_food_weight = 1;
				}
			}
		}
		//蛇每次⼀定之间要休眠的时间,时间短,蛇移动速度就快
		Sleep(ps ->_sleep_time);
		SnakeMove(ps);
	} while (ps->_sta == ok);
}

(三)GameEnd();

根据之前蛇的状态来决定最后的结果

void GameEnd(pSnake ps)
{
	pSnakenode cur = ps->_pSnake;
	SetPos(24, 12);
	switch (ps->_sta)
	{
	case end_normal:
		wprintf(L"您主动退出游戏\n");
		break;
	case kill_by_self:
		wprintf(L"您撞上⾃⼰了 ,游戏结束!\n");
		break;
	case kill_by_wall:
		wprintf(L"您撞墙了,游戏结束!\n");
		break;
	}
	//释放蛇⾝的节点
	while (cur)
	{
		pSnakenode del = cur;
		cur = cur->next;
		free(del);
	}
}

最后对主函数开始编写

void test()
{
	int ch = 0;
	srand((unsigned int)time(NULL));
	do
	{
		Snake snake = { 0 };
		GameStart(&snake);
		GameRun(&snake);
		GameEnd(&snake);
		SetPos(20, 15);
		printf("再来⼀局吗?(Y/N):");
		ch = getchar();
		getchar();//清理\n
	} while (ch == 'Y' || ch == 'y');
	SetPos(0, 27);
}
int main()
{
	//修改当前地区为本地模式,为了⽀持中⽂宽字符的打印
	setlocale(LC_ALL, "");
	//测试逻辑
	test();
	return 0;
}

最后的总代码
snake.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"


//设置光标的坐标
void SetPos(short x, short y)
{
	COORD pos = { x, y };
	HANDLE hOutput = NULL;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}
void WelcomeToGame()
{
	SetPos(40, 15);
	wprintf(L"欢迎来到贪吃蛇⼩游戏");
	SetPos(40, 25);//让按任意键继续的出现的位置好看点
	system("pause");
	system("cls");
	SetPos(25, 12);
	wprintf(L"用方向键操控");
	SetPos(25, 13);
	wprintf(L"加速获得更高分");
	SetPos(40, 25);//让按任意键继续的出现的位置好看点
	system("pause");
	system("cls");
}
void CreateMap()
{
	int i = 0;
	//上(0,0)-(56, 0)
	SetPos(0, 0);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//下(0,26)-(56, 26)
	SetPos(0, 26);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	//x是0,y从1开始增⻓
	for (i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//x是56,y从1开始增⻓
	for (i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}
void InitSnake(pSnake ps)
{
	pSnakenode cur = NULL;
	int i = 0;
	//创建蛇⾝节点,并初始化坐标
	//头插法
	for (i = 0; i < 5; i++)
	{
		//创建蛇⾝的节点
		cur = (pSnakenode)malloc(sizeof(Snakenode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		//设置坐标
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;
		//头插法
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	//打印蛇的⾝体
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps ->_sleep_time = 200;
	ps->_score = 0;
	ps->_sta = ok;
	ps->_dir = right;
	ps->_food_weight = 10;
}
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;
again:
	//产⽣的x坐标应该是2的倍数,这样才可能和蛇头坐标对⻬。
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	pSnakenode cur = ps->_pSnake;//获取指向蛇头的指针
	//⻝物不能和蛇⾝冲突
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	pSnakenode pFood = (pSnakenode)malloc(sizeof(Snakenode)); //创建⻝物
	if (pFood == NULL)
	{
		perror("CreateFood::malloc()");
		return;
	}
	else
	{
		pFood->x = x;
		pFood->y = y;
		SetPos(pFood->x, pFood->y);
		wprintf(L"%c", FOOD);
		ps->_pfood = pFood;
	}
}
void PrintHelpInfo()
{
	//打印提⽰信息
	SetPos(64, 15);
	wprintf(L"不能穿墙不能咬到自己\n");
	SetPos(64, 16);
	wprintf(L"用方向键控制.");
	SetPos(64, 17);
	wprintf(L"F3加速 F4减速");
	SetPos(64, 18);
	wprintf(L"esc退出 空格暂停.");
	
}
void pause()//暂停
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
//pSnakeNode psn 是下⼀个节点的地址
//pSnake ps 维护蛇的指针
int NextIsFood(pSnakenode psn, pSnake ps)
{
	return (psn->x == ps->_pfood->x) && (psn->y == ps->_pfood->y);
}
//pSnakeNode psn 是下⼀个节点的地址
//pSnake ps 维护蛇的指针
void EatFood(pSnakenode psn, pSnake ps)
{
	//头插法
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;
	pSnakenode cur = ps->_pSnake;
	//打印蛇
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps->_score+= ps->_food_weight;
	free(ps->_pfood);
	CreateFood(ps);
}
//pSnakeNode psn 是下⼀个节点的地址
//pSnake ps 维护蛇的指针
void NoFood(pSnakenode psn, pSnake ps)
{
	//头插法
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;
	pSnakenode cur = ps->_pSnake;
	//打印蛇
	while (cur->next->next != NULL)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	//最后⼀个位置打印空格,然后释放节点
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}
//pSnake ps 维护蛇的指针
int KillByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_sta = kill_by_wall;
		return 1;
	}
	return 0;
}
int KillBySelf(pSnake ps)
{
	pSnakenode cur = ps->_pSnake->next;
	while (cur)
	{
		if ((ps->_pSnake->x == cur->x)
			&& (ps->_pSnake->y == cur->y))
		{
			ps->_sta= kill_by_self;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}
void SnakeMove(pSnake ps)
{
	//创建下⼀个节点
	pSnakenode pNextNode = (pSnakenode)malloc(sizeof(Snakenode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	//确定下⼀个节点的坐标,下⼀个节点的坐标根据,蛇头的坐标和⽅向确定
	switch (ps->_dir)
	{
	case up:
	{
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
	}
	break;
	case down:
	{
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
	}
	break;
	case left:
	{
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
	}
	break;
	case right:
	{
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
	}
	break;
	}
	//如果下⼀个位置就是⻝物
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else//如果没有⻝物
	{
		NoFood(pNextNode, ps);
	}
	KillByWall(ps);
	KillBySelf(ps);
}
void GameStart(pSnake ps)
{
	//设置控制台窗⼝的⼤⼩,30⾏,100列
	//mode 为DOS命令
	system("mode con cols=100 lines=30");
	//设置cmd窗⼝名称
	system("title 贪吃蛇");
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//影藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态
	//打印欢迎界⾯
	WelcomeToGame();
	//打印地图
	CreateMap();
	InitSnake(ps);
	//创造第⼀个⻝物
	CreateFood(ps);
}
void GameRun(pSnake ps)
{
	//打印右侧帮助信息
	PrintHelpInfo();
	do
	{
		SetPos(64, 10);
		printf("得分:%d ", ps->_score);
		printf("每个食物得分:%d分", ps->_food_weight);
		if (KEY_PRESS(VK_UP) && ps->_dir != down)
		{
			ps->_dir = up;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != up)
		{
			ps->_dir = down;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != right)
		{
			ps->_dir = left;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != left)
		{
			ps->_dir = right;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps ->_sta = end_normal;
			break;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_sleep_time >= 50)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_sleep_time < 350)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
				if (ps->_sleep_time == 350)
				{
					ps->_food_weight = 1;
				}
			}
		}
		//蛇每次⼀定之间要休眠的时间,时间短,蛇移动速度就快
		Sleep(ps ->_sleep_time);
		SnakeMove(ps);
	} while (ps->_sta == ok);
}
void GameEnd(pSnake ps)
{
	pSnakenode cur = ps->_pSnake;
	SetPos(24, 12);
	switch (ps->_sta)
	{
	case end_normal:
		wprintf(L"您主动退出游戏\n");
		break;
	case kill_by_self:
		wprintf(L"您撞上⾃⼰了 ,游戏结束!\n");
		break;
	case kill_by_wall:
		wprintf(L"您撞墙了,游戏结束!\n");
		break;
	}
	//释放蛇⾝的节点
	while (cur)
	{
		pSnakenode del = cur;
		cur = cur->next;
		free(del);
	}
}

snake.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <locale.h>
#include <time.h> 

#define POS_X 24
#define POS_Y 5 
#define WALL L'□'
#define BODY  L'●'
#define FOOD L'☆'
#define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )

//类型声明
//蛇的方向
enum DIRECTION
{
	up = 1,
	down,
	left,
	right
};

//蛇的状态  正常 撞墙 撞自己 退出
enum GAME_STATUS
{
	ok,
	kill_by_wall,
	kill_by_self,
	end_normal
};

//蛇身节点类型
typedef struct Snakenode
{
	int x;
	int y;
	struct Snakenode* next;
}Snakenode, * pSnakenode;

//贪吃蛇
typedef struct Snake
{
	pSnakenode _pSnake;//指向蛇头的指针
	pSnakenode _pfood;//指向食物节点的指针
	enum DIRECTION _dir;//蛇的方向
	enum GAME_STATUS _sta;//蛇的状态
	int _food_weight;//食物分数
	int _score;//总成绩
	int _sleep_time;//速度 越短越快
}Snake, * pSnake;

//函数声明
//光标定位
void SetPos(short x, short y);
//游戏初始化
void GameStart(pSnake);

//欢迎界面打印
void WelcomeToGame();

//创建地图
void GreateMap();

//创建蛇
void InitSnake(pSnake ps);

//创建食物
void CreateFood(pSnake ps);

//运行
void GameRun(pSnake ps);

//走一步
void SnakeMove(pSnake ps);

//检测下一个坐标是不是食物
int NextIsFood(pSnakenode pnextnode, pSnake ps);

//吃掉食物
void EatFood(pSnakenode pnextnode, pSnake ps);
//蛇的移动
void SnakeMove(pSnake ps);
//下一个位置不是食物
void NoFood(pSnakenode pnextnode, pSnake ps);

//撞墙检测
int KillByWall(pSnake ps);
//撞⾃⾝检测
int KillBySelf(pSnake ps);

//结束(善后--释放节点)
void GameEnd(pSnake ps);

//暂停响应
void pause();
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值