热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C++小游戏——Airport'sControlTower1.0.0

这是一个关于机场塔台的小游戏(飞友福利~~),介于目前吾代码水平限制,游戏略显简陋(所以是1.0.0版嘛。。);游戏内部用↑↓(不能用ASDW)和键盘控制(部分地方会有提示);另

这是一个关于机场塔台的小游戏(飞友福利~~),介于目前吾代码水平限制,游戏略显简陋尴尬(所以是 1.0.0 版嘛。。);

游戏内部用↑↓(不能用A S D W)和键盘控制(部分地方会有提示);另外,游戏为了可以记录玩家和得分数据,需要两个附带的子文件difficulty.dat和players.dat(其实是txt文件输入数据后改dat防止熊玩家乱搞的。。),子文件制作很简单,如下图


其实没有子文件也可以玩。。游戏开始会有报错提示。。


游戏粗浅简陋,不排除有小bug,所以如果各位有好的建议可以提出来,以此发展更新版,吾QQ:3476017351;


代码比较简单,DEV—C++5.7.1编译通过,所以注释很少。。。

代码如下

/*
	Airport's Control Tower 1.0.0
	Copyright @ All Rights Reserved
*/
#include
#include
#include
#include
#include
#include
#include

#define output HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE) //控制句柄输出 

#define white SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE) 
#define red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED)
#define blue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE)
#define green SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN)
#define yellow SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN)
//更改字体颜色 
using namespace std;

const short dx=0,dy=0,fx=78,fy=43;//游戏界面的长宽 

struct node{
	short name;         //航班的所属公司名 
	short number;       //航班号 
	double speed;       //速度 
	double height;      //高度 
	double head;        //航向 
	double pos_xx;      //航班在界面的横坐标 
	double pos_yy;      //航班在界面的纵坐标 
	long long appear;   //航班生成时间 
	bool get;           //航班是否飞行或着陆或失事 
}planes[151];
struct edge{
	bool yes;       //判断航班是否正在转向或爬升下降 
	double head;    //航班航向、高度、速度在更改时的缓存 
	double speed;
	double height;
}changing[151];

short mission,difficulty,toplimit;  //关卡,难度,最大允许事故数 
short players,situation;  //玩家名称在record[][]中的位置, 总的玩家名称数量 
short inner[151];    //游戏操控中 →的纵坐标位置缓存 
bool call[1000],con[151],pro[151],controling,lost,successful=true; //为防止航班号重复的判断,航班是否处于操控的判断
char name[10][20];  //玩家名称 
char flight[10][2]={'C','A','C','Z','M','U','H','U','9','C','A','A','U','A','B','A','C','I','M','F'}; //航空公司 
char ccc[151][6]; //输出控制命令缓存 
int length[151],rapid[151]; //控制命令的长度和值 
int record[10][5]; //游戏成绩记录 
int score,mistakes,totplane,arrival; //游戏得分,事故数,航班总数,航班着陆数 
long long begintime; //航班生成时间 

void XY(HANDLE hOut,short x,short y); //句柄 
void initialization(); //初始化 
void start(); //主界面 
void game(); //游戏主程序 
void movement(short i); //控制飞机移动 
void control(int i); //玩家操控 
void scan(short index,int i); //玩家操控输入 
void then(); //游戏结束后清空缓存数据 
void clean1(); //清屏x:25--50 y:18--25;
void clean2(); //清屏x:25--65 y:18--40;
void clean3(); //清屏x:60--77 y:11--34 36--42;
void clean4(); //清屏x:1--59 y:1--42;
void clean5(); //清屏x:1--9 y:36--42;
void introductions(); //游戏简介(写得很少。。) 
void opitions(); //游戏设置 
void records(); //游戏记录 
void login(); //查看玩家
void delet(); //玩家删除 
void registers(); //玩家注册 
void colorful(short index); //更改字体颜色 
void choose(short number); //设置中的项目选择 
int move(short c,short end); //→的移动 

int main()
{
	white;
	initialization();
	start();
	return 0;
}
inline int move(short c,short end)
{
	output;
	char ch;
	short index=c;
	while(1)
	{
		if(kbhit())
		{
			ch=getch();
			if(ch==0x48)
			{
				XY(hOut,25,index);puts(" ");
				if(index==c) index=end;
				else --index;
				XY(hOut,25,index);puts("→");
			}
			else if(ch==0x50)
			{
				XY(hOut,25,index);puts(" ");
				if(index==end) index=c;
				else ++index;
				XY(hOut,25,index);puts("→");
			}
			else if(ch==13)
			{
				XY(hOut,25,index);puts(" ");
				return index;
			}
		}
	}
}
void initialization()
{
	output;
	XY(hOut,dx,dy);puts("╔");
	XY(hOut,fx,dy);puts("╗");
	XY(hOut,dx,fy);puts("╚");
	XY(hOut,fx,fy);puts("╝");
	XY(hOut,2,dy);puts("══════════════════════════════════════");
	XY(hOut,2,fy);puts("══════════════════════════════════════");
	for(short i=1;i<=42;++i)
	{
		XY(hOut,dx,i);
		puts("║");
		XY(hOut,fx,i);
		puts("║");
	}
	XY(hOut,1,1);
	FILE *fp;
	if(fp=fopen("difficulty.dat","r")) fscanf(fp,"%d%d%d",&mission,&difficulty,&toplimit);
	else mission=difficulty=toplimit=1,successful=false;
	if(fp=fopen("players.dat","r"))
	{
		fscanf(fp,"%d%d\n",&players,&situation);
		for(short i=0;i53||planes[i].pos_xx<1||planes[i].pos_yy>42||planes[i].pos_yy<1)
		{
			clean5();
			red;
			XY(hOut,64,5);puts("You lost");
			XY(hOut,64,6);puts("a plane!");
			score-=2000;
			++mistakes;
			green;
			planes[i].get=false;
			lost=true;
			if(mistakes>=toplimit)
			{
				XY(hOut,25,20);puts("you've made so many mistakes!");
				XY(hOut,27,21);puts("hope you can do better next time.");
				XY(hOut,27,22);printf("your final score : %d",score);
				Sleep(2000);
				then();
				start();
			}
		}
		else if((int)planes[i].pos_yy==20||(int)planes[i].pos_yy==21)
		{
			white;
			XY(hOut,22,20);puts("┄┄┄━━━");
			XY(hOut,22,21);puts("┄┄┄━━━");
			green;
			if((int)planes[i].pos_xx==20&&(int)planes[i].height<1000&&((int)planes[i].head>340||(int)planes[i].head<20))
			{
				score+=1000;
				XY(hOut,64,5);puts("A plane");
				XY(hOut,64,6);puts("landed :-)");
				planes[i].get=false;
				lost=true;
			}
			else
			{
				XY(hOut,(int)(planes[i].pos_xx),(int)(planes[i].pos_yy));
				printf("● %c%c%d",flight[planes[i].name][0],flight[planes[i].name][1],planes[i].number);
			}
		}
		else
		{
			XY(hOut,(int)(planes[i].pos_xx),(int)(planes[i].pos_yy));
			printf("● %c%c%d",flight[planes[i].name][0],flight[planes[i].name][1],planes[i].number);
		}
	}
}
void game()
{
	output;
	short i,index=1,len=62,situ=0,alpha=0;
	char ch,c[20];
	
	for(i=1;i<=42;++i) XY(hOut,60,i),puts("║");
	XY(hOut,60,35);puts("╠════════╣");
	XY(hOut,60,0);puts("╦");
	XY(hOut,60,43);puts("╩");
	XY(hOut,60,10);puts("╠════════╣");
	clean4();
	
	XY(hOut,22,20);puts("┄┄┄━━━");
	XY(hOut,22,21);puts("┄┄┄━━━");
	
	DWORD k=::GetTickCount();
	begintime=k/1000;
	planes[0].appear=begintime;
	totplane=mission*30;
	srand((unsigned)time(NULL));
	long long uptime1,uptime2;
	
	for(i=1;i<=totplane;++i)
	{
		uptime1=planes[i-1].appear;
		uptime2=uptime1+100;
		planes[i].appear=rand()%(uptime2-uptime1)+uptime1;
		planes[i].height=2000+rand()%8000;
		if(planes[i].height>5000)
		{
			planes[i].pos_xx=50.2;
			planes[i].pos_yy=3.2;
			planes[i].head=180;
		}
		else
		{
			planes[i].pos_xx=3.2;
			planes[i].pos_yy=5.2;
			planes[i].head=270;
		}
		planes[i].speed=150+rand()%150;
		planes[i].name=rand()%9;
		while(!call[planes[i].number=1+rand()%998])
			call[planes[i].number]=true;
		planes[i].get=false;
	}
	planes[1].appear=begintime;
	
	DWORD rule=::GetTickCount();
	while(arrivalplanes[i].speed+0.5&&changing[i].speed) planes[i].speed+=0.5;
			else if(changing[i].speedplanes[i].height+3&&changing[i].height) planes[i].height+=3;
			else if(changing[i].heightplanes[i].head+1) planes[i].head+=1;
			else if(changing[i].head=planes[index].appear)
		{
			XY(hOut,(int)planes[index].pos_xx,(int)planes[index].pos_yy);
			printf("● %c%c%d",flight[planes[index].name][0],flight[planes[index].name][1],planes[index].number);
			planes[index].get=true;
			++index;
		}
		if(index==totplane)
		{
			bool eer=true;
			for(int i=index;i>=1;i--)
			if(planes[i].get)
			{
				eer=false;
				break;
			}
			if(eer)
			{
				clean4();
				XY(hOut,25,20);puts("you won the game!");
				XY(hOut,27,21);printf("your final score : %d",score);
				XY(hOut,27,22);printf("the mistakes you've made : %d",mistakes);
				Sleep(2000);
				then();
				start();
			}
		}
		if(k-rule>=1000)
		{
			if(lost=true) lost=false,clean5();
			for(i=1;i<=index;++i) movement(i);
			XY(hOut,62,2);printf("score:%d",score);
			XY(hOut,62,3);printf("mistakes:%d",mistakes);
			rule=k;
		}
		for(i=1;i<=index;++i) if(con[i]) control(i);
		if(!controling)
		if(kbhit())
		{
			yellow;
			ch=getch();
			if(ch!=8&&ch!=13)
			{
				if(len<76)
				{
					XY(hOut,len,36);
					cout<=97&&c[0]<=122) c[0]-=32;
						if(c[1]>=97&&c[1]<=122) c[1]-=32;
						if(c[0]==flight[planes[i].name][0]&&c[1]==flight[planes[i].name][1])
						{
							int rap=0;
							if(planes[i].number<10) rap=c[2]-'0';
							else if(planes[i].number<100) rap=(c[2]-'0')*10+(c[3]-'0');
							else rap=(c[2]-'0')*100+(c[3]-'0')*10+(c[4]-'0');
							if(c[5]==0&&rap==planes[i].number)
							{
								clean3();len=62;
								memset(c,0,sizeof(c));situ=0;
								yellow;
								XY(hOut,69,11);printf("%c%c%d",flight[planes[i].name][0],flight[planes[i].name][1],planes[i].number);
								XY(hOut,66,12);printf("spd :%d",(int)planes[i].speed);
								XY(hOut,66,13);printf("alt :%d",(int)planes[i].height);
								XY(hOut,66,14);printf("head:%d",(int)planes[i].head);
								XY(hOut,66,15);printf("  back");
								con[i]=true;
								cOntroling=true;
								green;
							}
						}
					}
				}
			}
		}
	}
}
void then()
{
	if(score>=record[situation-1][mission-1])
	{
		record[situation-1][mission-1]=score;
		FILE *fp;
		if(fp=fopen("players.dat","r"))
		{
			fprintf(fp,"%d %d\n",players,situation);
			for(short i=0;i='0'&&ch<='9'&&length[i]<=5)
		{
			XY(hOut,62+length[i],36);cout<=100&&rapid[i]<=350) changing[i].speed=rapid[i];
				changing[i].yes=true;
				rapid[i]=0;
				return;
			}
			else if(index==13)
			{
				if(rapid[i]>=300&&rapid[i]<=10000) changing[i].height=rapid[i];
				changing[i].yes=true;
				rapid[i]=0;
				return;
			}
			else
			{
				if(rapid[i]>=0&&rapid[i]<=360) changing[i].head=rapid[i];
				changing[i].yes=true;
				rapid[i]=0;
				return;
			}
		}
		else if(ch==27)
		{
			pro[i]=false;
			cOntroling=false;
			con[i]=false;
			inner[i]=0;
			rapid[i]=0;
			clean3();
			return;
		}
	}
}
inline void clean1()
{
	output;
	for(short i=18;i<=25;++i)
	{
		XY(hOut,25,i);
		puts("                          ");
	}
	XY(hOut,1,1);
}
inline void clean2()
{
	output;
	for(short i=18;i<=40;++i)
	{
		XY(hOut,25,i);
		puts("                                         ");
	}
	XY(hOut,1,1);
}
inline void clean3()
{
	output;
	for(short i=11;i<=34;++i)
	{
		XY(hOut,62,i);
		puts("                ");
	}
	for(short i=36;i<=42;++i)
	{
		XY(hOut,62,i);
		puts("                ");
	}
	XY(hOut,61,42);
}
inline void clean4()
{
	output;
	for(short i=1;i<=42;++i) XY(hOut,2,i),puts("                                                          ");
	XY(hOut,1,1);
}
inline void clean5()
{
	output;
	for(short i=1;i<=9;++i)
	{
		XY(hOut,62,i);
		puts("                ");
	}
	XY(hOut,62,2);
}
void colorful(short index)
{
	switch(index)
	{
		case 1:white;break;
		case 2:red;break;
		case 3:blue;break;
		case 4:green;break;
		case 5:yellow;break;
	}
	initialization();
	opitions();
}
void choose(short number)
{
	if(number==22) clean1(),start();
	clean1();
	output;
	if(number!=21) for(short i=18;i<=22;++i) XY(hOut,30,i),printf("%d",i-17);
	else
	{
		XY(hOut,30,18);puts("white");
		XY(hOut,30,19);puts("red");
		XY(hOut,30,20);puts("blue");
		XY(hOut,30,21);puts("green");
		XY(hOut,30,22);puts("yellow");
	}
	XY(hOut,25,18);puts("→");
	short index=move(18,22)-17;
	switch(number)
	{
		case 18:mission=index;break;
		case 19:difficulty=index;break;
		case 20:toplimit=index;break;
		case 21:colorful(index);break;
	}
	FILE *fp;
	fp=fopen("difficulty.dat","w");
	fprintf(fp,"%d %d %d",mission,difficulty,toplimit);
	fclose(fp);
	opitions();
}
void introductions()
{
	clean1();
	output;
	XY(hOut,30,18);puts("  Control the planes and don't");
	XY(hOut,30,19);puts("make any mistake.");
	XY(hOut,30,20);puts("	Good luck.");
	XY(hOut,30,21);puts("	--press ESC to return.");
	char ch;
	while(1)
	{
		if(kbhit())
		{
			ch=getch();
			if(ch==27) clean2(),start();
		}
	}
}
void opitions()
{
	clean1();
	output;
	if(!successful)
	{
		XY(hOut,20,20);puts("some important files couldn't open correctly!");
		Sleep(1000);
		XY(hOut,20,20);puts("                                             ");
		start();
	}
	XY(hOut,30,18);printf("mission    : %d",mission);
	XY(hOut,30,19);printf("difficulty : %d",difficulty);
	XY(hOut,30,20);printf("toplimit   : %d",toplimit);
	XY(hOut,30,21);printf("color");
	XY(hOut,30,22);puts(" return");
	XY(hOut,25,18);puts("→");
	XY(hOut,1,1);
	choose(move(18,22));
}
void records()
{
	clean1();
	output;
	if(!successful)
	{
		XY(hOut,20,20);puts("some important files couldn't open correctly!");
		Sleep(1000);
		XY(hOut,20,20);puts("                                             ");
		start();
	}
	XY(hOut,30,18);printf("player : %s",name[situation-1]);
	XY(hOut,30,19);printf("  records :");
	for(short i=1;i<=5;++i) XY(hOut,30,19+i),printf("  level %d : %d",i,record[situation-1][i-1]);
	XY(hOut,30,25);printf("  return");
	XY(hOut,25,18);puts("→");
	while(1)
	{
		short c=move(18,25);
		if(c==25) clean1(),start();
		else if(c==18) clean1(),login();
	}
}
void login()
{
	output;
	for(short i=1;i<=players;++i) XY(hOut,30,17+i),printf("%s",name[i-1]);
	XY(hOut,30,18+players);puts("  --register");
	XY(hOut,30,19+players);puts("  --delet");
	XY(hOut,30,20+players);puts("  --return");
	XY(hOut,25,18);puts("→");
	while(1)
	{
		short c=move(18,20+players);
		if(c<=17+players)
		{
			situation=c-17;
			FILE *fp;
			fp=fopen("players.dat","w");
			fprintf(fp,"%d %d\n",players,situation);
			for(short i=0;i 
 



推荐阅读
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
author-avatar
阿都欧巴
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有