栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux 上 C++ 实现 贪吃蛇游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux 上 C++ 实现 贪吃蛇游戏

代码:
#include 
#include 
using namespace std;
int mp[105][105],x=8,y=8,star,n='d',maxn,m,high;
bool flag=false;
int scanKeyboard()
{
    int in;
    struct termios new_settings;
    struct termios stored_settings;
    tcgetattr(0,&stored_settings);
    new_settings=stored_settings;
    new_settings.c_lflag&=(~ICANON);
    new_settings.c_cc[VTIME]=0;
    tcgetattr(0,&stored_settings);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0,TCSANOW,&new_settings);
    in=getchar();
    tcsetattr(0,TCSANOW,&stored_settings);
    return in;
}
int suiji(int minn,int maxn)
{
	random_device seed;
	ranlux48 engine(seed());
	uniform_int_distribution<> distrib(minn, maxn);
    return(distrib(engine));
}
void p()
{
	if(mp[x][y]==-1)
	{
		flag=false;
		star++;
		if(star>high)
		{
			high=star;
		}
	}
	if(!flag)
	{
		for(int i=1;i<=16;i++)
		{
			for(int j=1;j<=16;j++)
			{
				if(mp[i][j]>=1)
				{
					mp[i][j]++;
				}
			}	
		}
		mp[x][y]=1;
	}
	else
	{
		if(x==0)
		{
			x=16;
		}
		if(x==17)
		{
			x=1;
		}
		if(y==0)
		{
			y=16;
		}
		if(y==17)
		{
			y=1;
		}
		if(mp[x][y]>1)
		{
			fstream hi("data/highest.txt");
			hi<=1)
				{
					mp[i][j]++;
				}
			}
		}
		mp[x][y]=1;
	}
	while(!flag)
	{
		int xx=suiji(1,16);
		int yy=suiji(1,16);
		if(mp[xx][yy]==0)
		{
			mp[xx][yy]=-1;
			flag=true;
		}
	}
	system("clear");
	cout<<"得分:"<1)
			{
				cout<<"$ ";
			}
			else if(mp[i][j]==-1)
			{
				cout<<"@ ";
			}
			else
			{
				cout<<"  ";
			}
		}
		cout<<"| "<>high;
	hi.close();
	mp[x][y]=1;
	mp[x][y-1]=2;
	thread t(f);
    t.detach();
	while(1)
	{
		if(n=='w')
		{
			if(x>=1)
			{
				x--;
			}
			p();
		}
		else if(n=='a')
		{
			if(y>=1)
			{
				y--;
			}
			p();
		}
		else if(n=='s')
		{
			if(x<=16)
			{
				x++;
			}
			p();
		}
		else if(n=='d')
		{
			if(y<=16)
			{
				y++;
			}
			p();
		}
		usleep(100000);
	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997047.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号