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

这篇具有很好参考价值的文章主要介绍了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++半年,也不能追求太多,如果大家有建议可以提出哦!话不多说,直接上代码!文章来源地址https://www.toymoban.com/news/detail-454075.html

#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();//开始玩 
} 

到了这里,关于C++飞机大战(注释较多,新手可读懂,附全代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【JAVA】飞机大战

    代码和图片放在这个地址了: https://gitee.com/r77683962/fighting/tree/master 20231016: 1、原来画面比较闪烁,使用双缓冲解决了; 2、当前服务端和客户端可以分别操作不同友方飞机; 3、日志打印自己定义了下格式; 20231003: 1、添加背景音乐; 20231001: 1、现在子弹打中飞机,飞机

    2024年02月08日
    浏览(32)
  • 【c语言】飞机大战(1)

    提前准备好游戏要的素材,可以到爱给网去找,飞机大战我们需要的是一个我方战机图片,一个背景图,三个敌方战机的图,我方战机的图片,敌方战机的图片,并且将图片和.cpp放在同一文件夹下. 这里创建.cpp的文件是因为要用到图形库,所以创建.cpp的文件,然后图片格式

    2024年02月04日
    浏览(28)
  • 【c语言】飞机大战终

    效果演示

    2024年02月03日
    浏览(29)
  • scratch飞机大战

    今天scratch作品是飞机大战。 我们做一个基础版的飞机大战,要实现的操作有:我们的飞机可以按左右键进行左右移动,但是不能上下移动,然后飞机一直在发射子弹,如果子弹碰到下落的敌机就可以获得得分,如果被敌机碰到了就算失败,游戏结束。 首先第一步,我们要从

    2024年02月09日
    浏览(50)
  • python学习-飞机大战

    一、pygame正确安装 1.安装:pip install pygame 2.验证安装:python -m pygame.examples.aliens;安装ok即会出现如下图所示动画 二、使用pygame创建图像窗口 1.游戏的初始化和退出;要使用pygame提供的所有功能之前,必须先调用init方法;在游戏结束,需要调用quit方法 2.在游戏中,所有可见的

    2024年02月11日
    浏览(67)
  • Python飞机大战(完整版)

    简介:一共分为2个py文件,分别是主类、和精灵类 飞机大战图片地址:链接: https://pan.baidu.com/s/18T6n9JFIDxBqYX9CnHi7ZQ  密码: tqbr 注释:项目启动后如果报libpng warning: iCCP: known incorrect sRGB profile无须处理,不会影响项目和代码,视觉洁癖可以直接百度搜索这个错误提示,网上有解

    2024年02月12日
    浏览(33)
  • QT实现飞机大战-2

    1.制作子弹类 创建子弹文件和类 添加子弹类中的成员函数和成员属性 实现成员函数 测试子弹 1.1创建子弹文件夹和类 创建Bullet类以及生成对应的文件 创建好后生成bullet.h 和 bullet.cpp两个文件 1.2添加子弹的成员函数和成员属性 战机发射子弹,子弹发射后会向上移动,移动到地

    2024年01月22日
    浏览(30)
  • 飞机大战python代码

    下面附有源代码,一个简单运用python的应用。 图片可自己自由调配 部分模块需要去从python的软件包去下载,可以去从清华和北大还有阿里的云仓库中获取镜像链接,下载速度会快点。 源代码如下并有详细注释:

    2024年02月03日
    浏览(26)
  • python实现飞机大战

    2024年03月24日
    浏览(41)
  • Android Studio实现飞机大战

    通过自定义View实现Android飞机大战小游戏,游戏玩法很简单,可以锻炼玩家的反应能力。开启背景音乐进行新的游戏,控制飞机移动来消灭敌机获取更多的分数,在移动过程中避免与敌机发生碰撞。主界面可以查看自己的历史战绩和游戏规则,详细规则如下: 我们定义了 Sp

    2024年02月16日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包