用二维数组写贪吃蛇

用二维数组写贪吃蛇,第1张

用二维数组写贪吃蛇

我们需要一个二维数组来保存地图、蛇的位置食物的位置。

地图空的位置用0表示,食物位置用-1来表示,蛇的位置用1、2、3、。。。来表示,其中1代表蛇头,2代表紧接着蛇头的一节身体,3代表再下一节身体,以此类推。下图是一个5*8的数组,表示了一个5*8的地图,一个长度为5的蛇,和一个用-1表示的食物的位置。

游戏运行流程:

  1. 初始化游戏,包括初始化地图,食物的位置,蛇的位置
  2. 获取玩家输入的控制命令
  3. 根据输入的控制命令移动蛇的位置(左转、右转还是方向不变),并且返回移动的结果(是吃到食物,还是撞到障碍物,还是其他)
  4. 分析移动的结果,如果是吃到了食物,分数加一,撞到了障碍物结束游戏。
  5. 将蛇画到控制台
  6. 回到2过程

代码:

#include
#include
#include
#include
#define SIZE 19 //定义地图大小

void init(int*, int*, int[SIZE][SIZE]);
int getCommand(int);
int move(int, int*, int[SIZE][SIZE]);
void draw(int[SIZE][SIZE]);

int main()
{
	int map[SIZE][SIZE] = { 0 };
	int command, result, length = 0; //length表示蛇的长度
	init(&command, &length, map); //初始化游戏
	while (1)
	{
		command = getCommand(command); //获取玩家输入的命令
		result = move(command, &length, map); //根据命令移动蛇
		if (result == 1) //分析移动的结果
		{
			break;
		}
		else
		{
			draw(map); //将蛇画到控制台
		}
		Sleep(1000);
	}
}

void init(int *command, int *length, int map[SIZE][SIZE])
{
	//初始化初始命令
	*command = 2; //初始命令向下
	//初始化蛇的长度
	*length = 3;
	//初始化蛇的位置
	map[2][1] = 1;
	map[1][1] = 2;
	map[0][1] = 3;
	//初始化食物的位置
	map[3][3] = -1;
}

int getCommand(int command)
{
	int temp = -1;
	if (_kbhit()) //如果有键盘输入
	{
		switch (_getch())
		{
		case 'A': case 'a': temp = 3; break; //表示向左
		case 'S': case 's': temp = 2; break; //表示向下
		case 'D': case 'd': temp = 1; break; //表示向右
		case 'W': case 'w': temp = 0; break; //表示向上
		}
		if (temp != -1 && abs(command - temp) != 2) //蛇不能反向
		{
			command = temp;
		}
	}
	return command;
}

int move(int command, int *length, int map[SIZE][SIZE])
{
	int i, j, head_i, head_j;
	for (i = 0; i < SIZE; i++)
	{
		for (j = 0; j < SIZE; j++)
		{
			if (map[i][j] == *length) //如果是蛇的最后一节身体
			{
				map[i][j] = 0;
			}
			else if (map[i][j] > 1) //如果是蛇中间节的身体
			{
				map[i][j] += 1;
			}
			else if (map[i][j] == 1) //如果是蛇头
			{
				map[i][j] += 1;
				//确定蛇头新的位置head_i和head_j
				switch (command)
				{
				case 0: head_i = i - 1, head_j = j; break;
				case 1: head_i = i, head_j = j + 1; break;
				case 2: head_i = i + 1, head_j = j; break;
				case 3: head_i = i, head_j = j - 1; break;
				}
			}
		}
	}
	if (map[head_i][head_j] == -1) //如果吃到了食物
	{
		map[head_i][head_j] = 1;
		*length = *length + 1; //蛇的长度加一
		//重新生成食物
		while (1)
		{
			i = rand() % SIZE, j = rand() % SIZE;
			if (map[i][j] == 0)
			{
				map[i][j] = -1;
				break;
			}
		}
	}
	//如果撞到了自己或者撞到了墙
	else if (map[head_i][head_j] > 0 || head_i < 0 || head_i == SIZE || head_j < 0 ||head_j == SIZE)
	{
		return 1;
	}
	else
	{
		map[head_i][head_j] = 1;
	}
	return 0;
}

void draw(int map[SIZE][SIZE])
{
	int i, j;
	system("cls"); //清除屏幕
	for (i = 0; i <= SIZE; i++) //输出上边框
	{
		if (i == 0)
		{
			printf("┏");
		}
		else if (i == SIZE)
		{
			printf(" ━━┓");
		}
		else
		{
			printf(" ━");
		}
	}
	printf("n");
	for (i = 0; i < SIZE; i++)
	{
		for (j = 0; j < SIZE; j++)
		{
			if (j == 0) //输出左边框
			{
				printf("┃ ");
			}
			if (map[i][j] == 1) //输出蛇头
			{
				printf("○");
			}
			else if (map[i][j] > 1) //输出蛇身
			{
				printf("●");
			}
			else if (map[i][j] == -1) //输出食物
			{
				printf("★");
			}
			else
			{
				printf("  ");
			}
			if (j == SIZE - 1) //输出右边框
			{
				printf("┃ ");
			}
		}
		printf("n");
	}
	for (i = 0; i <= SIZE; i++) //输出下边框
	{
		if (i == 0)
		{
			printf("┗");
		}
		else if (i == SIZE)
		{
			printf(" ━━┛");
		}
		else
		{
			printf(" ━");
		}
	}
	printf("n");
}

运行效果如下:

 

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

原文地址: https://outofmemory.cn/zaji/5636541.html

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

发表评论

登录后才能评论

评论列表(0条)

保存