学了c++,总想用c++写点东西,便想到了飞机大战,话不多说,直接开始写!!

一,头文件的问题

这个不用多说~直接上代码

#include <iostream>//可以用bits/stdc++.h替代
#include <stdlib.h>//要用到system(),可以用bits/stdc++.h替代
#include <ctime>//要用到srand()和time(),可以用bits/stdc++.h替代
#include <windows.h>//要用到SetConsoleTextAttribute,不可以用bits/stdc++.h替代
#include <conio.h>//要用到最重要的kbhit()和getch(),不可以用bits/stdc++.h替代
#include <unistd.h>//要用到sleep(),不可以用bits/stdc++.h替代
#define planerow 15//我方战机的行,由于不变,直接define
using namespace std;
int planeline=35,rnd=1,ans;//我方战机的列,次数,回答 

二,地图的问题

这个我也不用多说了~直接手绘!

char a[100][100]={//地图(手绘) {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
}; 

三,清屏

要移动,至少得写个清屏函数吧……

void clear(){//清屏 system("cls");
}

四,画地图

那还得输出地图

void draw(){//地图输出 for(int i=0;i<=16;i++){for(int j=0;j<=70;j++)if(a[i][j]=='1'){//我方战机 color(1);cout<<"*";color(0);}elseif(a[i][j]=='2'){//敌方战机 color(2);cout<<"*";color(0);} elseif(a[i][j]=='3'){//子弹 color(3);cout<<"*";color(0);}else cout<<a[i][j];//边框和空格 cout<<endl;//换行 }
}

五,发射炮弹

首先,你得先让子弹出现

void boom(){//控制发射子弹 a[planerow-1][planeline]='3';
}

之后,子弹还有前进

void about_boom(){//控制子弹前进clear();//清屏 int r1[10000]={0},l1[10000]={0},cur=1;//记录哪个地方有子弹,r1是行,l1是列,cur是光标 for(int row=1;row<=15;row++)for(int line=1;line<=69;line++)if(a[row][line]=='3'){r1[cur]=row;l1[cur]=line;cur++;}for(int i=1;i<=cur;i++){a[r1[i]-1][l1[i]]='3';//前进一格 a[r1[i]][l1[i]]=' ';//清除原位 } draw();
}

那有人问了,如果撞到敌机了怎么办?不用管,那是敌机部分的事儿了……

六,我方战机移动

没有颜色有点单调……

void color(int pll){//控制颜色,不然太难打了 if(pll==3)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);//子弹(蓝色)if(pll==2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);//敌机(绿色) if(pll==1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);//我方(红色) if(pll==0)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED |FOREGROUND_BLUE| FOREGROUND_GREEN );//原色
}

最后还得用kbhit()和getch()……

void kbread(){if(kbhit()){//如果键盘按下 if(getch()=='a'){//左移 if(a[planerow][planeline-1]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline--;//左移一位 a[planerow][planeline]='1';draw();}}if(getch()=='q'){//快速左移 if(a[planerow][planeline-1]==' '&&a[planerow][planeline-2]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline-=2;//左移2位 a[planerow][planeline]='1';draw();}}if(getch()=='d'){//右移 if(a[planerow][planeline+1]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline++;//右移一位 a[planerow][planeline]='1';draw();}}if(getch()=='e'){//快速右移 if(a[planerow][planeline+1]==' '&&a[planerow][planeline+2]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline+=2;//右移2位 a[planerow][planeline]='1';draw();}}if(getch()=='w'){//发射子弹 clear();boom();//发射 draw();}}
}

七,游戏失败

如果敌机撞墙(突破底线),那就输了

void kill(){//游戏失败 clear();//清屏 color(1);//颜色为红 cout<<"失败!!!"<<endl;color(0);//回原色 sleep(10);//等待10秒 exit(0);//停止一切
}

八,敌方飞机

到最难的部分了,自己看注释吧……

void enemy_plane(){//敌机 if(ans==1){//简单模式 clear();//清屏 srand(time(NULL));//播下时间种子 if(rnd==1){//第一次(刚开始) int enemy_line=rand()%69+1;//随机出现 a[1][enemy_line]='2';}if(rnd%7==0){//每7次运行会执行一次 int r[10000]={0},l[10000]={0},cur=1;//记录哪个地方有敌机,r是行,l是列,cur是光标 for(int row=1;row<=15;row++)for(int line=1;line<=69;line++)if(a[row][line]=='2'){r[cur]=row;l[cur]=line;cur++;}for(int i=1;i<=cur;i++){//进行前进操作 if(a[r[i]+1][l[i]]=='3')//遇到炮弹 a[r[i]][l[i]]=' ';//消失 a[r[i]+1][l[i]]='2';//否则前进1格 a[r[i]][l[i]]=' ';//清除原位 if(r[i]+1==16)//碰到底部 kill();//失败 }}if(rnd%50==0){//每执行50次中执行1次 int enemy_line=rand()%69+1;//生成敌机 a[1][enemy_line]='2';}draw();rnd++;//次数++ }if(ans==2){//和上面差不多,只是效率变了 clear();srand(time(NULL));if(rnd==1){int enemy_line=rand()%69+1;a[1][enemy_line]='2';}if(rnd%4==0){//每4次运行会执行一次 int r[10000]={0},l[10000]={0},cur=1;for(int row=1;row<=15;row++)for(int line=1;line<=69;line++)if(a[row][line]=='2'){r[cur]=row;l[cur]=line;cur++;}for(int i=1;i<=cur;i++){if(a[r[i]+1][l[i]]=='3')a[r[i]][l[i]]=' ';a[r[i]+1][l[i]]='2';a[r[i]][l[i]]=' ';if(r[i]+1==16)kill();}}if(rnd%35==0){//每35次运行会执行一次 int enemy_line=rand()%69+1;a[1][enemy_line]='2';}draw();rnd++;}
}

九,结合一下

void play(){while(true){enemy_plane();//生成敌机 kbread();//是否读入 about_boom();//子弹飞行 sleep(0.5);//等待0.5秒 }
}

十,主函数

int main()
{menu();//先选择模式 clear();//清屏 complete();//初始战机坐标 draw();//画地图 play();//开始玩
}

十一,总代码

总的来说,这个代码还可以,直接放到dev里或其他运就行了,有点闪,但毕竟小编

才接触c++半年,也不能追求太多,如果大家有建议可以提出哦!话不多说,直接上代码!

#include <iostream>//可以用bits/stdc++.h替代
#include <stdlib.h>//要用到system(),可以用bits/stdc++.h替代
#include <ctime>//要用到srand()和time(),可以用bits/stdc++.h替代
#include <windows.h>//要用到SetConsoleTextAttribute,不可以用bits/stdc++.h替代
#include <conio.h>//要用到最重要的kbhit()和getch(),不可以用bits/stdc++.h替代
#include <unistd.h>//要用到sleep(),不可以用bits/stdc++.h替代
#define planerow 15//我方战机的行,由于不变,直接define
using namespace std;
int planeline=35,rnd=1,ans;//我方战机的列,次数,回答
char a[100][100]={//地图(手绘) {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
};
void clear(){//清屏 system("cls");
}
void menu(){//初始化 cout<<"简单:1"<<endl<<"困难:2"<<endl;//选择模式 cin>>ans; //读入模式 clear();//清屏 cout<<"规则:a左移,d右移,w发射子弹,q快速左移,e快速右移,请调至英文模式"<<endl;//规则 sleep(5);//5秒后自动进入
}
void complete(){//确定战机坐标 a[15][35]='1';//初始坐标
}
void color(int pll){//控制颜色,不然太难打了 if(pll==3)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);//子弹(蓝色)if(pll==2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);//敌机(绿色) if(pll==1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);//我方(红色) if(pll==0)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED |FOREGROUND_BLUE| FOREGROUND_GREEN );//原色
}
void draw(){//地图输出 for(int i=0;i<=16;i++){for(int j=0;j<=70;j++)if(a[i][j]=='1'){//我方战机 color(1);cout<<"*";color(0);}elseif(a[i][j]=='2'){//敌方战机 color(2);cout<<"*";color(0);} elseif(a[i][j]=='3'){//子弹 color(3);cout<<"*";color(0);}else    cout<<a[i][j];//边框和空格 cout<<endl;//换行 }
}
void kill(){//游戏失败 clear();//清屏 color(1);//颜色为红 cout<<"失败!!!"<<endl;color(0);//回原色 sleep(10);//等待10秒 exit(0);//停止一切
}
void boom(){//控制发射子弹 a[planerow-1][planeline]='3';
}
void about_boom(){//控制子弹前进clear();//清屏 int r1[10000]={0},l1[10000]={0},cur=1;//记录哪个地方有子弹,r1是行,l1是列,cur是光标 for(int row=1;row<=15;row++)for(int line=1;line<=69;line++)if(a[row][line]=='3'){r1[cur]=row;l1[cur]=line;cur++;}for(int i=1;i<=cur;i++){a[r1[i]-1][l1[i]]='3';//前进一格 a[r1[i]][l1[i]]=' ';//清除原位 } draw();
}
void enemy_plane(){//敌机 if(ans==1){//简单模式 clear();//清屏 srand(time(NULL));//播下时间种子 if(rnd==1){//第一次(刚开始) int enemy_line=rand()%69+1;//随机出现 a[1][enemy_line]='2';}if(rnd%7==0){//每7次运行会执行一次 int r[10000]={0},l[10000]={0},cur=1;//记录哪个地方有敌机,r是行,l是列,cur是光标 for(int row=1;row<=15;row++)for(int line=1;line<=69;line++)if(a[row][line]=='2'){r[cur]=row;l[cur]=line;cur++;}for(int i=1;i<=cur;i++){//进行前进操作 if(a[r[i]+1][l[i]]=='3')//遇到炮弹 a[r[i]][l[i]]=' ';//消失 a[r[i]+1][l[i]]='2';//否则前进1格 a[r[i]][l[i]]=' ';//清除原位 if(r[i]+1==16)//碰到底部 kill();//失败 }}if(rnd%50==0){//每执行50次中执行1次 int enemy_line=rand()%69+1;//生成敌机 a[1][enemy_line]='2';}draw();rnd++;//次数++ }if(ans==2){//和上面差不多,只是效率变了 clear();srand(time(NULL));if(rnd==1){int enemy_line=rand()%69+1;a[1][enemy_line]='2';}if(rnd%4==0){//每4次运行会执行一次 int r[10000]={0},l[10000]={0},cur=1;for(int row=1;row<=15;row++)for(int line=1;line<=69;line++)if(a[row][line]=='2'){r[cur]=row;l[cur]=line;cur++;}for(int i=1;i<=cur;i++){if(a[r[i]+1][l[i]]=='3')a[r[i]][l[i]]=' ';a[r[i]+1][l[i]]='2';a[r[i]][l[i]]=' ';if(r[i]+1==16)kill();}}if(rnd%35==0){//每35次运行会执行一次 int enemy_line=rand()%69+1;a[1][enemy_line]='2';}draw();rnd++;}
}
void kbread(){if(kbhit()){//如果键盘按下 if(getch()=='a'){//左移 if(a[planerow][planeline-1]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline--;//左移一位 a[planerow][planeline]='1';draw();}}if(getch()=='q'){//快速左移 if(a[planerow][planeline-1]==' '&&a[planerow][planeline-2]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline-=2;//左移2位 a[planerow][planeline]='1';draw();}}if(getch()=='d'){//右移 if(a[planerow][planeline+1]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline++;//右移一位 a[planerow][planeline]='1';draw();}}if(getch()=='e'){//快速右移 if(a[planerow][planeline+1]==' '&&a[planerow][planeline+2]==' '){clear();//清屏 a[planerow][planeline]=' ';//原位清空 planeline+=2;//右移2位 a[planerow][planeline]='1';draw();}}if(getch()=='w'){//发射子弹 clear();boom();//发射 draw();}}
}
void play(){while(true){enemy_plane();//生成敌机 kbread();//是否读入 about_boom();//子弹飞行 sleep(0.5);//等待0.5秒 }
}
int main()
{menu();//先选择模式 clear();//清屏 complete();//初始战机坐标 draw();//画地图 play();//开始玩
} 

C++飞机大战(注释较多,新手可读懂,附全代码)相关推荐

  1. 初学者怎样看懂python代码_Python零基础入门-(如何让人读懂你的代码)文档注释

    目标注释的作用 单行注释(行注释) 多行注释(块注释) 01. 注释的作用使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释)以 # 开头,# 右边的所有 ...

  2. 这个R包自动注释单细胞数据的平均准确率为83%,使用后我的结果出现了点问题|附全代码...

    估计大家都有一个这样的感觉就是单细胞数据具有一定的数据依赖性,好多的marker在相同的组织中,别人的数据就表达的十分明显,在你的数据中就是不太显著,比如NK细胞的KLRF1.于是,细胞自动注释也就应 ...

  3. devc中文注释显示问号_Python零基础入门-(如何让人读懂你的代码)文档注释

    目标 注释的作用 单行注释(行注释) 多行注释(块注释) 01. 注释的作用 使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释) 以 # 开头,# 右边 ...

  4. 新手怎么读懂一个中型的Django项目

    [前言]中型的项目是比较多的APP,肯会涉及多数据表的操作.如果有人带那就最好了,自己要先了解基本的django框架(MTV ,ORM等)师傅可以给讲解一下框架怎么组织url.py,model.py, ...

  5. C++游戏编程教程(七)——改进飞机大战游戏

    注:在本篇博客中,对上一篇博客的飞机大战游戏进行了完善,但有很多细微的修改,由于篇幅原因,没有把所有代码列出来,大家需要仔细阅读,否则可能漏掉一些地方,导致编译错误或产生bug. PS:如果大家有什么 ...

  6. C语言飞机大战程序思路,C语言代码实现飞机大战

    本文实例为大家分享了C语言实现简单飞机大战的具体代码,供大家参考,具体内容如下 这个游戏的功能很单一,也就是"飞机大战",哈哈哈哈.总共只有300多行代码左右,你也可以想想它会有多 ...

  7. C语言课程设计 — 飞机大战

    这个游戏的功能很单一,也就是"飞机大战",哈哈哈哈.总共只有300多行代码左右,你也可以想想它会有多简陋,把它复制下来编译一下可以直接执行,需要的同学可以自取~ PS:我运行的环境 ...

  8. Python项目——飞机大战!

    文章目录 一.项目介绍--飞机大战 实战步骤 确认模块 -- pygame 安装 pygame 验证安装 二.pygame 快速入门 项目准备 1. 使用 `pygame` 创建图形窗口 小节目标 1 ...

  9. 自动发射子弹c语言,C语言代码实现飞机大战

    本文实例为大家分享了C语言实现简单飞机大战的具体代码,供大家参考,具体内容如下 这个游戏的功能很单一,也就是"飞机大战",哈哈哈哈.总共只有300多行代码左右,你也可以想想它会有多 ...

最新文章

  1. 王爽实验9.自己解读,代码注释很多!
  2. java中如何调用自身结构_如何在Java中的自定义异常中设置我自己的消息,可以检索我的getMessage()但是没有使用构造函数,有什么办法吗?...
  3. 数据远程实时备份容灾方案
  4. python3.8怎么打开创建_Python 3.8 新功能大揭秘【新手必学】
  5. 一起来学SpringBoot | 第四篇:整合Thymeleaf模板
  6. 软件架构自学笔记---架构分析
  7. 洛谷P1273 有线电视网
  8. ubuntu16.0.4安装mysql5.7以及设置远程访问
  9. LIRe 源代码分析 6:检索(ImageSearcher)[以颜色布局为例]
  10. 计算器: 请输入两个数和一个符号,完成两个数的+ - * / % // **
  11. Java——异常处理,数据库连接
  12. Kotlin 文档入门-函数 集合
  13. GIS + 现代农业”,将会擦出怎样的火花?——智慧农业专题论坛侧记
  14. 物联网通信技术-概述
  15. APE音乐的处理[shntool]
  16. Android APP微信第三方登录踩坑 - 微信开放平台修改应用包名后微信第三方登录失败
  17. php统计页面访问量_PHP 统计 网页 总访问次数 附代码
  18. java 微信给好友发信息吗,不在线,好友发给我的微信消息,会不会丢?
  19. 计算机视觉论文-2021-06-24
  20. 纯前端实现—满天星效果

热门文章

  1. 100个让舌头抽筋的绕口令!!!
  2. 【ML4CO论文精读】用于组合优化的机器学习:方法论之旅(Yoshua Bengio, 2021)
  3. EXCEL-分组排名(中美)
  4. IDEA使用中directory和package
  5. 我认为这种计算机卖的很好英语翻译,英语翻译
  6. Docker下redis与springboot三部曲之三:springboot下访问redis哨兵
  7. 星星点灯(点亮LED灯)
  8. C#调用matlab
  9. checkbox选中和不选中 jqu_jQuery控制checkbox选中状态但是不显示选中
  10. 电子表整点报时怎么取消_上海迪士尼取消FP后,预约等候卡使用攻略!