控制台贪吃蛇

本文介绍了一个使用C++编写的蛇游戏,包含键盘控制、实时路径生成与碰撞检测。通过`conio.h`实现游戏控制,利用`List`数据结构模拟蛇身,展示了如何处理方向切换和食物生成,以及判断蛇身与食物的重叠。游戏还包括了简单的AI元素和边界检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <conio.h>
#include <List>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
struct Pos
{
	int x;
	int y;
};
list <Pos>shenti;
int a[20][20] = {
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
//画面输出
void shuchu(int a[20][20]);
//改变蛇身链表
bool gaibianshenti(char fangxiang,Pos &shiwu,bool &siwang);
//判断食物是否与蛇身重合
bool ischonghe(Pos shiwu);
int main()
{
	//蛇头
	Pos tou = {2,2};
	shenti.push_back(tou);
	
	//初始化方向
	char fangxiang='d';
	//初始化食物点
	Pos shiwu = {0,0};
	//是否死亡
	bool siwang=false;
	int a1[20][20];
	//创建计时器1秒刷新一次
	clock_t start = clock();
	clock_t end;
	char g = ' ';
	while (true)
	{
		//Sleep(2000);
		end = clock();
		//输入方向接收
		if (_kbhit())
		{
			
			g = _getch();
			
			
		}
		if (1 == ((end - start) / 1000))
		{
			
			if (g == 'a' || g == 'w' || g == 's' || g == 'd')
			{
				//cout << g << endl;
				//反方向无法通行
				if (fangxiang == 'a' && g == 'w')
				{
					fangxiang = g;
				}
				else if (fangxiang == 'a' && g == 's')
				{
					fangxiang = g;
				}
				else if (fangxiang == 'w' && g == 'd')
				{
					fangxiang = g;
				}
				else if (fangxiang == 'w' && g == 'a')
				{
					fangxiang = g;
				}
				else if (fangxiang == 's' && g == 'd')
				{
					fangxiang = g;
				}
				else if (fangxiang == 's' && g == 'a')
				{
					fangxiang = g;
				}
				else if (fangxiang == 'd' && g == 's')
				{
					fangxiang = g;
				}
				else if (fangxiang == 'd' && g == 'w')
				{
					fangxiang = g;
				}
				
			}
			//初始化地图
			for (size_t i = 0; i < 20; i++)
			{
				for (size_t j = 0; j < 20; j++)
				{
					a1[i][j] = a[i][j];
				}
			}
			
			//每次产生食物一个
			srand(time(0));
			if (shiwu.x==0&&shiwu.y==0)
			{
				do
				{
					shiwu.x = rand() % 18 + 1;
					shiwu.y = rand() % 18 + 1;
				}while (ischonghe(shiwu));//判断食物是否与蛇身重合
				//cout << shiwu.x << "," << shiwu.y << endl;
			}
			bool chidao=gaibianshenti(fangxiang,shiwu,siwang);//判断是否吃到食物
			//渲染地图
			if (chidao)
			{
				list<Pos>::iterator it = shenti.begin();
				for (it = shenti.begin(); it != shenti.end(); it++)
				{
					a1[it->y][it->x] = 2;
				}
			}
			else
			{
				list<Pos>::iterator it = shenti.begin();
				for (it = shenti.begin(); it != shenti.end(); it++)
				{
					a1[it->y][it->x] = 2;
				}
				a1[shiwu.y][shiwu.x] = 3;
			}
            system("cls");
			//打印
			shuchu(a1);
			if (siwang == true)
			{
				break;
			}
			//system("cls");
			start = end;
		}
	}
	system("cls");
	cout << "游戏结束" << endl;
	return 0;
}


void shuchu(int a[20][20])
{

	for (size_t i = 0; i < 20; i++)
	{
		for (size_t j = 0; j < 20; j++)
		{
			if (a[i][j] == 0)
			{
				cout << " ";
			}
			else if (a[i][j] == 1)
			{
				cout << "#";
			}
			else if (a[i][j] == 2)
			{
				cout << "@";
			}
			else if (a[i][j]==3)
			{
				cout << "*";
			}
			else
			{
				cout << a[i][j];
			}
		}
		cout << endl;
	}
}

bool gaibianshenti(char fangxiang, Pos &shiwu, bool &siwang)
{
	//shiwu.x = 0;
	//shiwu.y = 0;
	if (fangxiang == 'a')
	{
		//cout << fangxiang << endl;
		Pos p;
		p.x=shenti.begin()->x - 1;
		p.y = shenti.begin()->y;
		
		
		//判断是否碰撞身体
		list<Pos>::iterator it = shenti.begin();
		for (it = shenti.begin(); it != shenti.end(); it++)
		{
			if (p.x == it->x && p.y == it->y)
			{
				siwang = true;
			}
		}
		//是否碰撞边框
		for (size_t i = 0; i < 20; i++)
		{
			for (size_t j = 0; j < 20; j++)
			{
				if (a[i][j] == 1)
				{
					if (p.x == j && p.y == i)
					{
						siwang = true;
					}
				}
			}
		}

		shenti.push_front(p);
		if (shiwu.x == p.x && shiwu.y == p.y)
		{
			shiwu.x = 0;
			shiwu.y = 0;
			return true;
		}
		else
		{
			shenti.pop_back();
		}
	}
	else if (fangxiang=='s')
	{
		//cout << fangxiang << endl;
		Pos p;
		p.x = shenti.begin()->x;
		p.y = shenti.begin()->y+1;
		
		list<Pos>::iterator it = shenti.begin();
		for (it = shenti.begin(); it != shenti.end(); it++)
		{
			if (p.x == it->x && p.y == it->y)
			{
				siwang = true;
			}
		}
		for (size_t i = 0; i < 20; i++)
		{
			for (size_t j = 0; j < 20; j++)
			{
				if (a[i][j] == 1)
				{
					if (p.x == j && p.y == i)
					{
						siwang = true;
					}
				}
			}
		}
		shenti.push_front(p);
		if (shiwu.x == p.x && shiwu.y == p.y)
		{
			shiwu.x = 0;
			shiwu.y = 0;
			return true;
		}
		else
		{
			shenti.pop_back();
		}
	}
	else if(fangxiang=='d')
	{
		//cout << fangxiang << endl;
		Pos p;
		p.x = shenti.begin()->x + 1;
		p.y = shenti.begin()->y;
		
		list<Pos>::iterator it = shenti.begin();
		for (it = shenti.begin(); it != shenti.end(); it++)
		{
			if (p.x == it->x && p.y == it->y)
			{
				siwang = true;
			}
		}
		for (size_t i = 0; i < 20; i++)
		{
			for (size_t j = 0; j < 20; j++)
			{
				if (a[i][j] == 1)
				{
					if (p.x == j && p.y == i)
					{
						siwang = true;
					}
				}
			}
		}
		shenti.push_front(p);
		if (shiwu.x == p.x && shiwu.y == p.y)
		{
			shiwu.x = 0;
			shiwu.y = 0;
			return true;
		}
		else
		{
			shenti.pop_back();
		}
	}
	else if(fangxiang=='w')
	{
		//cout << fangxiang << endl;
		Pos p;
		p.x = shenti.begin()->x;
		p.y = shenti.begin()->y-1;
		
		list<Pos>::iterator it = shenti.begin();
		for (it = shenti.begin(); it != shenti.end(); it++)
		{
			if (p.x == it->x && p.y == it->y)
			{
				siwang = true;
			}
		}
		for (size_t i = 0; i < 20; i++)
		{
			for (size_t j = 0; j < 20; j++)
			{
				if (a[i][j] == 1)
				{
					if (p.x == j && p.y == i)
					{
						siwang = true;
					}
				}
			}
		}
		shenti.push_front(p);
		if (shiwu.x == p.x && shiwu.y == p.y)
		{
			shiwu.x = 0;
			shiwu.y = 0;
			return true;
		}
		else
		{
			shenti.pop_back();
		}
	}
	return false;
}
bool ischonghe(Pos shiwu)
{
	list<Pos>::iterator it = shenti.begin();
	for (it = shenti.begin(); it != shenti.end(); it++)
	{
		if (it->x==shiwu.x&&it->y==shiwu.y)
		{
			return true;
		}
	}
	return false;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值