首页 > 其他分享 >贪吃蛇游戏代码

贪吃蛇游戏代码

时间:2023-05-31 17:34:07浏览次数:53  
标签:map direction 游戏 int 代码 len squence 贪吃蛇 void


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

const int maxn = 100;
const int n = 20;

struct node
{
	int x, y;
};

int map[maxn][maxn]; // 0表示空格,1表示蛇身,2表示食物,3表示撞死的位置, 4表示蛇头.
node food;
node squence[maxn]; // 蛇的身子的坐标.
int len; // 蛇的长度.
bool eat; // 判断当前事物是否被吃了.
bool gameover; // 游戏是否失败.
int direction; // 判断当前蛇头是朝哪个方向.

void Output(void);
void Move(int direction);
void Move_up(void);
void Move_down(void);
void Move_left(void);
void Move_right(void);

int main()
{
	int i, j;
	double start;
	int gamespeed; // 游戏速度自己调整.
	int timeover;
	int game_times = 1; // 游戏的次数.
	char c = 'x';
	printf("请输入游戏的级别(1到500,1最难,500最简单):\n");
	scanf("%d", &gamespeed);
	gamespeed = gamespeed;
	// 对图的初始化.
	for (i = 0; i <= n + 1; i++)
	{
		for (j = 0; j <= n + 1; j++)
		{
			map[i][j] = 0;
		}
	}
	// 对蛇的初始化.
	for (i = 1; i <= n; i++)
	{
		squence[i].x = 0;
		squence[i].y = 0;
	}
	len = 1; // 蛇的长度为1.
	squence[len].x = 1;
	squence[len].y = 1; // 初始位置在点(1, 1).
	map[1][1] = 4;
	direction = 4; // 默认开始时蛇向右走.
	srand(time(0));
	while (game_times <= 20)
	{
		eat = 0; // 食物还没被吃.
		while (true)
		{ // 随机生出食物的坐标,注意不能与蛇身重合,否则就重新随机产生.
			food.x = rand() % 20 + 1;
			food.y = rand() % 20 + 1;
			if (map[food.x][food.y] == 0)
			{
				break;
			}
		}
		map[food.x][food.y] = 2; // 食物位置.
		system("cls");
		Output();
		// 以下这段半秒钟不按键还取原方向继续前行.
		while (!eat)
		{
			start = clock();
			timeover=1;
			while(!kbhit())
			{ // 说明没有按键.
				if (clock() - start <= gamespeed)
				{ // 如果时间超过游戏时间.
					timeover = 1;
				}
				else
				{
					timeover = 0;
					break;
				}
			}
			if (timeover)
			{ // 说明有按键.
				// 按一次键,可以连取两个
				c = getch();
				c = getch();
				// 以下几行告诉怎样判断用户按的哪个方向键
				if(c==72) direction = 1; // printf("向上");
				if(c==80) direction = 2; // printf("向下");
				if(c==75) direction = 3; // printf("向左");
				if(c==77) direction = 4; // printf("向右");
			}
			Move(direction);
			system("cls");
			if (gameover)
			{
				Output();
				printf("Game Over!!!\n");
				return 0;
			}
			Output();
		}
		game_times++; // 又成功吃到一次食物.
	}
	printf("You win!!!\n");
	return 0;
}

void Move(int direction)
{
	switch (direction)
	{
	case 1 : Move_up(); break;
	case 2 : Move_down(); break;
	case 3 : Move_left(); break;
	default : Move_right();
	}
}

void Output(void)
{
	int i, j;
	for (j = 0; j <= n + 1; j++)
	{
		printf("#");
	}
	printf("\n");
	for (i = 1; i <= n; i++)
	{
		for (j = 0; j <= n + 1; j++)
		{
			if (j == 0 || j == n + 1)
			{
				if (map[i][j] == 3)
				{
					printf("!");
				}
				else
				{
					printf("#");
				}
			}
			else
			{
				if (map[i][j] == 1)
				{
					printf("*");
				}
				else if (map[i][j] == 2)
				{
					printf("@");
				}
				else if (map[i][j] == 3)
				{
					printf("!");
				}
				else if (map[i][j] == 4)
				{
					switch (direction)
					{
					case 1 : printf("%c", 30); break;
					case 2 : printf("%c", 31); break;
					case 3 : printf("%c", 17); break;
					default : printf("%c", 16);
					}
				}
				else
				{
					printf(" ");
				}
			}
		}
		printf("\n");
	}
	for (j = 0; j <= n + 1; j++)
	{
		printf("#");
	}
	printf("\n");
}

void Move_up(void)
{
	int i;
	int x, y;
	if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x + 1)
	{ // 不能移动,则按原来的移动.
		direction = 2; // 按原来的向下移动.
		Move(direction);
		return ;
	}
	// 开始移动.
	x = squence[len].x - 1;
	y = squence[len].y;
	if (x == 0 || map[x][y] == 1)
	{ // 撞到边界或者自己撞到自己.
		map[x][y] = 3;
		gameover = 1;
	}
	if (map[x][y] == 2)
	{ // 说明已经吃到事物.
		map[squence[len].x][squence[len].y] = 1;
		len++;
		squence[len].x = x;
		squence[len].y = y;
		map[x][y] = 4;
		eat = 1;
	}
	else
	{
		map[squence[1].x][squence[1].y] = 0;
		for (i = 1; i <= len - 1; i++)
		{
			squence[i].x = squence[i + 1].x;
			squence[i].y = squence[i + 1].y;
			map[squence[i + 1].x][squence[i + 1].y] = 1;
		}
		squence[len].x = squence[len].x - 1;
		if (gameover)
		{
			map[squence[len].x][squence[len].y] = 3;
		}
		else
		{
			map[squence[len].x][squence[len].y] = 4;
		}
	}
}

void Move_down(void)
{
	int i;
	int x, y;
	if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x - 1)
	{ // 不能移动,则按原来的移动.
		direction = 1; // 按原来的向上移动.
		Move(direction);
		return ;
	}
	// 开始移动.
	x = squence[len].x + 1;
	y = squence[len].y;
	if (x == n + 1 || map[x][y] == 1)
	{ // 撞到边界或者自己撞到自己.
		map[x][y] = 3;
		gameover = 1;
	}
	if (map[x][y] == 2)
	{ // 说明已经吃到事物.
		map[squence[len].x][squence[len].y] = 1;
		len++;
		squence[len].x = x;
		squence[len].y = y;
		map[x][y] = 4;
		eat = 1;
	}
	else
	{
		map[squence[1].x][squence[1].y] = 0;
		for (i = 1; i <= len - 1; i++)
		{
			squence[i].x = squence[i + 1].x;
			squence[i].y = squence[i + 1].y;
			map[squence[i + 1].x][squence[i + 1].y] = 1;
		}
		squence[len].x = squence[len].x + 1;
		if (gameover)
		{
			map[squence[len].x][squence[len].y] = 3;
		}
		else
		{
			map[squence[len].x][squence[len].y] = 4;
		}
	}
}

void Move_left(void)
{
	int i;
	int x, y;
	if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y + 1)
	{ // 不能移动,则按原来的移动.
		direction = 4; // 按原来的向右移动.
		Move(direction);
		return ;
	}
	// 开始移动.
	x = squence[len].x;
	y = squence[len].y - 1;
	if (y == 0 || map[x][y] == 1)
	{ // 撞到边界或者自己撞到自己.
		map[x][y] = 3;
		gameover = 1;
	}
	if (map[x][y] == 2)
	{ // 说明已经吃到事物.
		map[squence[len].x][squence[len].y] = 1;
		len++;
		squence[len].x = x;
		squence[len].y = y;
		map[x][y] = 4;
		eat = 1;
	}
	else
	{
		map[squence[1].x][squence[1].y] = 0;
		for (i = 1; i <= len - 1; i++)
		{
			squence[i].x = squence[i + 1].x;
			squence[i].y = squence[i + 1].y;
			map[squence[i + 1].x][squence[i + 1].y] = 1;
		}
		squence[len].y = squence[len].y - 1;
		if (gameover)
		{
			map[squence[len].x][squence[len].y] = 3;
		}
		else
		{
			map[squence[len].x][squence[len].y] = 4;
		}
	}
}

void Move_right(void)
{
	int i;
	int x, y;
	if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y - 1)
	{ // 不能移动,则按原来的移动.
		direction = 3; // 按原来的向左移动.
		Move(direction);
		return ;
	}
	// 开始移动.
	x = squence[len].x;
	y = squence[len].y + 1;
	if (y == n + 1 || map[x][y] == 1)
	{ // 撞到边界或者自己撞到自己.
		map[x][y] = 3;
		gameover = 1;
	}
	if (map[x][y] == 2)
	{ // 说明已经吃到事物.
		map[squence[len].x][squence[len].y] = 1;
		len++;
		squence[len].x = x;
		squence[len].y = y;
		map[x][y] = 4;
		eat = 1;
	}
	else
	{
		map[squence[1].x][squence[1].y] = 0;
		for (i = 1; i <= len - 1; i++)
		{
			squence[i].x = squence[i + 1].x;
			squence[i].y = squence[i + 1].y;
			map[squence[i + 1].x][squence[i + 1].y] = 1;
		}
		squence[len].y = squence[len].y + 1;
		if (gameover)
		{
			map[squence[len].x][squence[len].y] = 3;
		}
		else
		{
			map[squence[len].x][squence[len].y] = 4;
		}
	}
}



标签:map,direction,游戏,int,代码,len,squence,贪吃蛇,void
From: https://blog.51cto.com/u_16146153/6388661

相关文章

  • Pycharm自定义代码片段
    编写自定义代码片段可以避免编写很多重复的东西;下文以PyCharm2023.1.2为例(已安装Chinese插件);打开设置,参考下图1,2,3,4,5顺序点击菜单;看到如下页面;输入编写,描述,模板文本;点击定义按钮,会显示一个弹窗,选择语言环境,点击除内容区的空白处关闭并保......
  • python版本的“共轭梯度法”算法代码
    在看代码的过程中遇到了共轭梯度法这个概念,对这个算法的数学解释看过几遍,推导看过了,感觉懂了,然后过上一些日子就又忘记了,然后又看了一遍推导,然后过了一些日子也就又忘记了,最后想想这个算法的数学解释就不要再取深究了,毕竟平时也不太会用到,偶尔用到了只要保证代码会写也就OK了。 ......
  • UE4代码编写标准
    #代码编写标准此文为CodingStandard(opensnewwindow)的原创翻译,本文内容版权归原文所有,仅供学习,如需转载望注本文地址,翻译不易,谢谢理解。在Epic,我们有一些编码标准和约定。这个文档不打算讨论或进行改进,相反,它反映了Epic的当前编码标准。编码约定对程序员很重要,因为:软件......
  • golang实现设计模式之抽象工厂模式总结-代码、优缺点、适用场景
    抽象工厂模式也是一种创建型的设计模式,其是在工厂模式的基础上实现更高程度的内聚。我们知道在工厂模式中,一种产品类就需要新建个对应的工厂类生成产品的实例,这会有什么问题呢?虽然工厂模式解决了简单工厂模式不好扩展的问题,实现了OCP,但一种产品就需要新建一个工厂类,比如有10000种......
  • git强制覆盖本地代码
    gitpull强制覆盖本地的代码方式,下面是正确的方法:gitfetch--all然后,你有两个选择:gitreset--hardorigin/master或者如果你在其他分支上:gitreset--hardorigin/<branch_name>说明:gitfetch从远程下载最新的,而不尝试合并或rebase任何东西。然后gitreset将主分支重置为......
  • Mybatis-plus关于代码生成器的使用
    1、添加依赖 2、在test包下创建一个CodeGet类,实现生成代码的功能。注意:全局配置、数据源配置一定要和自己的电脑配置一致! 3、执行CodeGet类中的main方法。打印台有如下图提示字样,即自动生成成功。 4、对比两张图。在wechat文件夹下有controller、entity、mapper、s......
  • MS SQL Server 中的存储过程是一种预编译的代码块,可以接收输入参数并返回输出结果,用于
    MSSQLServer中的存储过程是一种预编译的代码块,可以接收输入参数并返回输出结果,用于完成特定的数据库操作。它们是SQLServer中存储逻辑业务的一种常见方式。下面是存储过程的优势和劣势:优势:更高的性能:存储过程在首次执行时会被编译和优化,然后将编译后的执行计划缓存起来,......
  • Java实战-基于JDK的LRU算法实现、优雅的实现代码耗时统计(Spring AOP、AutoCloseable
    场景Java中基于JDK的LRU算法实现LRU算法-缓存淘汰算法-Leastrecentlyused,最近最少使用算法根据数据的历史访问记录来进行淘汰数据,其核心思想是:如果有数据最近被访问过,那么将来被访问的几率也更高在Java中可以利用LinkedHashMap容器简单实现LRU算法LinkedHashMap底层就是用......
  • 使用神经网络-垃圾邮件检测-LSTM或者CNN(一维卷积)效果都不错【代码有问题,pass】
     fromsklearn.feature_extraction.textimportCountVectorizerimportosfromsklearn.naive_bayesimportGaussianNBfromsklearn.model_selectionimporttrain_test_splitfromsklearnimportmetricsimportmatplotlib.pyplotaspltimportnumpyasnpfromskle......
  • 主流源代码设计工具Github介绍
        众所周知,目前主流的源代码管理工具有Github和TFS这两个软件。而现在也是越来越多人使用和支持Github这个软件,我接下来将会详细介绍Github这款软件。Git是一款免费、开源的分布式版本控制系统,而Github是一个基于git的代码托管平台,付费用户可以将代码可见设置权限,而我们......