C++小游戏——Airport's Control Tower 1.0.0

来源:互联网 发布:淘宝企业店铺优势缺点 编辑:程序博客网 时间:2024/06/03 23:06

这是一个关于机场塔台的小游戏(飞友福利~~),介于目前吾代码水平限制,游戏略显简陋尴尬(所以是 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.0Copyright @ All Rights Reserved*/#include<stdio.h>#include<windows.h>#include<time.h>#include<math.h>#include<conio.h>#include<iostream>#include<cstdlib>#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;i<players;++i) fscanf(fp,"%s%d%d%d%d%d\n",name[i],&record[i][0],&record[i][1],&record[i][2],&record[i][3],&record[i][4]);fclose(fp);}else successful=false;if(!successful){XY(hOut,20,20);puts("some important files couldn't open correctly!");Sleep(1000);XY(hOut,20,20);puts("                                             ");}}void start(){output;XY(hOut,30,18);puts("new game");XY(hOut,30,19);puts("introductions");XY(hOut,30,20);puts("opitions");XY(hOut,30,21);puts("records");XY(hOut,30,22);puts("exit");XY(hOut,25,18);puts("→");XY(hOut,1,1);switch(move(18,22)){case 18:game();break;case 19:introductions();break;case 20:opitions();break;case 21:records();break;case 22:exit(0);}}void movement(short i){output;double face_x,face_y;if(planes[i].get){XY(hOut,(int)(planes[i].pos_xx),(int)(planes[i].pos_yy));puts("        ");face_x=cos(planes[i].head/57.3);face_y=sin(planes[i].head/57.3);planes[i].pos_xx+=face_x*planes[i].speed/500;planes[i].pos_yy-=face_y*planes[i].speed/1000;if(planes[i].pos_xx>53||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(arrival<totplane){green;Sleep(100); DWORD k=::GetTickCount();for(i=1;i<=index;++i) if(changing[i].yes){if(changing[i].speed>planes[i].speed+0.5&&changing[i].speed) planes[i].speed+=0.5;else if(changing[i].speed<planes[i].speed-0.5&&changing[i].speed) planes[i].speed-=0.5;else if(changing[i].speed!=planes[i].speed&&changing[i].speed) planes[i].speed=changing[i].speed;if(changing[i].height>planes[i].height+3&&changing[i].height) planes[i].height+=3;else if(changing[i].height<planes[i].height-3&&changing[i].height) planes[i].height-=3;else if(changing[i].height!=planes[i].height&&changing[i].height) planes[i].height=changing[i].height;if(changing[i].head>planes[i].head+1) planes[i].head+=1;else if(changing[i].head<planes[i].head-1&&changing[i].head) planes[i].head-=1;else if(changing[i].head!=planes[i].head&&changing[i].head) planes[i].head=changing[i].head;if(changing[i].speed==planes[i].speed&&changing[i].height==planes[i].height&&changing[i].head==planes[i].head)changing[i].yes=false;}if((k/1000)>=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<<ch;++len;c[situ]=ch;++situ;}}if(ch==8){XY(hOut,--len,36);puts(" ");c[situ]=0;--situ;}if(ch==13){for(i=1;i<=index;++i){if(planes[i].get){if(c[0]>=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<players;++i) fprintf(fp,"%s %d %d %d %d %d \n",name[i],&record[i][0],&record[i][1],&record[i][2],&record[i][3],&record[i][4]);fclose(fp);}}output;XY(hOut,78,35);puts("║");XY(hOut,78,10);puts("║");XY(hOut,60,0);puts("═");XY(hOut,60,43);puts("═");for(int i=1;i<=42;++i) XY(hOut,1,i),puts("                                                                             ");memset(planes,0,sizeof(planes));memset(call,false,sizeof(call));memset(con,false,sizeof(con));memset(pro,false,sizeof(pro));lost=false;controling=false;memset(ccc,0,sizeof(ccc));memset(length,0,sizeof(length));memset(rapid,0,sizeof(rapid));}void control(int i){output;yellow;if(!inner[i]) inner[i]=12,XY(hOut,63,inner[i]),puts("→");char ch;if(pro[i]) scan(inner[i],i);else if(kbhit()){ch=getch();if(ch==13){if(inner[i]==15){pro[i]=false;controling=false;con[i]=false;inner[i]=0;rapid[i]=0;clean3();return;}XY(hOut,62,36);pro[i]=true;scan(inner[i],i);return;}else if(ch==0x48){XY(hOut,63,inner[i]);puts(" ");if(inner[i]==12) inner[i]=15;else --inner[i];XY(hOut,63,inner[i]);puts("→");}else if(ch==0x50){XY(hOut,63,inner[i]);puts(" ");if(inner[i]==15) inner[i]=12;else ++inner[i];XY(hOut,63,inner[i]);puts("→");}}green;}void scan(short index,int i){output;yellow;char ch;if(kbhit()){ch=getch();if(ch!=13&&ch!=8&&ch>='0'&&ch<='9'&&length[i]<=5){XY(hOut,62+length[i],36);cout<<ch;ccc[i][length[i]]=ch;++length[i];}else if(ch==8&&length[i]){XY(hOut,61+length[i],36);cout<<" ";ccc[i][length[i]]=0;--length[i];}else if(ch==13){pro[i]=false;controling=false;con[i]=false;inner[i]=0;green;clean3();for(short j=0;j<length[i];++j){rapid[i]*=10;rapid[i]+=(ccc[i][j]-'0');}length[i]=0;memset(ccc[i],0,sizeof(ccc[i]));if(index==12){if(rapid[i]>=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<players;++i) fprintf(fp,"%s %d %d %d %d %d \n",name[i],record[i][0],record[i][1],record[i][2],record[i][3],record[i][4]);fclose(fp);clean2();records();}else if(c==18+players) clean2(),registers();else if(c==19+players) clean2(),delet();else if(c==20+players) clean2(),records();}}void delet(){output;for(short i=0;i<players;++i) XY(hOut,30,18+i),printf("%s",name[i]);XY(hOut,30,18+players);puts("  --return");XY(hOut,25,18);puts("→");short c=move(18,18+players)-17;if(c==players+1) clean2(),records();clean2();XY(hOut,30,18);puts("Are your sure to delet this player?");XY(hOut,30,19);puts("  --Press Enter to confirm");XY(hOut,30,20);puts("  --Press ESC to cancle");char ch;while(1){if(kbhit()){ch=getch();if(ch==27) clean2(),records();else if(ch==13){if(situation==c||situation==players) situation=1;FILE *fp;fp=fopen("players.dat","w");fprintf(fp,"%d %d\n",players-1,situation);for(short i=0;i<players;++i) if(i!=c-1) fprintf(fp,"%s %d %d %d %d %d \n",name[i],record[i][0],record[i][1],record[i][2],record[i][3],record[i][4]);--players;fclose(fp);fp=fopen("players.dat","r");memset(name,0,sizeof(name));memset(record,0,sizeof(record));fscanf(fp,"%d%d\n",&players,&situation);for(short i=0;i<players;++i) fscanf(fp,"%s%d%d%d%d%d\n",name[i],&record[i][0],&record[i][1],&record[i][2],&record[i][3],&record[i][4]);fclose(fp);clean2();records();} }}}void registers(){output;XY(hOut,30,18);puts("Please write your name in 20 words:");XY(hOut,30,20);puts("  --press Enter to confirm");XY(hOut,30,21);puts("  --press ESC to return");short lenth=0,index=19;char ch;while(1){if(kbhit()){ch=getch();if(ch==27){for(short i=0;i<20;++i) name[players+1][i]=0;clean2();login();}else if(ch==13){if(lenth){++players;FILE *fp;fp=fopen("players.dat","w");fprintf(fp,"%d %d",players,situation);for(short i=0;i<players;++i) fprintf(fp,"%s %d %d %d %d %d \n",name[i],record[i][0],record[i][1],record[i][2],record[i][3],record[i][4]);fclose(fp);clean2();records();}}else if(ch==8){if(lenth){XY(hOut,29+lenth,19);puts(" ");name[players][lenth]=0;--lenth;}}else if(lenth<=20&&ch!=0x48&&ch!=0x50){XY(hOut,30+lenth,19);cout<<ch;name[players][lenth]=ch;++lenth;}}}}inline void XY(HANDLE hOut,short x,short y){COORD pos;pos.X=x;pos.Y=y;SetConsoleCursorPosition(hOut,pos);}



原创粉丝点击