飞机大战-复制到C++里

#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <windows.h>
#include <conio.h> 
#include <unistd.h>
#define planerow 15
using namespace std;
int planeline=35,rnd=1,ans;

void printf_blue(const char *s)
{
    printf("\033[0m\033[1;34m%s\033[0m", s);
}
void printf_red(const char *s)
{
    printf("\033[0m\033[1;31m%s\033[0m", s);
}

void printf_green(const char *s)
{
    printf("\033[0m\033[1;32m%s\033[0m", s);
}

void printf_yellow(const char *s)
{
    printf("\033[0m\033[1;33m%s\033[0m", s);
}
int randint(int l, int r)
{
    return rand() % (r - l + 1) + l;
}
void printf_pink(const char *s)
{
    printf("\033[0m\033[1;35m%s\033[0m", s);
}
char a[100][100]=
{ 
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
	{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
}; 
void clear(){//清屏 
	system("cls");
}
void menu(){
	printf_green("简单");
	cout << ":1"<<endl;
	printf_red("困难"); 
	cout << ":2" << endl; 
	cin>>ans;
	clear();
	printf_yellow("操作说明:\n"); 
	cout << "a 键向左移动, d 键向右移动\nq 键快速左移, e 键快速右移\n";
	printf_red("注: 用英文模式才管用") ; 
	sleep(2);
}
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);
	printf_red("失败!\n"); 
	color(0);
	sleep(10);
	exit(0);
}
void boom(){
	a[planerow-1][planeline]='3';
}
void about_boom(){
	clear(); 
	int r1[10000]={0},l1[10000]={0},cur=1;
	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;
			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%50==0){
			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){
			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()
{
	clear();
	printf_yellow("欢迎来到") ;
	printf_red("飞机大战\n\n");
	sleep(1); 
	printf_pink("请选择模式!\n"); 
	menu();//先选择模式 
	clear();//清屏
	printf_blue("游戏准备加载!\n");
	sleep(1.2);
	printf_pink("加载中..."); 
	sleep(randint(1, 3));
	ShowCursor(false);
	clear();
	complete();//初始战机坐标 
	draw();//画地图 

	play();//开始玩 
}