蒟蒻打的飞机大战1.0(简单易懂)
  • 板块灌水区
  • 楼主gf20231114
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/1/29 07:54
  • 上次更新2024/1/29 08:28:19
查看原帖
蒟蒻打的飞机大战1.0(简单易懂)
1246864
gf20231114楼主2024/1/29 07:54

蒟蒻打的飞机大战1.0(简单易懂)

#include<conio.h>
#include<windows.h>
#include<bits/stdc++.h>
using namespace std;
struct location{
	short y,x;
}plane,fighter;//我机与敌机坐标
queue<location>bullet;//子弹坐标


void slowout(string str){
	for(int i=0;i<str.length();i++){
		printf("%c",str[i]);
		Sleep(30);
	}
}

HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;//这样可以快速调整光标位置(精华呀)!
int game_difficulty;//是否让敌机移动(具体看循环的最后,它只在那里出现)
long long kill_time=0,history_score;//击杀敌机数量;历史最高得分
bool robot_play,fighter_live;//是否让计算机玩;敌机是否存活


int main(){
	system("color 9F");
	slowout("现在是公元2032年,战争时期!");
	Sleep(1000);
	slowout("数百架六代敌机从十艘福特级航母上一架架的起飞,此刻的您已经发射完了所有的导弹,剩下的武器只有一台机炮,");
	Sleep(1000);
	slowout("但是为了祖国,为了世界的核平!");
	Sleep(1000);
	slowout("同时也为了您自己,您必须保证没有一架敌机突防!\n\n");
	Sleep(1000);
	slowout("操作指南(极简版):");
	Sleep(500);
	slowout("w,a,s,d\n");
	printf("请按r欣赏机器示范或按其他任意键开始游戏...");
	srand(time(0));//用于随机生成敌机
	char c=getch();
	if(c=='R'||c=='r'){
		robot_play=true;
		MessageBox(0,"在示范过程中,您可以按任意键结束示范。","小贴士",MB_OK);
	}while(true){
		plane.y=20,plane.x=40;
		fighter.y=0,fighter.x=30;//初始坐标
		fighter_live=true;
		kill_time=0; 
		while(true){
			//屏幕边界:40*60 
			system("cls");
			coord.Y=0,coord.X=70;
			SetConsoleCursorPosition(hout,coord);//设置控制台光标位置
			printf("Your kill:%lld",kill_time);
			++coord.Y,coord.X=70;
			SetConsoleCursorPosition(hout,coord);
			printf("Your score:%lld",kill_time*(kill_time+1)/2);
			++coord.Y,coord.X=70;
			SetConsoleCursorPosition(hout,coord);
			printf("Your history score:%lld",history_score=max(history_score,kill_time*(kill_time+1)/2));
			
			
			coord.Y=plane.y,coord.X=plane.x;
			SetConsoleCursorPosition(hout,coord);
			printf("|");
			++coord.Y,coord.X=plane.x-1;
			SetConsoleCursorPosition(hout,coord);
			printf("/|\\");
			++coord.Y,coord.X=plane.x-2;
			SetConsoleCursorPosition(hout,coord);
			printf("/|||\\");
			++coord.Y,coord.X=plane.x-3;
			SetConsoleCursorPosition(hout,coord);
			printf("=======");
			//输出我机
			
			coord.Y=fighter.y;
			coord.X=fighter.x-3;
			SetConsoleCursorPosition(hout,coord);
			printf("=======");
			++coord.Y,coord.X=fighter.x-2;
			SetConsoleCursorPosition(hout,coord);
			printf("\\\\|//");
			++coord.Y,coord.X=fighter.x-1;
			SetConsoleCursorPosition(hout,coord);
			printf("\\|/");
			++coord.Y,coord.X=fighter.x;
			SetConsoleCursorPosition(hout,coord);
			printf("|");
			//输出敌机
			
			
			if(plane.y){
				location new_bullet;
				new_bullet.y=plane.y-1,new_bullet.x=plane.x;
				bullet.push(new_bullet);
			}//如果我机未触碰到屏幕顶上则生成新的子弹
			for(int i=1;i<=bullet.size();i++){
				location tmp=bullet.front();
				bullet.pop();
				coord.Y=tmp.y,coord.X=tmp.x;
				SetConsoleCursorPosition(hout,coord);
				printf(".");
				//输出子弹
				
				if(tmp.y==fighter.y+4&&tmp.x>=fighter.x-1&&tmp.x<=fighter.x+2||tmp.y==fighter.y+3&&tmp.x>=fighter.x-2&&tmp.x<=fighter.x+3||tmp.y==fighter.y+2&&tmp.x>=fighter.x-3&&tmp.x<=fighter.x+4||tmp.y==fighter.y+1&&tmp.x>=fighter.x-4&&tmp.x<=fighter.x+5||tmp.y==fighter.y&&tmp.x>=fighter.x-5&&tmp.x<=fighter.x+6){
					fighter_live=false;//如果子弹命中敌机敌机就死了
					++kill_time;
				}
				
				
				if(tmp.y){ 
					--tmp.y;//如果子弹未触碰屏幕顶上则继续上移
					bullet.push(tmp);
				}
			}
			
			
			if(kbhit()){//判断是否有输入(以免程序暂停,子弹、敌机停止运动)
				char c=getch();
				if(robot_play){
					break;
				}if(c=='d'||c=='D'){
					if(plane.x<=55){//如果已触碰到屏幕边界则不移动
						plane.x+=2;
					}
				}else if(c=='a'||c=='A'){
					if(plane.x>=5){
						plane.x-=2;
					}
				}else if(c=='s'||c=='S'){
					if(plane.y<=35){
						plane.y+=2;
					}
				}else if(c=='w'||c=='W'){
					if(plane.y>=2){
						plane.y-=2;
					}
				}
			}//这部分用于玩家控制我机移动
			if(robot_play){
				if(plane.y<35){
					++plane.y;
				}else if(plane.x<fighter.x){
					++plane.x;
				}else if(plane.x>fighter.x){
					--plane.x;
				}
			}//这部分用于计算机控制我机移动
			
			
			if(!fighter_live){//如果敌机死亡则随机生成新的
				fighter.x=rand()%44+3,fighter.y=0;
				fighter_live=true;
			}
			
			
			if(++game_difficulty>=max(20-kill_time/2,(long long)4)){//这样可以让游戏难度越来越大
				game_difficulty=0;
				if(fighter.y<47){//如果敌机未到达屏幕底下则继续下移
					++fighter.y;
				}else{
					Sleep(3000);//否则玩家死亡
					break;
				}
			}
		}system("cls");
		printf("You lose!\nYour kill:%lld\nYour score:%lld",kill_time,kill_time*(kill_time+1)/2);
		history_score=max(history_score,kill_time*(kill_time+1)/2);
		printf("\n\nAnd your history score is:%lld\n\n请按r欣赏机器示范或按其他任意键继续游戏......",history_score);
		char c=getch();
		if(c=='R'||c=='r'){
			robot_play=true;
		}else{
			robot_play=false;
		}
	}return 0;
}//taskkill /im studentmain.exe /f反止我忘了怎么杀极域
2024/1/29 07:54
加载中...