这是一个关于机场塔台的小游戏(飞友福利~~),介于目前吾代码水平限制,游戏略显简陋(所以是 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;i 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 planes[i].speed+0.5&&changing[i].speed) planes[i].speed+=0.5; else if(changing[i].speed planes[i].height+3&&changing[i].height) planes[i].height+=3; else if(changing[i].height planes[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