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

  学了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);
			}
			else
				if(a[i][j]=='2'){//敌方战机 
					color(2);
					cout<<"*";
					color(0);
				} 
				else
					if(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);
			}
			else
				if(a[i][j]=='2'){//敌方战机 
					color(2);
					cout<<"*";
					color(0);
				} 
				else
					if(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();//开始玩 
} 

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值