无任何格外需求的命令行C++飞机大战,内含BOSS,动画,万行代码!免费奉上!

个程序的源码没有什么技术要求,一般至少能看懂95%,因为博主是大一上学期写着玩的,当写了一周,还拿它参加了学校的创意编程比赛,结果第一用的ui,直接降维打击了,拿了个二等奖

 

 

操作方法游戏内都有

注意事项:1在动画时不能乱按
     2小怪设计的初衷是躲避,因此碰到就直接死(主要是我当初没想到我能把敌方子弹射击出来)
     3子弹按的速度过快会在某个点停下来,困难模式基本不会有这种情况,狂按就完了
     4由于小怪主要设计是躲避,所以子弹给的比较少,到BOSS时会自动增加,足够用
     5积分达到200时BOSS就会出现,BOSS四个技能,其中激光碰到大概率会直接死
     6尽量选择普通和困难,简单模式太弱智了

游戏思路:控制光标移动,通过坐标定点输出起到子弹移动,飞机移动效果和动画效果。
                 以不同数组分别存放我方与敌方的子弹坐标,理论上增加数组就可以增加子弹种类


缺点:就是移动僵硬,不能使长按速度保持稳定,所以点按玩起来效果更好。
          我方飞机不够没观,敌机种类较少
          没有音效(是一个遗憾)

大家有什么问题欢迎评论或私信问我哦,博主现在大一,一般一天之内就可以回复。

因为这个程序是一个多月前写的,有些重要的东西我可能没有说,欢迎提问!

头文件:

#include<iostream>
#include<Windows.h>
#include<conio.h>
#include<cstdlib>
#include<time.h>
#include<stdlib.h>
#include<cmath>
#include"menu.h"
 
//飞机活动大小
#define width 80
#define height 40
//窗口大小
#define t_width 120
#define t_height 40
 
void gotoxy(short x, short y);
void Game();
 
void enemyAppear(char arr[width + 10][height]);
void enemyMove(char arr[width + 10][height]);
 
void bulletMove(char arr[width + 10][height]);
void BOSSgame();
void shotBOSS(char arr[width + 10][height]);
void difficulty();
 
void cartoonStartMenu();
void start_menu();
void game_menu();
void cartoonGameMenu();
void end_menu();
void success_menu();
void BOSScartoon();
void printBOSS();

全局变量:

这部分主要是调节难度用的

extern int score = 0;
extern int bullet = 99999;
extern int HP = 100000;
extern int BOSSHP = 500;
extern int damage = 3;
extern int enemySpeed = 10;
extern int BOSSspeed = 15;
extern int Number = 4;
extern int BOSS = 0;
 

最难的部分:

该部分是我认为命令行实现按键移动的关键,如果只是想试玩一下只要知道函数功能就够了

深入了解handle(句柄)我推荐:控制台API函数----HANDLE、SetConsoleCursorPosition、SetConsoleTextAttribute_weixin_30901729的博客-CSDN博客

//清除光标
void clearCursor()
{
	HANDLE hOut;
	CONSOLE_CURSOR_INFO cur = { 1,0 };
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(hOut, &cur);
}
 
 
//使光标移动到指定位置
void gotoxy(short x, short y)
{
	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x,y };
	SetConsoleCursorPosition(hOut, pos);
}

存子弹和飞机位置的数组:

char enemyArr[width + 10][height] = { 0 };
char myBulletArr[width + 10][height] = { 0 };

我方飞机的子弹射击,移动

//飞机图像
void printPlane(short x, short y)
{
	gotoxy(x, y);
	cout << "*";//头
	gotoxy(x, y + 1);
	cout << "*";//尾
	gotoxy(x - 1, y + 1);
	cout << "*";//左翼
	gotoxy(x + 1, y + 1);
	cout << "*";//右翼
}
 
 
class myPlane
{
public:
	//头的坐标
	short x = width / 2;
	short y = height / 2;
 
 
 
	//初始化飞机
	void initMyPlane()
	{
		printPlane(x, y);
	}
 
	//清除飞机移动前位置图像
	void clearMyplane()
	{
		gotoxy(x, y);
		cout << " ";//头
		gotoxy(x, y + 1);
		cout << " ";//尾
		gotoxy(x - 1, y + 1);
		cout << " ";//左翼
		gotoxy(x + 1, y + 1);
		cout << " ";//右翼
	}
 
 
	//射击
	void shoot(char myArr[width + 10][height])
	{
		if (bullet == 0)
			return;
		myArr[x][y - 1] = '*';
		gotoxy(x, y - 1);
		cout << "*";
		bullet--;
		gotoxy(width + (t_width - width) / 2, 9);
		cout << "   ";
		gotoxy(width + (t_width - width) / 2, 9);
		cout << bullet;
	}
 
	//飞机移动+游戏中的菜单栏功能实现
	void myPlaneMove(char arr[width + 10][height])
	{
		//判断是否有键盘操作
		if (_kbhit())
		{
			char move;
			move = _getch();
			switch (move)
			{
			case 'w':
				if (BOSS == 1)
					if (y == 16)
						break;
				//判断是否是边界
				if (y == 1)
					break;
				//清除原图像
				clearMyplane();
				y--;
				//打印新位置
				printPlane(x, y);
				break;
			case 'a':
				if (x - 1 == 1)
					break;
				clearMyplane();
				x--;
				printPlane(x, y);
				break;
			case 's':
				if (y + 1 == height - 2)
					break;
				clearMyplane();
				y++;
				printPlane(x, y);
				break;
			case 'd':
				if (x + 1 == width - 2)
					break;
				clearMyplane();
				x++;
				printPlane(x, y);
				break;
			case 'j':
				shoot(myBulletArr);
			case 'W':
				if (BOSS == 1)
					if (y == 16)
						break;
				//判断是否是边界
				if (y == 1)
					break;
				//清除原图像
				clearMyplane();
				y--;
				//打印新位置
				printPlane(x, y);
				break;
			case 'A':
				if (x - 1 == 1)
					break;
				clearMyplane();
				x--;
				printPlane(x, y);
				break;
			case 'S':
				if (y + 1 == height - 2)
					break;
				clearMyplane();
				y++;
				printPlane(x, y);
				break;
			case 'D':
				if (x + 1 == width - 2)
					break;
				clearMyplane();
				x++;
				printPlane(x, y);
				break;
            //控制射击
			case 'J':
				shoot(myBulletArr);
			}
		}
	}
};
 
myplane p;//创捷我方飞机

难度参数设置:

//难度选择
void difficulty()
{
	gotoxy(t_width / 2 - 5, t_height / 2 - 4);
	cout << "1.简单模式";
	gotoxy(t_width / 2 - 5, t_height / 2);
	cout << "2.普通模式";
	gotoxy(t_width / 2 - 5, t_height / 2 + 4);
	cout << "3.困难模式";
	char choice;
	choice = _getch();
	switch (choice)
	{
	case '1':
		enemySpeed = 50;
		BOSSspeed = 25;
		HP = 1000;
		damage = 5;
		bullet = 50;
		Number = 2;
		break;
	case '2':
		enemySpeed = 10;
		BOSSspeed = 15;
		HP = 500;
		damage = 3;
		bullet = 30;
		Number = 4;
		break;
	case '3':
		enemySpeed = 8;
		BOSSspeed = 5;
		HP = 100;
		damage = 2;
		bullet = 10;
		Number = 6;
		break;
	}
 
}

子弹功能实现:

void shotEnemy(char enemyArr[width + 10][height], char myArr[width + 10][height])
{
	for (int i = 2; i < width; i++)
		for (int j = 2; j < height; j++)
		{
			if (myArr[i][j] == '*' || myArr[i][j] == '@')
			{
				//碰头
				if (enemyArr[i][j - 2] == '+')
				{
					myArr[i][j] = '@';
 
					gotoxy(i, j - 1);
					cout << "*";
				}
				//碰左翼
				else if (enemyArr[i][j - 2] == 'p')
				{
					myArr[i][j] = '@';
 
					gotoxy(i, j - 1);
					cout << "*";
				}
				//碰右翼
				else if (enemyArr[i][j - 2] == 'q')
				{
					myArr[i][j] = '@';
 
					gotoxy(i, j - 1);
					cout << "*";
				}
				//碰头
				if (enemyArr[i][j - 1] == '+')
				{
					myArr[i][j] = 0;
					enemyArr[i][j - 1] = 0;
					enemyArr[i - 1][j - 2] = 0;
					enemyArr[i + 1][j - 2] = 0;
					enemyArr[i][j - 2] = 0;
 
 
					gotoxy(i, j - 1);
					cout << " ";
					gotoxy(i - 1, j - 2);
					cout << " ";
					gotoxy(i + 1, j - 2);
					cout << " ";
					gotoxy(i, j - 2);
					cout << " ";
					gotoxy(i, j);
					cout << " ";
 
				}
 
				//碰左翼
				else if (enemyArr[i][j - 1] == 'p')
				{
					myArr[i][j] = 0;
					enemyArr[i][j - 1] = 0;
					enemyArr[i - 1][j] = 0;
					enemyArr[i - 1][j - 1] = 0;
					enemyArr[i - 2][j - 1] = 0;
 
 
					gotoxy(i, j - 1);
					cout << " ";
					gotoxy(i - 1, j);
					cout << " ";
					gotoxy(i - 1, j - 1);
					cout << " ";
					gotoxy(i - 2, j - 1);
					cout << " ";
					gotoxy(i, j);
					cout << " ";
				}
 
				//碰右翼
				else if (enemyArr[i][j - 1] == 'q')
				{
					myArr[i][j] = 0;
					enemyArr[i][j - 1] = 0;
					enemyArr[i + 1][j] = 0;
					enemyArr[i + 1][j - 1] = 0;
					enemyArr[i + 2][j - 1] = 0;
 
 
					gotoxy(i, j - 1);
					cout << " ";
					gotoxy(i + 1, j);
					cout << " ";
					gotoxy(i + 1, j - 1);
					cout << " ";
					gotoxy(i + 2, j - 1);
					cout << " ";
					gotoxy(i, j);
					cout << " ";
				}
 
 
 
			}
		}
}
 
//子弹移动
void bulletMove(char myArr[width + 10][height])
{
	for (int i = 2; i < width; i++)
		for (int j = 2; j < height; j++)
		{
			if (myArr[i][j] == '*')
			{
				myArr[i][j] = 0;
				myArr[i][j - 1] = '*';
				gotoxy(i, j - 1);
				cout << "*";
				gotoxy(i, j);
				cout << " ";
			}
 
		}
 
	//清除触界子弹
	for (int k = 1; k < width; k++)
	{
		if (myArr[k][1] == '*')
		{
			gotoxy(k, 1);
			cout << " ";
			myArr[k][1] = 0;
		}
	}
}

敌机设置:

//随机出现敌机
void enemyAppear(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	//随机敌机数量
	int number = rand() % 6 + Number;
	//随机敌机位置
	for (int i = 0; i < number; i++)
	{
		short x = rand() % (width - 4) + 2;
		//防止敌机重叠
		if (enemyArr[x][2] == '+' || enemyArr[x - 1][2] == '+' || enemyArr[x + 1][2] == '+' || enemyArr[x - 2][2] == '+' || enemyArr[x + 2][2] == '+')
			continue;
		short y = 2;
		enemyArr[x][y] = '+';
		enemyArr[x][y - 1] = 'T';
		enemyArr[x - 1][y - 1] = 'q';
		enemyArr[x + 1][y - 1] = 'p';
 
		gotoxy(x, y);
		cout << "+";//头
		gotoxy(x, y - 1);
		cout << "T";//尾   
		gotoxy(x - 1, y - 1);
		cout << "q";//右翼     //+++ 
		gotoxy(x + 1, y - 1);  // 0   
		cout << "p";//左翼
 
	}
}
 
void enemyMove(char enemyArr[width + 10][height])
{
	for (int i = width; i > 0; i--)
		for (int j = height - 3; j > 0; j--)
		{
			if (enemyArr[i][j] == '+')
			{
				enemyArr[i][j - 1] = 0;
				enemyArr[i - 1][j - 1] = 0;
				enemyArr[i + 1][j - 1] = 0;
 
				enemyArr[i][j] = 'T';
				enemyArr[i][j + 1] = '+';
				enemyArr[i - 1][j] = 'q';
				enemyArr[i + 1][j] = 'p';
				gotoxy(i, j - 1);
				cout << " ";
				gotoxy(i - 1, j - 1);
				cout << " ";
				gotoxy(i + 1, j - 1);
				cout << " ";
				gotoxy(i, j + 1);
				cout << "+";
				gotoxy(i, j);
				cout << "T";
				gotoxy(i - 1, j);
				cout << "q";
				gotoxy(i + 1, j);
				cout << "p";
			}
		}
 
	//清除触界敌机
	for (int i = 0; i < width; i++)
	{
		if (enemyArr[i][height - 2] == '+')
		{
			enemyArr[i][height - 2] = 0;
			enemyArr[i][height - 3] = 0;
			enemyArr[i + 1][height - 3] = 0;
			enemyArr[i - 1][height - 3] = 0;
			gotoxy(i, height - 2);
			cout << " ";
			gotoxy(i - 1, height - 3);
			cout << " ";
			gotoxy(i + 1, height - 3);
			cout << " ";
			gotoxy(i, height - 3);
			cout << " ";
			score++;
		}
	}
}
 
//判断是否触碰敌机
bool judgeEnemy(short x, short y, char enemyArr[width + 10][height])
{
	if (enemyArr[x][y] == '+' || enemyArr[x][y] == 'p' || enemyArr[x][y] == 'q' || enemyArr[x - 1][y + 1] == '+' || enemyArr[x - 1][y + 1] == 'p' || enemyArr[x - 1][y + 1] == 'q'
		|| enemyArr[x + 1][y + 1] == '+' || enemyArr[x + 1][y + 1] == 'p' || enemyArr[x + 1][y + 1] == 'q' || enemyArr[x][y + 1] == '+' || enemyArr[x][y + 1] == 'p' || enemyArr[x][y + 1] == 'q')
	{
		return true;
	}
	else
	{
 
		gotoxy(width + (t_width - width) / 2, 11);
		cout << HP << "    ";
		return false;
	}
}

函数整合:

void Game()
{
	//存储敌机坐标
	system("cls");
	difficulty();
	system("cls");
	cartoonStartMenu();
	game_menu();
	p.initMyPlane();
	while (true)
	{
		enemyAppear(enemyArr);
        //这里可能有点难理解,可以先把外层循环去掉只看内层循环
        //Sleep(1)是停止1ms也就是说每enemySpeed毫秒子弹,敌军移动一次
        //把外层循环加进来就是10*enemySpeed毫秒敌军出现一次
        //这样写的目的是使得每一毫秒程序都能检测是否有按键移动射击操作
		for (int i = 0; i < 10; i++)
		{
 
			for (int j = 0; j < enemySpeed; j++) {
				p.myPlaneMove(enemyArr);
				if (judgeEnemy(p.x, p.y, enemyArr))
				{
					end_menu();
				}
				Sleep(1);
			}
			bulletMove(myBulletArr);
			shotEnemy(enemyArr, myBulletArr);
			enemyMove(enemyArr);
			gotoxy(width + (t_width - width) / 2, 7);
			cout << score;
            //分数大于200进入BOSS战
			if (score > 200)
				BOSSgame();
		}
	}
 
}

BOSS功能的实现:

大家在这里可以充分发挥自己的创造力啊>_<

//BOSS子弹移动
void enemyBulletMove(char enemyArr[width + 10][height])
{
	for (int i = width; i > 0; i--)
		for (int j = height - 3; j > 0; j--)
		{
			if (enemyArr[i][j] == '+')
			{
				enemyArr[i][j] = 0;
				enemyArr[i][j + 1] = '+';
 
				gotoxy(i, j);
				cout << " ";
 
				gotoxy(i, j + 1);
				cout << "+";
				judgeBOSSBullet(p.x, p.y, enemyArr);
			}
		}
 
	//清除触界敌军子弹
	for (int i = 0; i < width; i++)
	{
		if (enemyArr[i][height - 2] == '+')
		{
			enemyArr[i][height - 2] = 0;
 
			gotoxy(i, height - 2);
			cout << " ";
		}
	}
}
 
 
//持续射击
void keepShooting(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	for (int j = 0; j < 100; j++)
	{
		int x = rand() % 78 + 1;
		gotoxy(x, 16);
		cout << "+";
		enemyArr[x][16] = '+';
		gotoxy(79 - x, 16);
		cout << "+";
		enemyArr[79 - x][16] = '+';
		for (int j = 0; j < BOSSspeed; j++)
		{
			Sleep(1);
			p.myPlaneMove(enemyArr);
		}
		enemyBulletMove(enemyArr);
		bulletMove(myBulletArr);
		shotBOSS(myBulletArr);
	}
}
 
 
//技能1
void skillA(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	for (int z = 0; z < 3; z++)
	{
		for (int i = width / 2 - 5; i > 2; i--)
		{
			gotoxy(i, 16);
			cout << "+";
			enemyArr[i][16] = '+';
			gotoxy((width - i - 1), 16);
			cout << "+";
			enemyArr[width - i - 1][16] = '+';
			for (int j = 0; j < BOSSspeed; j++)
			{
				Sleep(1);
				p.myPlaneMove(enemyArr);
			}
			enemyBulletMove(enemyArr);
			bulletMove(myBulletArr);
			shotBOSS(myBulletArr);
		}
	}
 
}
 
 
//技能2
void skillB(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	for (int z = 0; z < 3; z++)
	{
		int x = rand() % 76 + 2;
		gotoxy(x, 16);
		cout << "!";
		gotoxy(79 - x, 16);
		cout << "!";
		for (int k = 0; k < 10; k++)
		{
			for (int j = 0; j < BOSSspeed; j++)
			{
				Sleep(1);
				p.myPlaneMove(enemyArr);
			}
			enemyBulletMove(enemyArr);
			bulletMove(myBulletArr);
			shotBOSS(myBulletArr);
		}
		for (int i = 16; i < height - 1; i++)
		{
			gotoxy(x, i);
			cout << "+";
			enemyArr[x][i] = '+';
			gotoxy(x + 1, i);
			cout << "+";
			enemyArr[x + 1][i] = '+';
			gotoxy(x - 1, i);
			cout << "+";
			enemyArr[x - 1][i] = '+';
			gotoxy(79 - x, i);
			cout << "+";
			enemyArr[79 - x][i] = '+';
			gotoxy(79 - x - 1, i);
			cout << "+";
			enemyArr[79 - x - 1][i] = '+';
			gotoxy(79 - x + 1, i);
			cout << "+";
			enemyArr[79 - x + 1][i] = '+';
		}
	}
}
 
//技能3
void skillC(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	int x = rand() % 6 + 37;
	gotoxy(x, 16);
	cout << "+";
	enemyArr[x][16] = '+';
	gotoxy(79 - x, 16);
	cout << "+";
	enemyArr[79 - x][16] = '+';
	p.myPlaneMove(enemyArr);
	enemyBulletMove(enemyArr);
	bulletMove(myBulletArr);
	shotBOSS(myBulletArr);
	for (int j = 0; j < 50; j++)
	{
		int dir = rand() % 2 - 1;
		x = x + dir;
		gotoxy(x, 16);
		cout << "+";
		enemyArr[x][16] = '+';
		gotoxy(79 - x, 16);
		cout << "+";
		enemyArr[79 - x][16] = '+';
		for (int j = 0; j < BOSSspeed; j++)
		{
			Sleep(1);
			p.myPlaneMove(enemyArr);
		}
		enemyBulletMove(enemyArr);
		bulletMove(myBulletArr);
		shotBOSS(myBulletArr);
	}
}
 
 
//BOSS血条
void BOSSHPcartoon()
{
	gotoxy(13, 1);
	cout << "HP:";
	gotoxy(16, 1);
	for (int i = 0; i < BOSSHP / 10; i++)
		cout << "▇";
	cout << "    ";
}
 
//射击BOSS检测
void shotBOSS(char myArr[width + 10][height])
{
	//尾部
	for (int i = 9; i < 14; i++)
	{
		if (myArr[i][4] == '*')
		{
			BOSSHP = BOSSHP - damage / 2;
			myArr[i][4] = 0;
			gotoxy(i, 4);
			cout << " ";
		}
		if (myArr[79 - i][4] == '*')
		{
			BOSSHP = BOSSHP - damage / 2;
			myArr[79 - i][4] = 0;
			gotoxy(79 - i, 4);
			cout << " ";
		}
	}
	//中后部
	for (int i = 14; i < 18; i++)
	{
		if (myArr[i][6] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[i][6] = 0;
			gotoxy(i, 6);
			cout << " ";
		}
		if (myArr[79 - i][6] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[79 - i][6] = 0;
			gotoxy(79 - i, 6);
			cout << " ";
		}
	}
	//中前部
	for (int i = 18; i < 29; i++)
	{
		if (myArr[i][11] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[i][11] = 0;
			gotoxy(i, 11);
			cout << " ";
		}
		if (myArr[79 - i][11] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[79 - i][11] = 0;
			gotoxy(79 - i, 11);
			cout << " ";
		}
	}
	for (int i = 27; i < 40; i++)
	{
		if (myArr[i][13] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[i][13] = 0;
			gotoxy(i, 13);
			cout << " ";
		}
		if (myArr[79 - i][13] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[79 - i][13] = 0;
			gotoxy(79 - i, 13);
			cout << " ";
		}
	}
	//头后部
	for (int i = 31; i < 40; i++)
	{
		if (myArr[i][15] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[i][15] = 0;
			gotoxy(i, 15);
			cout << " ";
		}
		if (myArr[79 - i][15] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[79 - i][15] = 0;
			gotoxy(79 - i, 15);
			cout << " ";
		}
	}
	//头部
	for (int i = 37; i < 40; i++)
	{
		if (myArr[i][16] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[i][16] = 0;
			gotoxy(i, 16);
			cout << " ";
		}
		if (myArr[79 - i][16] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[79 - i][16] = 0;
			gotoxy(79 - i, 16);
			cout << " ";
		}
	}
	BOSSHPcartoon();
	if (BOSSHP <= 0)
		success_menu();
}
 
 
void BOSSshot(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	while (true)
	{
		int i = rand() % 4;
		switch (i)
		{
		case 0:
			keepShooting(enemyArr);
			break;
		case 1:
			skillA(enemyArr);
			break;
		case 2:
			skillB(enemyArr);
			break;
		case 3:
			skillC(enemyArr);
			break;
		}
	}
}

BOSS函数整合:

void BOSSgame()
{
	BOSS = 1;
	bullet = 1000;
	for (int i = 2; i < width; i++)
		for (int j = 2; j < height; j++)
		{
			if (enemyArr[i][j] == '+')
			{
				enemyArr[i][j] = 0;
				enemyArr[i - 1][j - 1] = 0;
				enemyArr[i + 1][j - 1] = 0;
				enemyArr[i][j - 1] = 0;
 
 
				gotoxy(i - 1, j - 1);
				cout << " ";
				gotoxy(i + 1, j - 1);
				cout << " ";
				gotoxy(i, j - 1);
				cout << " ";
				gotoxy(i, j);
				cout << " ";
			}
		}
 
	BOSScartoon();
	BOSSHPcartoon();
	BOSSshot(enemyArr);
}

菜单,动画的实现:

这个动画。。。嘿嘿,其实没啥学的必要,我是图省事这么写的,你们肯定有更好的办法来实现

大家看看上面的菜单就行了,可以直接看后面的main函数

//开始菜单
void start_menu()
{
	for (int i = t_height - 1; i >= t_height / 2 - 4; i--)
	{
		gotoxy(t_width / 2 - 5, i + 1);
		cout << "        ";
		gotoxy(t_width / 2 - 5, i);
		cout << "飞机大战";
		if (i < t_height - 2 - 4)
		{
			gotoxy(t_width / 2 - 6, i + 5);
			cout << "          ";
			gotoxy(t_width / 2 - 6, i + 4);
			cout << "y.进入游戏";
 
		}
		if (i < t_height - 2 - 8)
		{
			gotoxy(t_width / 2 - 6, i + 9);
			cout << "          ";
			gotoxy(t_width / 2 - 6, i + 8);
			cout << "n.退出游戏";
		}
		Sleep(300);
	}
	while (true)
	{
		if (_kbhit())
		{
			char input = _getch();
 
 
			switch (input)
			{
			case 'n':
				system("cls");
				cout << "退出成功" << endl;
				exit(0);
				break;
			case 'N':
				system("cls");
				cout << "退出成功" << endl;
				exit(0);
				break;
			case 'y':
				system("cls");
				cout << "进入游戏" << endl;
				Sleep(1000);
				system("cls");
				Game();
 
				break;
			
			case 'Y':
				system("cls");
				cout << "进入游戏" << endl;
				Sleep(1000);
				system("cls");
				Game();
 
				break;
			}
		}
		
	}
}
 
void cartoonStartMenu()
{
 
	gotoxy(t_width / 2 - 8, t_height / 2 - 6);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 5);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 4);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 3);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 2);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 1);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 + 1);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 + 2);
	cout << "************";
	Sleep(1000);
	system("cls");
	gotoxy(t_width / 2 - 8, t_height / 2 - 6);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 5);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 4);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 3);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 2);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 1);
	cout << "*           ";
	gotoxy(t_width / 2 - 8, t_height / 2);
	cout << "*           ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 1);
	cout << "*           ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 2);
	cout << "************";
	Sleep(1000);
	system("cls");
	gotoxy(t_width / 2 - 8, t_height / 2 - 6);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 5);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 4);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 3);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 2);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 1);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 1);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 2);
	cout << "     **     ";
	Sleep(1000);
	system("cls");
}
 
 
//地图框架和右菜单
void game_menu()
{
	//框架
	gotoxy(0, 0);
	for (int i = 0; i < width; i++)
		cout << "#";
	gotoxy(0, height - 1);
	for (int i = 0; i < width; i++)
		cout << "#";
	for (int i = 0; i < height; i++)
	{
		gotoxy(0, i);
		cout << "#";
	}
	for (int i = 0; i < height; i++)
	{
		gotoxy(width - 1, i);
		cout << "#";
	}
 
 
	//右部菜单
	gotoxy(width + (t_width - width) / 2 - 6, 5);
	cout << "代号:NJTECH";
	gotoxy(width + (t_width - width) / 2 - 6, 7);
	cout << "得分:";
	gotoxy(width + (t_width - width) / 2 - 6, 9);
	cout << "子弹:";
	gotoxy(width + (t_width - width) / 2 - 6, 11);
	cout << "血量:";
 
	gotoxy(width, 13);
	cout << "                   **                   ";
	gotoxy(width, 14);
	cout << "                   **                   ";
	gotoxy(width, 15);
	cout << "                   **                   ";
	gotoxy(width, 16);
	cout << "                  ****                  ";
	gotoxy(width, 17);
	cout << "                 ******                 ";
	gotoxy(width, 18);
	cout << "            *  ****  ****  *            ";
	gotoxy(width, 19);
	cout << "           ******************           ";
	gotoxy(width, 20);
	cout << "          ****   ******   ****          ";
	gotoxy(width, 21);
	cout << "       **************************       ";
	gotoxy(width, 22);
	cout << "             ****      ****             ";
	gotoxy(width, 23);
	cout << "               **      **               ";
	gotoxy(width, 24);
	cout << "                *      *                ";
	//规则
	gotoxy(width + (t_width - width) / 2 - 14, 28);
	cout << "w 前进";
	gotoxy(width + (t_width - width) / 2 - 14, 30);
	cout << "s 后退";
	gotoxy(width + (t_width - width) / 2 - 14, 32);
	cout << "d 右移";
	gotoxy(width + (t_width - width) / 2 - 14, 34);
	cout << "a 左移";
	gotoxy(width + (t_width - width) / 2 - 14, 36);
	cout << "j 射击";
}
 
 
 
void cartoonGameMenu()
{
	for (int i = 0; i < height / 2; i++)
	{
		gotoxy(0, i);
		for (int j = 0; j < t_width; j++)
		{
			cout << "▇";
		}
		gotoxy(0, height - i - 1);
		for (int k = 0; k < t_width; k++)
		{
			cout << "▇";
		}
		Sleep(100);
	}
	system("cls");
	Sleep(1000);
	for (int i = 0; i < t_width; i++)
	{
		for (int j = 0; j < t_height; j++)
		{
			cout << "▇";
			if (j == t_width - 1)
				cout << endl;
		}
	}
	Sleep(1000);
	system("cls");
	Sleep(1000);
 
}
 
void BOSScartoon()
{
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "****";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "****";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "    ";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "    ";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "****";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "****";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "    ";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "    ";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "****";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "****";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "    ";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "    ";
	}
	Sleep(1000);
	printBOSS();
}
 
void end_menu()
{
	system("cls");
	Sleep(3000);
	gotoxy(width / 2 - 8, height - 1);
	cout << "你失败了,但这不是你的过错,只是这次的英雄并不是你......";
	Sleep(300);
	for (int i = height - 2; i > 18; i--)
	{
		gotoxy(width / 2 - 8, i + 1);
		cout << "                                                        ";
		gotoxy(width / 2 - 8, i);
		cout << "你失败了,但这不是你的过错,只是这次的英雄并不是你......";
		Sleep(300);
	}
	
	Sleep(10000000);
}
 
void success_menu()
{
	for (int i = 16; i > 1; i--)
	{
		gotoxy(2, i);
		cout << "                                                                     ";
		Sleep(100);
	}
	Sleep(1000);
	gotoxy(width / 2 - 3, 2);
	cout << "游戏胜利";
	Sleep(300);
	for (int i = 3; i < 17; i++)
	{
		gotoxy(width / 2 - 3, i - 1);
		cout << "        ";
		gotoxy(width / 2 - 3, i);
		cout << "游戏胜利";
		Sleep(300);
	}
	Sleep(100000);
}
 
void printBOSS()
{
	gotoxy(0, 0);
	for (int i = 0; i < width; i++)
		cout << "#";
	gotoxy(0, height - 1);
	for (int i = 0; i < width; i++)
		cout << "#";
	for (int i = 0; i < height; i++)
	{
		gotoxy(0, i);
		cout << "#";
	}
	for (int i = 0; i < height; i++)
	{
		gotoxy(width - 1, i);
		cout << "#";
	}
	for (int i = 2; i < 16; i++)
	{
		gotoxy(37, i);
		cout << "\\ ++ /";
		if (i > 2)
		{
			gotoxy(33, i - 1);
			cout << "\\  \\  ++  /  /";
 
		}
		if (i > 3)
		{
			gotoxy(30, i - 2);
			cout << "\\ \\  \\   ++   /  / /";
		}
		if (i > 4)
		{
			gotoxy(27, i - 3);
			cout << "+++++++************+++++++";
			gotoxy(27, i - 4);
			cout << "                          ";
		}
		if (i > 5)
		{
			gotoxy(32, i - 4);
			cout << "****************";
			gotoxy(32, i - 5);
			cout << "                ";
		}
		if (i > 6)
		{
			gotoxy(18, i - 5);
			cout << "***  ***00******   ******   ******00***  ***";
			gotoxy(18, i - 6);
			cout << "                                            ";
		}
		if (i > 7)
		{
			gotoxy(19, i - 6);
			cout << "******00****   **  ****  **   ****00******";
			gotoxy(19, i - 7);
			cout << "                                          ";
		}
		if (i > 8)
		{
			gotoxy(24, i - 7);
			cout << "*****    *00*      *00*    *****";
			gotoxy(24, i - 8);
			cout << "                                ";
		}
		if (i > 9)
		{
			gotoxy(22, i - 8);
			cout << "****   ****0000* ** *0000****   ****";
			gotoxy(22, i - 9);
			cout << "                                    ";
		}
		if (i > 10)
		{
			gotoxy(23, i - 9);
			cout << "*********0000***  ***0000*********";
			gotoxy(23, i - 10);
			cout << "                                  ";
		}
		if (i > 11)
		{
			gotoxy(14, i - 10);
			cout << "*********** ************    ************ ***********";
			gotoxy(14, i - 11);
			cout << "                                                    ";
		}
		if (i > 12)
		{
			gotoxy(14, i - 11);
			cout << "********   ***  *  *  *      *  *  *  ***   ********";
			gotoxy(14, i - 12);
			cout << "                                                    ";
		}
		if (i > 13)
		{
			gotoxy(9, i - 12);
			cout << "************       ******    ****    ******       ************";
			gotoxy(9, i - 13);
			cout << "                                                              ";
		}
		Sleep(300);
	}
 
 
 
}

main函数实现:

int main()
{
	system("mode con cols=120 lines=40");
	clearCursor();
	cartoonGameMenu();
	start_menu();
	cartoonStartMenu();
	game_menu();
	BOSSgame();
}

附加:

感谢“杨某一辰”提出的建议杨某一辰的博客_CSDN博客-c++,一些有趣的东西,c领域博主杨某一辰擅长c++,一些有趣的东西,c,等方面的知识,杨某一辰关注c++,区块链,c语言,人工智能领域.https://blog.csdn.net/langhat?type=blog

以下是他提供的改进代码

#include<iostream>
#include<Windows.h>
#include<conio.h>
#include<cstdlib>
#include<time.h>
#include<stdlib.h>
#include<cmath>
 
//飞机活动大小
#define width 80
#define height 40
//窗口大小
#define t_width 120
#define t_height 40
using namespace std;
 
void gotoxy(short x, short y);
void Game();
 
void enemyAppear(char arr[width + 10][height]);
void enemyMove(char arr[width + 10][height]);
 
void bulletMove(char arr[width + 10][height]);
void BOSSgame();
void shotBOSS(char arr[width + 10][height]);
void difficulty();
 
void cartoonStartMenu();
void start_menu();
void game_menu();
void cartoonGameMenu();
void end_menu();
void success_menu();
void BOSScartoon();
void printBOSS();
 
extern int score = 0;
extern int bullet = 99999;
extern int HP = 100000;
extern int BOSSHP = 500;
extern int damage = 3;
extern int enemySpeed = 10;
extern int BOSSspeed = 15;
extern int Number = 4;
extern int BOSS = 0;
 
//清除光标
void clearCursor()
{
	HANDLE hOut;
	CONSOLE_CURSOR_INFO cur = { 1,0 };
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(hOut, &cur);
}
 
 
//使光标移动到指定位置
void gotoxy(short x, short y)
{
	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x,y };
	SetConsoleCursorPosition(hOut, pos);
}
 
char enemyArr[width + 10][height] = { 0 };
char myBulletArr[width + 10][height] = { 0 };
 
//飞机图像
void printPlane(short x, short y)
{
	gotoxy(x, y);
	cout << "*";//头
	gotoxy(x, y + 1);
	cout << "*";//尾
	gotoxy(x - 1, y + 1);
	cout << "*";//左翼
	gotoxy(x + 1, y + 1);
	cout << "*";//右翼
}
 
 
class myPlane
{
public:
	//头的坐标
	short x = width / 2;
	short y = height / 2;
 
 
 
	//初始化飞机
	void initMyPlane()
	{
		printPlane(x, y);
	}
 
	//清除飞机移动前位置图像
	void clearMyplane()
	{
		gotoxy(x, y);
		cout << " ";//头
		gotoxy(x, y + 1);
		cout << " ";//尾
		gotoxy(x - 1, y + 1);
		cout << " ";//左翼
		gotoxy(x + 1, y + 1);
		cout << " ";//右翼
	}
 
 
	//射击
	void shoot(char myArr[width + 10][height])
	{
		if (bullet == 0)
			return;
		myArr[x][y - 1] = '*';
		gotoxy(x, y - 1);
		cout << "*";
		bullet--;
		gotoxy(width + (t_width - width) / 2, 9);
		cout << "   ";
		gotoxy(width + (t_width - width) / 2, 9);
		cout << bullet;
	}
 
	//飞机移动+游戏中的菜单栏功能实现
	void myPlaneMove(char arr[width + 10][height])
	{
		//判断是否有键盘操作
		if (_kbhit())
		{
			char move;
			move = _getch();
			switch (move)
			{
			case 'w':
				if (BOSS == 1)
					if (y == 16)
						break;
				//判断是否是边界
				if (y == 1)
					break;
				//清除原图像
				clearMyplane();
				y--;
				//打印新位置
				printPlane(x, y);
				break;
			case 'a':
				if (x - 1 == 1)
					break;
				clearMyplane();
				x--;
				printPlane(x, y);
				break;
			case 's':
				if (y + 1 == height - 2)
					break;
				clearMyplane();
				y++;
				printPlane(x, y);
				break;
			case 'd':
				if (x + 1 == width - 2)
					break;
				clearMyplane();
				x++;
				printPlane(x, y);
				break;
			case 'j':
				shoot(myBulletArr);
			case 'W':
				if (BOSS == 1)
					if (y == 16)
						break;
				//判断是否是边界
				if (y == 1)
					break;
				//清除原图像
				clearMyplane();
				y--;
				//打印新位置
				printPlane(x, y);
				break;
			case 'A':
				if (x - 1 == 1)
					break;
				clearMyplane();
				x--;
				printPlane(x, y);
				break;
			case 'S':
				if (y + 1 == height - 2)
					break;
				clearMyplane();
				y++;
				printPlane(x, y);
				break;
			case 'D':
				if (x + 1 == width - 2)
					break;
				clearMyplane();
				x++;
				printPlane(x, y);
				break;
            //控制射击
			case 'J':
				shoot(myBulletArr);
			}
		}
	}
};
 
myPlane p;//创捷我方飞机
 
//难度选择
void difficulty()
{
	gotoxy(t_width / 2 - 5, t_height / 2 - 4);
	cout << "1.简单模式";
	gotoxy(t_width / 2 - 5, t_height / 2);
	cout << "2.普通模式";
	gotoxy(t_width / 2 - 5, t_height / 2 + 4);
	cout << "3.困难模式";
	char choice;
	choice = _getch();
	switch (choice)
	{
	case '1':
		enemySpeed = 50;
		BOSSspeed = 25;
		HP = 1000;
		damage = 5;
		bullet = 50;
		Number = 2;
		break;
	case '2':
		enemySpeed = 10;
		BOSSspeed = 15;
		HP = 500;
		damage = 3;
		bullet = 30;
		Number = 4;
		break;
	case '3':
		enemySpeed = 8;
		BOSSspeed = 5;
		HP = 100;
		damage = 2;
		bullet = 10;
		Number = 6;
		break;
	}
 
}
 
void shotEnemy(char enemyArr[width + 10][height], char myArr[width + 10][height])
{
	for (int i = 2; i < width; i++)
		for (int j = 2; j < height; j++)
		{
			if (myArr[i][j] == '*' || myArr[i][j] == '@')
			{
				//碰头
				if (enemyArr[i][j - 2] == '+')
				{
					myArr[i][j] = '@';
 
					gotoxy(i, j - 1);
					cout << "*";
				}
				//碰左翼
				else if (enemyArr[i][j - 2] == 'p')
				{
					myArr[i][j] = '@';
 
					gotoxy(i, j - 1);
					cout << "*";
				}
				//碰右翼
				else if (enemyArr[i][j - 2] == 'q')
				{
					myArr[i][j] = '@';
 
					gotoxy(i, j - 1);
					cout << "*";
				}
				//碰头
				if (enemyArr[i][j - 1] == '+')
				{
					myArr[i][j] = 0;
					enemyArr[i][j - 1] = 0;
					enemyArr[i - 1][j - 2] = 0;
					enemyArr[i + 1][j - 2] = 0;
					enemyArr[i][j - 2] = 0;
 
 
					gotoxy(i, j - 1);
					cout << " ";
					gotoxy(i - 1, j - 2);
					cout << " ";
					gotoxy(i + 1, j - 2);
					cout << " ";
					gotoxy(i, j - 2);
					cout << " ";
					gotoxy(i, j);
					cout << " ";
 
				}
 
				//碰左翼
				else if (enemyArr[i][j - 1] == 'p')
				{
					myArr[i][j] = 0;
					enemyArr[i][j - 1] = 0;
					enemyArr[i - 1][j] = 0;
					enemyArr[i - 1][j - 1] = 0;
					enemyArr[i - 2][j - 1] = 0;
 
 
					gotoxy(i, j - 1);
					cout << " ";
					gotoxy(i - 1, j);
					cout << " ";
					gotoxy(i - 1, j - 1);
					cout << " ";
					gotoxy(i - 2, j - 1);
					cout << " ";
					gotoxy(i, j);
					cout << " ";
				}
 
				//碰右翼
				else if (enemyArr[i][j - 1] == 'q')
				{
					myArr[i][j] = 0;
					enemyArr[i][j - 1] = 0;
					enemyArr[i + 1][j] = 0;
					enemyArr[i + 1][j - 1] = 0;
					enemyArr[i + 2][j - 1] = 0;
 
 
					gotoxy(i, j - 1);
					cout << " ";
					gotoxy(i + 1, j);
					cout << " ";
					gotoxy(i + 1, j - 1);
					cout << " ";
					gotoxy(i + 2, j - 1);
					cout << " ";
					gotoxy(i, j);
					cout << " ";
				}
 
 
 
			}
		}
}
 
//子弹移动
void bulletMove(char myArr[width + 10][height])
{
	for (int i = 2; i < width; i++)
		for (int j = 2; j < height; j++)
		{
			if (myArr[i][j] == '*')
			{
				myArr[i][j] = 0;
				myArr[i][j - 1] = '*';
				gotoxy(i, j - 1);
				cout << "*";
				gotoxy(i, j);
				cout << " ";
			}
 
		}
 
	//清除触界子弹
	for (int k = 1; k < width; k++)
	{
		if (myArr[k][1] == '*')
		{
			gotoxy(k, 1);
			cout << " ";
			myArr[k][1] = 0;
			Beep(500,150);
		}
	}
}
 
//随机出现敌机
void enemyAppear(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	//随机敌机数量
	int number = rand() % 6 + Number;
	//随机敌机位置
	for (int i = 0; i < number; i++)
	{
		short x = rand() % (width - 4) + 2;
		//防止敌机重叠
		if (enemyArr[x][2] == '+' || enemyArr[x - 1][2] == '+' || enemyArr[x + 1][2] == '+' || enemyArr[x - 2][2] == '+' || enemyArr[x + 2][2] == '+')
			continue;
		short y = 2;
		enemyArr[x][y] = '+';
		enemyArr[x][y - 1] = 'T';
		enemyArr[x - 1][y - 1] = 'q';
		enemyArr[x + 1][y - 1] = 'p';
 
		gotoxy(x, y);
		cout << "+";//头
		gotoxy(x, y - 1);
		cout << "T";//尾   
		gotoxy(x - 1, y - 1);
		cout << "q";//右翼     //+++ 
		gotoxy(x + 1, y - 1);  // 0   
		cout << "p";//左翼
 
	}
}
 
void enemyMove(char enemyArr[width + 10][height])
{
	for (int i = width; i > 0; i--)
		for (int j = height - 3; j > 0; j--)
		{
			if (enemyArr[i][j] == '+')
			{
				enemyArr[i][j - 1] = 0;
				enemyArr[i - 1][j - 1] = 0;
				enemyArr[i + 1][j - 1] = 0;
 
				enemyArr[i][j] = 'T';
				enemyArr[i][j + 1] = '+';
				enemyArr[i - 1][j] = 'q';
				enemyArr[i + 1][j] = 'p';
				gotoxy(i, j - 1);
				cout << " ";
				gotoxy(i - 1, j - 1);
				cout << " ";
				gotoxy(i + 1, j - 1);
				cout << " ";
				gotoxy(i, j + 1);
				cout << "+";
				gotoxy(i, j);
				cout << "T";
				gotoxy(i - 1, j);
				cout << "q";
				gotoxy(i + 1, j);
				cout << "p";
			}
		}
 
	//清除触界敌机
	for (int i = 0; i < width; i++)
	{
		if (enemyArr[i][height - 2] == '+')
		{
			enemyArr[i][height - 2] = 0;
			enemyArr[i][height - 3] = 0;
			enemyArr[i + 1][height - 3] = 0;
			enemyArr[i - 1][height - 3] = 0;
			gotoxy(i, height - 2);
			cout << " ";
			gotoxy(i - 1, height - 3);
			cout << " ";
			gotoxy(i + 1, height - 3);
			cout << " ";
			gotoxy(i, height - 3);
			cout << " ";
			score++;
		}
	}
}
 
//判断是否触碰敌机
bool judgeEnemy(short x, short y, char enemyArr[width + 10][height])
{
	if (enemyArr[x][y] == '+' || enemyArr[x][y] == 'p' || enemyArr[x][y] == 'q' || enemyArr[x - 1][y + 1] == '+' || enemyArr[x - 1][y + 1] == 'p' || enemyArr[x - 1][y + 1] == 'q'
		|| enemyArr[x + 1][y + 1] == '+' || enemyArr[x + 1][y + 1] == 'p' || enemyArr[x + 1][y + 1] == 'q' || enemyArr[x][y + 1] == '+' || enemyArr[x][y + 1] == 'p' || enemyArr[x][y + 1] == 'q')
	{
		Beep(1000,30);
		return true;
	}
	else
	{
 
		gotoxy(width + (t_width - width) / 2, 11);
		cout << HP << "    ";
		return false;
	}
}
 
void Game()
{
	//存储敌机坐标
	system("cls");
	difficulty();
	system("cls");
	cartoonStartMenu();
	game_menu();
	p.initMyPlane();
	while (true)
	{
		enemyAppear(enemyArr);
        //这里可能有点难理解,可以先把外层循环去掉只看内层循环
        //Sleep(1)是停止1ms也就是说每enemySpeed毫秒子弹,敌军移动一次
        //把外层循环加进来就是10*
        //这样写的目的是使得每一毫秒程序enemySpeed毫秒敌军出现一次都能检测是否有按键移动射击操作
		for (int i = 0; i < 10; i++)
		{
 
			for (int j = 0; j < enemySpeed; j++) {
				p.myPlaneMove(enemyArr);
				if (judgeEnemy(p.x, p.y, enemyArr))
				{
					end_menu();
				}
				Beep(750,5);
			}
			Beep(750,5);
			bulletMove(myBulletArr);
			shotEnemy(enemyArr, myBulletArr);
			enemyMove(enemyArr);
			gotoxy(width + (t_width - width) / 2, 7);
			cout << score;
            //分数大于200进入BOSS战
			if (score > 200){
				Beep(1000,300);
				Beep(250,300);
				BOSSgame();
			}
		}
	}
 
}
 
//BOSS子弹移动
void enemyBulletMove(char enemyArr[width + 10][height])
{
	for (int i = width; i > 0; i--)
		for (int j = height - 3; j > 0; j--)
		{
			if (enemyArr[i][j] == '+')
			{
				enemyArr[i][j] = 0;
				enemyArr[i][j + 1] = '+';
 
				gotoxy(i, j);
				cout << " ";
 
				gotoxy(i, j + 1);
				cout << "+";
			}
		}
 
	//清除触界敌军子弹
	for (int i = 0; i < width; i++)
	{
		if (enemyArr[i][height - 2] == '+')
		{
			enemyArr[i][height - 2] = 0;
 
			gotoxy(i, height - 2);
			cout << " ";
			Beep(350,300);
		}
	}
}
 
 
//持续射击
void keepShooting(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	for (int j = 0; j < 100; j++)
	{
		int x = rand() % 78 + 1;
		gotoxy(x, 16);
		cout << "+";
		enemyArr[x][16] = '+';
		gotoxy(79 - x, 16);
		cout << "+";
		enemyArr[79 - x][16] = '+';
		for (int j = 0; j < BOSSspeed; j++)
		{
			Sleep(1);
			p.myPlaneMove(enemyArr);
		}
		enemyBulletMove(enemyArr);
		bulletMove(myBulletArr);
		shotBOSS(myBulletArr);
		Beep(756,150);
	}
}
 
 
//技能1
void skillA(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	for (int z = 0; z < 3; z++)
	{
		for (int i = width / 2 - 5; i > 2; i--)
		{
			gotoxy(i, 16);
			cout << "+";
			enemyArr[i][16] = '+';
			gotoxy((width - i - 1), 16);
			cout << "+";
			enemyArr[width - i - 1][16] = '+';
			for (int j = 0; j < BOSSspeed; j++)
			{
				Sleep(1);
				p.myPlaneMove(enemyArr);
			}
			enemyBulletMove(enemyArr);
			bulletMove(myBulletArr);
			shotBOSS(myBulletArr);
		}
	}
 
}
 
 
//技能2
void skillB(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	for (int z = 0; z < 3; z++)
	{
		int x = rand() % 76 + 2;
		gotoxy(x, 16);
		cout << "!";
		gotoxy(79 - x, 16);
		cout << "!";
		for (int k = 0; k < 10; k++)
		{
			for (int j = 0; j < BOSSspeed; j++)
			{
				Sleep(1);
				p.myPlaneMove(enemyArr);
			}
			enemyBulletMove(enemyArr);
			bulletMove(myBulletArr);
			shotBOSS(myBulletArr);
		}
		for (int i = 16; i < height - 1; i++)
		{
			gotoxy(x, i);
			cout << "+";
			enemyArr[x][i] = '+';
			gotoxy(x + 1, i);
			cout << "+";
			enemyArr[x + 1][i] = '+';
			gotoxy(x - 1, i);
			cout << "+";
			enemyArr[x - 1][i] = '+';
			gotoxy(79 - x, i);
			cout << "+";
			enemyArr[79 - x][i] = '+';
			gotoxy(79 - x - 1, i);
			cout << "+";
			enemyArr[79 - x - 1][i] = '+';
			gotoxy(79 - x + 1, i);
			cout << "+";
			enemyArr[79 - x + 1][i] = '+';
		}
	}
}
 
//技能3
void skillC(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	int x = rand() % 6 + 37;
	gotoxy(x, 16);
	cout << "+";
	enemyArr[x][16] = '+';
	gotoxy(79 - x, 16);
	cout << "+";
	enemyArr[79 - x][16] = '+';
	p.myPlaneMove(enemyArr);
	enemyBulletMove(enemyArr);
	bulletMove(myBulletArr);
	shotBOSS(myBulletArr);
	for (int j = 0; j < 50; j++)
	{
		int dir = rand() % 2 - 1;
		x = x + dir;
		gotoxy(x, 16);
		cout << "+";
		enemyArr[x][16] = '+';
		gotoxy(79 - x, 16);
		cout << "+";
		enemyArr[79 - x][16] = '+';
		for (int j = 0; j < BOSSspeed; j++)
		{
			Sleep(1);
			p.myPlaneMove(enemyArr);
		}
		enemyBulletMove(enemyArr);
		bulletMove(myBulletArr);
		shotBOSS(myBulletArr);
	}
}
 
 
//BOSS血条
void BOSSHPcartoon()
{
	gotoxy(13, 1);
	cout << "HP:";
	gotoxy(16, 1);
	for (int i = 0; i < BOSSHP / 10; i++)
		cout << "▇";
	cout << "    ";
}
 
//射击BOSS检测
void shotBOSS(char myArr[width + 10][height])
{
	//尾部
	for (int i = 9; i < 14; i++)
	{
		if (myArr[i][4] == '*')
		{
			BOSSHP = BOSSHP - damage / 2;
			myArr[i][4] = 0;
			gotoxy(i, 4);
			cout << " ";
		}
		if (myArr[79 - i][4] == '*')
		{
			BOSSHP = BOSSHP - damage / 2;
			myArr[79 - i][4] = 0;
			gotoxy(79 - i, 4);
			cout << " ";
		}
	}
	//中后部
	for (int i = 14; i < 18; i++)
	{
		if (myArr[i][6] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[i][6] = 0;
			gotoxy(i, 6);
			cout << " ";
		}
		if (myArr[79 - i][6] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[79 - i][6] = 0;
			gotoxy(79 - i, 6);
			cout << " ";
		}
	}
	//中前部
	for (int i = 18; i < 29; i++)
	{
		if (myArr[i][11] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[i][11] = 0;
			gotoxy(i, 11);
			cout << " ";
		}
		if (myArr[79 - i][11] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[79 - i][11] = 0;
			gotoxy(79 - i, 11);
			cout << " ";
		}
	}
	for (int i = 27; i < 40; i++)
	{
		if (myArr[i][13] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[i][13] = 0;
			gotoxy(i, 13);
			cout << " ";
		}
		if (myArr[79 - i][13] == '*')
		{
			BOSSHP = BOSSHP - damage;
			myArr[79 - i][13] = 0;
			gotoxy(79 - i, 13);
			cout << " ";
		}
	}
	//头后部
	for (int i = 31; i < 40; i++)
	{
		if (myArr[i][15] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[i][15] = 0;
			gotoxy(i, 15);
			cout << " ";
		}
		if (myArr[79 - i][15] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[79 - i][15] = 0;
			gotoxy(79 - i, 15);
			cout << " ";
		}
	}
	//头部
	for (int i = 37; i < 40; i++)
	{
		if (myArr[i][16] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[i][16] = 0;
			gotoxy(i, 16);
			cout << " ";
		}
		if (myArr[79 - i][16] == '*')
		{
			BOSSHP = BOSSHP - 2 * damage;
			myArr[79 - i][16] = 0;
			gotoxy(79 - i, 16);
			cout << " ";
		}
	}
	BOSSHPcartoon();
	if (BOSSHP <= 0)
		success_menu();
}
 
 
void BOSSshot(char enemyArr[width + 10][height])
{
	srand((unsigned int)time(NULL));
	while (true)
	{
		int i = rand() % 4;
		switch (i)
		{
		case 0:
			keepShooting(enemyArr);
			break;
		case 1:
			skillA(enemyArr);
			break;
		case 2:
			skillB(enemyArr);
			break;
		case 3:
			skillC(enemyArr);
			break;
		}
	}
}
 
void BOSSgame()
{
	BOSS = 1;
	bullet = 1000;
	for (int i = 2; i < width; i++)
		for (int j = 2; j < height; j++)
		{
			if (enemyArr[i][j] == '+')
			{
				enemyArr[i][j] = 0;
				enemyArr[i - 1][j - 1] = 0;
				enemyArr[i + 1][j - 1] = 0;
				enemyArr[i][j - 1] = 0;
 
 
				gotoxy(i - 1, j - 1);
				cout << " ";
				gotoxy(i + 1, j - 1);
				cout << " ";
				gotoxy(i, j - 1);
				cout << " ";
				gotoxy(i, j);
				cout << " ";
			}
		}
 
	BOSScartoon();
	BOSSHPcartoon();
	BOSSshot(enemyArr);
}
 
//开始菜单
void start_menu()
{
	for (int i = t_height - 1; i >= t_height / 2 - 4; i--)
	{
		gotoxy(t_width / 2 - 5, i + 1);
		cout << "        ";
		gotoxy(t_width / 2 - 5, i);
		cout << "飞机大战";
		if (i < t_height - 2 - 4)
		{
			gotoxy(t_width / 2 - 6, i + 5);
			cout << "          ";
			gotoxy(t_width / 2 - 6, i + 4);
			cout << "y.进入游戏";
 
		}
		if (i < t_height - 2 - 8)
		{
			gotoxy(t_width / 2 - 6, i + 9);
			cout << "          ";
			gotoxy(t_width / 2 - 6, i + 8);
			cout << "n.退出游戏";
		}
		Sleep(300);
	}
	while (true)
	{
		if (_kbhit())
		{
			char input = _getch();
 
 
			switch (input)
			{
			case 'n':
				system("cls");
				cout << "退出成功" << endl;
				exit(0);
				break;
			case 'N':
				system("cls");
				cout << "退出成功" << endl;
				exit(0);
				break;
			case 'y':
				system("cls");
				cout << "进入游戏" << endl;
				Sleep(1000);
				system("cls");
				Game();
 
				break;
			
			case 'Y':
				system("cls");
				cout << "进入游戏" << endl;
				Sleep(1000);
				system("cls");
				Game();
 
				break;
			}
		}
		
	}
}
 
void cartoonStartMenu()
{
 
	gotoxy(t_width / 2 - 8, t_height / 2 - 6);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 5);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 4);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 3);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 2);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 1);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 + 1);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 + 2);
	cout << "************";
	Sleep(1000);
	system("cls");
	gotoxy(t_width / 2 - 8, t_height / 2 - 6);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 5);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 4);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 3);
	cout << "           *";
	gotoxy(t_width / 2 - 8, t_height / 2 - 2);
	cout << "************";
	gotoxy(t_width / 2 - 8, t_height / 2 - 1);
	cout << "*           ";
	gotoxy(t_width / 2 - 8, t_height / 2);
	cout << "*           ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 1);
	cout << "*           ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 2);
	cout << "************";
	Sleep(1000);
	system("cls");
	gotoxy(t_width / 2 - 8, t_height / 2 - 6);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 5);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 4);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 3);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 2);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 - 1);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 1);
	cout << "     **     ";
	gotoxy(t_width / 2 - 8, t_height / 2 + 2);
	cout << "     **     ";
	Sleep(1000);
	system("cls");
}
 
 
//地图框架和右菜单
void game_menu()
{
	//框架
	gotoxy(0, 0);
	for (int i = 0; i < width; i++)
		cout << "#";
	gotoxy(0, height - 1);
	for (int i = 0; i < width; i++)
		cout << "#";
	for (int i = 0; i < height; i++)
	{
		gotoxy(0, i);
		cout << "#";
	}
	for (int i = 0; i < height; i++)
	{
		gotoxy(width - 1, i);
		cout << "#";
	}
 
 
	//右部菜单
	gotoxy(width + (t_width - width) / 2 - 6, 5);
	cout << "代号:NJTECH";
	gotoxy(width + (t_width - width) / 2 - 6, 7);
	cout << "得分:";
	gotoxy(width + (t_width - width) / 2 - 6, 9);
	cout << "子弹:";
	gotoxy(width + (t_width - width) / 2 - 6, 11);
	cout << "血量:";
 
	gotoxy(width, 13);
	cout << "                   **                   ";
	gotoxy(width, 14);
	cout << "                   **                   ";
	gotoxy(width, 15);
	cout << "                   **                   ";
	gotoxy(width, 16);
	cout << "                  ****                  ";
	gotoxy(width, 17);
	cout << "                 ******                 ";
	gotoxy(width, 18);
	cout << "            *  ****  ****  *            ";
	gotoxy(width, 19);
	cout << "           ******************           ";
	gotoxy(width, 20);
	cout << "          ****   ******   ****          ";
	gotoxy(width, 21);
	cout << "       **************************       ";
	gotoxy(width, 22);
	cout << "             ****      ****             ";
	gotoxy(width, 23);
	cout << "               **      **               ";
	gotoxy(width, 24);
	cout << "                *      *                ";
	//规则
	gotoxy(width + (t_width - width) / 2 - 14, 28);
	cout << "w 前进";
	gotoxy(width + (t_width - width) / 2 - 14, 30);
	cout << "s 后退";
	gotoxy(width + (t_width - width) / 2 - 14, 32);
	cout << "d 右移";
	gotoxy(width + (t_width - width) / 2 - 14, 34);
	cout << "a 左移";
	gotoxy(width + (t_width - width) / 2 - 14, 36);
	cout << "j 射击";
}
 
 
 
void cartoonGameMenu()
{
	for (int i = 0; i < height / 2; i++)
	{
		gotoxy(0, i);
		for (int j = 0; j < t_width; j++)
		{
			cout << "▇";
		}
		gotoxy(0, height - i - 1);
		for (int k = 0; k < t_width; k++)
		{
			cout << "▇";
		}
		Sleep(100);
	}
	system("cls");
	Sleep(1000);
	for (int i = 0; i < t_width; i++)
	{
		for (int j = 0; j < t_height; j++)
		{
			cout << "▇";
			if (j == t_width - 1)
				cout << endl;
		}
	}
	Sleep(1000);
	system("cls");
	Sleep(1000);
 
}
 
void BOSScartoon()
{
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "****";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "****";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "    ";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "    ";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "****";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "****";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "    ";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "    ";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "****";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "****";
	}
	Sleep(1000);
	for (int i = 0; i < 9; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 - 8 + i);
		cout << "    ";
	}
	for (int i = 0; i < 3; i++)
	{
		gotoxy(width / 2 - 1, t_height / 2 + 2 + i);
		cout << "    ";
	}
	Sleep(1000);
	printBOSS();
}
 
void end_menu()
{
	system("cls");
	Sleep(3000);
	gotoxy(width / 2 - 8, height - 1);
	cout << "你失败了,但这不是你的过错,只是这次的英雄并不是你......";
	Sleep(300);
	for (int i = height - 2; i > 18; i--)
	{
		gotoxy(width / 2 - 8, i + 1);
		cout << "                                                        ";
		gotoxy(width / 2 - 8, i);
		cout << "你失败了,但这不是你的过错,只是这次的英雄并不是你......";
		Sleep(300);
	}
	
	Sleep(10000000);
}
 
void success_menu()
{
	for (int i = 16; i > 1; i--)
	{
		gotoxy(2, i);
		cout << "                                                                     ";
		Sleep(100);
	}
	Sleep(1000);
	gotoxy(width / 2 - 3, 2);
	cout << "游戏胜利";
	Sleep(300);
	for (int i = 3; i < 17; i++)
	{
		gotoxy(width / 2 - 3, i - 1);
		cout << "        ";
		gotoxy(width / 2 - 3, i);
		cout << "游戏胜利";
		Sleep(300);
	}
	Sleep(100000);
}
 
void printBOSS()
{
	gotoxy(0, 0);
	for (int i = 0; i < width; i++)
		cout << "#";
	gotoxy(0, height - 1);
	for (int i = 0; i < width; i++)
		cout << "#";
	for (int i = 0; i < height; i++)
	{
		gotoxy(0, i);
		cout << "#";
	}
	for (int i = 0; i < height; i++)
	{
		gotoxy(width - 1, i);
		cout << "#";
	}
	for (int i = 2; i < 16; i++)
	{
		gotoxy(37, i);
		cout << "\\ ++ /";
		if (i > 2)
		{
			gotoxy(33, i - 1);
			cout << "\\  \\  ++  /  /";
 
		}
		if (i > 3)
		{
			gotoxy(30, i - 2);
			cout << "\\ \\  \\   ++   /  / /";
		}
		if (i > 4)
		{
			gotoxy(27, i - 3);
			cout << "+++++++************+++++++";
			gotoxy(27, i - 4);
			cout << "                          ";
		}
		if (i > 5)
		{
			gotoxy(32, i - 4);
			cout << "****************";
			gotoxy(32, i - 5);
			cout << "                ";
		}
		if (i > 6)
		{
			gotoxy(18, i - 5);
			cout << "***  ***00******   ******   ******00***  ***";
			gotoxy(18, i - 6);
			cout << "                                            ";
		}
		if (i > 7)
		{
			gotoxy(19, i - 6);
			cout << "******00****   **  ****  **   ****00******";
			gotoxy(19, i - 7);
			cout << "                                          ";
		}
		if (i > 8)
		{
			gotoxy(24, i - 7);
			cout << "*****    *00*      *00*    *****";
			gotoxy(24, i - 8);
			cout << "                                ";
		}
		if (i > 9)
		{
			gotoxy(22, i - 8);
			cout << "****   ****0000* ** *0000****   ****";
			gotoxy(22, i - 9);
			cout << "                                    ";
		}
		if (i > 10)
		{
			gotoxy(23, i - 9);
			cout << "*********0000***  ***0000*********";
			gotoxy(23, i - 10);
			cout << "                                  ";
		}
		if (i > 11)
		{
			gotoxy(14, i - 10);
			cout << "*********** ************    ************ ***********";
			gotoxy(14, i - 11);
			cout << "                                                    ";
		}
		if (i > 12)
		{
			gotoxy(14, i - 11);
			cout << "********   ***  *  *  *      *  *  *  ***   ********";
			gotoxy(14, i - 12);
			cout << "                                                    ";
		}
		if (i > 13)
		{
			gotoxy(9, i - 12);
			cout << "************       ******    ****    ******       ************";
			gotoxy(9, i - 13);
			cout << "                                                              ";
		}
		Sleep(300);
	}
 
 
 
}
 
int main()
{
	system("mode con cols=120 lines=40");
	clearCursor();
	cartoonGameMenu();
	start_menu();
	cartoonStartMenu();
	game_menu();
	BOSSgame();
}

 

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
C++飞机大战游戏是一款经典的2D射击游戏,玩家操控飞机战斗。下面是一个简单的C++飞机大战游戏代码示例: ```cpp #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y; // 飞机的坐标 int fruitX, fruitY; // 敌机的坐标 int score; // 得分 enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else cout << " "; if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x >= width) x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); // 控制游戏速度 Sleep(10); } return 0; } ``` 这是一个简单的飞机大战游戏代码示例,玩家可以通过键盘控制飞机的移动,吃到敌机可以得分。游戏会根据玩家的操作进刷新,并且控制游戏的速度。你可以根据自己的需求修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值