#include<iostream> // 输入输出流头文件,用于控制台输入输出
#include<windows.h> // Windows系统API头文件,用于控制台光标、窗口操作
#include<conio.h> // 控制台输入头文件,用于无回显按键检测(_getch)、键盘敲击检测(_kbhit)
#include<time.h> // 时间头文件,用于生成随机数种子
#include<string> // 字符串头文件,用于字符串操作
using namespace std;
/*=============== 所有结构体定义 ===============*/
// 帧/矩形框结构体:描述一个矩形区域的坐标和标记
typedef struct Frame
{
COORD position[2]; // 坐标数组,position[0]左上角,position[1]右下角
int flag; // 状态标记位,备用
}Frame;
/*=============== 所有工具函数 ===============*/
// 功能:设置控制台光标位置(参数:COORD坐标结构体)
// 参数 a:包含X(列)、Y(行)的光标目标坐标
void SetPos(COORD a)
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出句柄
SetConsoleCursorPosition(out, a); // 设置光标位置
}
// 功能:重载设置光标位置(参数:列、行)
// 参数 i:列坐标(X),j:行坐标(Y)
void SetPos(int i, int j)
{
COORD pos={i, j}; // 构造坐标结构体
SetPos(pos); // 调用上面的光标设置函数
}
// 功能:隐藏控制台光标,优化游戏画面显示
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 光标信息:大小1,隐藏(0)
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// 功能:绘制水平行:在第y行,x1到x2列之间填充字符ch
// 参数 y:行号,x1:起始列,x2:结束列,ch:填充字符
void drawRow(int y, int x1, int x2, char ch)
{
SetPos(x1,y); // 光标移动到行首
for(int i = 0; i <= (x2-x1); i++) // 循环填充整行
cout<<ch;
}
// 功能:重载绘制水平行(参数:两个COORD坐标,要求Y相同)
// 参数 a、b:线段起止坐标,ch:填充字符
void drawRow(COORD a, COORD b, char ch)
{
if(a.Y == b.Y) // 判断是否为水平行
drawRow(a.Y, a.X, b.X, ch);
else
{
SetPos(0, 25);
cout<<"error code 01:无法填充行,因为两个坐标的纵坐标不相等";
system("pause");
}
}
// 功能:绘制垂直列:在第x列,y1到y2行之间填充字符ch
// 参数 x:列号,y1:起始行,y2:结束行,ch:填充字符
void drawCol(int x, int y1, int y2, char ch)
{
int y=y1;
while(y!=y2+1)
{
SetPos(x, y);
cout<<ch;
y++;
}
}
// 功能:重载绘制垂直列(参数:两个COORD坐标,要求X相同)
void drawCol(COORD a, COORD b, char ch)
{
if(a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch);
else
{
SetPos(0, 25);
cout<<"error code 02:无法填充列,因为两个坐标的横坐标不相等";
system("pause");
}
}
// 功能:绘制矩形边框:由左上角a、右下角b组成,行用row,列用col
// 参数 a:左上角,b:右下角,row:上下边框字符,col:左右边框字符
void drawFrame(COORD a, COORD b, char row, char col)
{
drawRow(a.Y, a.X+1, b.X-1, row); // 绘制上边框
drawRow(b.Y, a.X+1, b.X-1, row); // 绘制下边框
drawCol(a.X, a.Y+1, b.Y-1, col); // 绘制左边框
drawCol(b.X, a.Y+1, b.Y-1, col); // 绘制右边框
}
// 功能:重载绘制边框(直接传入坐标数值)
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
COORD a={x1, y1};
COORD b={x2, y2};
drawFrame(a, b, row, col);
}
// 功能:重载绘制边框(传入Frame结构体)
void drawFrame(Frame frame, char row, char col)
{
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}
// 功能:绘制游戏主界面UI:游戏区、得分区、操作区等边框+文字
void drawPlaying()
{
drawFrame(0, 0, 48, 24, '=', '|'); // 绘制游戏主地图边框
drawFrame(49, 0, 79, 4, '-', '|'); // 绘制输出信息框
drawFrame(49, 4, 79, 9, '-', '|'); // 绘制得分框
drawFrame(49, 9, 79, 20, '-', '|'); // 绘制操作说明框
drawFrame(49, 20, 79, 24, '-', '|'); // 绘制其他信息框
// 输出右侧固定文字
SetPos(52, 6);
cout<<"得分:";
SetPos(52, 7);
cout<<"称号:";
SetPos(52,10);
cout<<"操作方式:";
SetPos(52,12);
cout<<" a,s,d,w 控制战机移动,k攻击";
SetPos(52,14);
cout<<" p 暂停游戏。";
SetPos(52,16);
cout<<" e 退出游戏。";
}
// 功能:生成[a, b)区间的随机整数
// 参数 a:最小值,b:最大值
int random(int a, int b)
{
int c=(rand() % (a-b))+ a;
return c;
}
// 功能:在矩形a~b内随机生成一个坐标
COORD random(COORD a, COORD b)
{
int x=random(a.X, b.X);
int y=random(a.Y, b.Y);
COORD c={x, y};
return c;
}
// 功能:判断一个坐标点是否在Frame矩形内
// 参数 frame:矩形框,spot:待判断坐标点
bool judgeCoordInFrame(Frame frame, COORD spot)
{
if(spot.X>=frame.position[0].X)
if(spot.X<=frame.position[1].X)
if(spot.Y>=frame.position[0].Y)
if(spot.Y<=frame.position[1].Y)
return true;
return false;
}
// 功能:打印坐标信息
void printCoord(COORD a)
{
cout <<"( "<<a.X<<" , "<<a.Y<<" )";
}
// 功能:打印Frame矩形的两个对角坐标
void printFrameCoord(Frame a)
{
printCoord(a.position[0]);
cout <<" - ";
printCoord(a.position[1]);
}
// 功能:绘制游戏开始菜单,返回难度选择结果
int drawMenu()
{
SetPos(30, 1);
cout<<"P l a n e W a r";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4);
cout<<"w 和 s 选择, k 确定";
SetPos(15, 11);
cout<<"1. 简单的敌人";
SetPos(15, 13);
cout<<"2. 冷酷的敌人";
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有着较慢的移动速度。";
SetPos(24, 21);
cout<<"祝你玩的愉快";
int j=11; // 菜单当前选中行
SetPos(12, j);
cout<<">>"; // 绘制选中箭头
// 菜单按键循环
while(1)
{ if( _kbhit() ) // 检测是否有按键按下
{
char x=_getch(); // 获取按键(无回显)
switch (x)
{
case 'w' : // 上移选择
{
if( j == 13)
{
SetPos(12, j);
cout<<" "; // 清除原箭头
j = 11; // 切换到上一个选项
SetPos(12, j);
cout<<">>"; // 绘制新箭头
// 更新说明文字
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有着较慢的移动速度,容易对付。";
}
break;
}
case 's' : // 下移选择
{
if( j == 11 )
{
SetPos(12, j);
cout<<" ";
j = 13;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"冷酷的敌人:";
SetPos(51, 13);
cout<<"冷酷的敌人移动速度较快,难对付哟!!!";
}
break;
}
case 'k' : // 确认选择
{
if (j == 8) return 1;
else return 2;
}
}
}
}
}
/*================== 游戏核心类 ==================*/
// 游戏类:封装战机、子弹、敌机、得分、逻辑等所有游戏数据
class Game
{
public:
COORD position[10]; // 战机的10个组成坐标(绘制战机形状)
COORD bullet[10]; // 10发子弹坐标(对象池)
Frame enemy[8]; // 8个敌机矩形
int score; // 当前得分
int rank; // 游戏难度/敌机移动速度等级
int rankf; // 上一帧难度值(用于判断称号变化)
string title; // 玩家称号
int flag_rank; // 称号升级标记
Game (); // 构造函数:初始化游戏数据
// 初始化函数
void initPlane(); // 初始化战机位置
void initBullet(); // 初始化子弹
void initEnemy(); // 初始化敌机
// 移动函数
void planeMove(char); // 战机移动
void bulletMove(); // 子弹移动
void enemyMove(); // 敌机移动
// 绘制函数(显示)
void drawPlane(); // 绘制战机
void drawBullet(); // 绘制子弹
void drawEnemy(); // 绘制敌机
// 擦除函数(用空格覆盖)
void drawPlaneToNull();
void drawBulletToNull();
void drawEnemyToNull();
// 单个对象擦除
void drawThisBulletToNull( COORD );
void drawThisEnemyToNull( Frame );
void Pause(); // 游戏暂停
void Playing(); // 游戏主循环
void judgePlane(); // 碰撞检测:战机撞敌机
void judgeEnemy(); // 碰撞检测:子弹打敌机
void Shoot(); // 发射子弹
void GameOver(); // 游戏结束
void printScore(); // 打印得分和称号
};
// 类构造函数:初始化所有游戏数据
Game::Game()
{
initPlane();
initBullet();
initEnemy();
score = 0; // 初始得分0
rank = 25; // 初始难度25
rankf = 0;
flag_rank = 0;
}
// 功能:初始化战机的10个坐标,组成飞机形状
void Game::initPlane()
{
COORD centren={39, 22}; // 战机中心坐标
position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
position[1].X=centren.X-2;
position[2].X=position[6].X=centren.X-1;
position[3].X=position[8].X=centren.X+1;
position[4].X=centren.X+2;
for(int i=0; i<=4; i++)
position[i].Y=centren.Y;
for(int i=6; i<=8; i++)
position[i].Y=centren.Y+1;
position[5].Y=centren.Y-1;
position[9].Y=centren.Y-2;
}
// 功能:绘制战机:O组成机身,|组成机头
void Game::drawPlane()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
if(i!=5)
cout<<"O";
else if(i==5)
cout<<"|";
}
}
// 功能:擦除战机:用空格覆盖原位置
void Game::drawPlaneToNull()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
cout<<" ";
}
}
// 功能:初始化子弹:Y=30表示未激活(闲置状态)
void Game::initBullet()
{
for(int i=0; i<10; i++)
bullet[i].Y = 30;
}
// 功能:绘制激活的子弹(^符号)
void Game::drawBullet()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30) // Y≠30表示子弹已发射
{
SetPos(bullet[i]);
cout<<"^";
}
}
}
// 功能:擦除子弹轨迹
void Game::drawBulletToNull()
{
for(int i=0; i<10; i++)
if( bullet[i].Y != 30 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
SetPos(pos);
cout<<" ";
}
}
// 功能:随机初始化8个敌机的矩形位置
void Game::initEnemy()
{
COORD a={1, 1};
COORD b={45, 15};
for(int i=0; i<8; i++)
{
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
// 功能:绘制所有敌机矩形
void Game::drawEnemy()
{
for(int i=0; i<8; i++)
drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
}
// 功能:擦除所有敌机
void Game::drawEnemyToNull()
{
for(int i=0; i<8; i++)
{
drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
}
}
// 功能:游戏暂停功能,按p继续
void Game::Pause()
{
SetPos(61,2);
cout<<" ";
SetPos(61,2);
cout<<"暂停中...";
char c=_getch();
while(c!='p') // 循环等待p键继续
c=_getch();
SetPos(61,2);
cout<<" ";
}
// 功能:战机移动控制
// 参数 x:按键字符(w上s下a左d右)
void Game::planeMove(char x)
{
if(x == 'a') // 左移
if(position[1].X != 1) // 边界检测
for(int i=0; i<=9; i++)
position[i].X -= 2;
if(x == 's') // 下移
if(position[7].Y != 23)
for(int i=0; i<=9; i++)
position[i].Y += 1;
if(x == 'd') // 右移
if(position[4].X != 47)
for(int i=0; i<=9; i++)
position[i].X += 2;
if(x == 'w') // 上移
if(position[5].Y != 3)
for(int i=0; i<=9; i++)
position[i].Y -= 1;
}
// 功能:所有子弹向上移动,出屏则重置
void Game::bulletMove()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
bullet[i].Y -= 1; // 向上飞
if( bullet[i].Y == 1 ) // 到达顶部
{
COORD pos={bullet[i].X, bullet[i].Y+1};
drawThisBulletToNull( pos );
bullet[i].Y=30; // 重置为闲置状态
}
}
}
}
// 功能:敌机向下移动,出屏则重置到顶部
void Game::enemyMove()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<2; j++)
enemy[i].position[j].Y++;
if(24 == enemy[i].position[1].Y) // 敌机到达底部
{
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b); // 随机重置位置
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
}
// 功能:碰撞检测:战机任意部位撞上敌机 → 游戏结束
void Game::judgePlane()
{
// for(int i = 0; i < 8; i++)
// for(int j=0; j<9; j++)
// if(judgeCoordInFrame(enemy[i], position[j]))
// {
// SetPos(62, 1);
// cout<<"坠毁";
// drawFrame(enemy[i], '+', '+');
// Sleep(1000);
// GameOver();
// break;
// }
}
// 功能:擦除单个子弹
void Game::drawThisBulletToNull( COORD c)
{
SetPos(c);
cout<<" ";
}
// 功能:擦除单个敌机
void Game::drawThisEnemyToNull( Frame f )
{
drawFrame(f, ' ', ' ');
}
// 功能:碰撞检测:子弹击中敌机 → 得分+敌机重置
void Game::judgeEnemy()
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 10; j++)
if( judgeCoordInFrame(enemy[i], bullet[j]) )
{
score += 5; // 击中+5分
drawThisEnemyToNull( enemy[i] );
// 重置敌机位置
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
drawThisBulletToNull( bullet[j] );
bullet[j].Y = 30; // 子弹重置
}
}
// 功能:发射子弹:找到闲置子弹,设置到战机机头位置
void Game::Shoot()
{
for(int i=0; i<10; i++)
if(bullet[i].Y == 30)
{
bullet[i].X = position[5].X;
bullet[i].Y = position[5].Y-1;
break;
}
}
// 功能:实时打印得分、升级称号
void Game::printScore()
{
// 得分达到阈值,提升难度+称号
if(score == 120 && flag_rank == 0)
{
rank -= 3;
flag_rank = 1;
}
else if( score == 360 && flag_rank == 1)
{
rank -= 5;
flag_rank = 2;
}
else if( score == 480 && flag_rank == 2)
{
rank -= 5;
flag_rank = 3;
}
int x=rank/5;
SetPos(60, 6);
cout<<score;
if( rank!=rankf )
{
SetPos(60, 7);
// 根据等级设置称号
if( x == 5)
title="初级飞行员";
else if( x == 4)
title="中级飞行员";
else if( x == 3)
title="高级飞行员";
else if( x == 2 )
title="王牌飞行员";
cout<<title;
}
rankf = rank;
}
// 功能:游戏主循环:处理按键、移动、碰撞、绘制
void Game::Playing()
{
drawEnemy();
drawPlane();
int flag_bullet = 0; // 子弹移动速度控制
int flag_enemy = 0; // 敌机移动速度控制
while(1)
{
Sleep(8); // 控制游戏帧率
if(_kbhit())
{
char x = _getch();
// 方向键控制移动
if ('a' == x || 's' == x || 'd' == x || 'w' == x)
{
drawPlaneToNull();
planeMove(x);
drawPlane();
judgePlane();
}
else if ('p' == x) // 暂停
Pause();
else if( 'k' == x) // 射击
Shoot();
else if( 'e' == x) // 退出
{
GameOver();
break;
}
}
// 子弹移动刷新(间隔执行,控制速度)
if( 0 == flag_bullet )
{
bulletMove();
drawBulletToNull();
drawBullet();
judgeEnemy();
}
flag_bullet++;
if( 5 == flag_bullet )
flag_bullet = 0;
// 敌机移动刷新(根据rank难度控制速度)
if( 0 == flag_enemy )
{
drawEnemyToNull();
enemyMove();
drawEnemy();
judgePlane();
}
flag_enemy++;
if( flag_enemy >= rank )
flag_enemy = 0;
printScore(); // 更新得分面板
}
}
// 功能:游戏结束界面:显示结算信息,选择重新开始/退出
void Game::GameOver()
{
system("cls");
COORD p1={28,9};
COORD p2={53,15};
drawFrame(p1, p2, '=', '|');
SetPos(36,12);
string str="Game Over!";
for(int i=0; i<str.size(); i++) // 逐字显示
{
Sleep(80);
cout<<str[i];
}
Sleep(1000);
system("cls");
drawFrame(p1, p2, '=', '|');
SetPos(31, 11);
cout<<"击落敌机:"<<score/5<<" 架";
SetPos(31, 12);
cout<<"得 分:"<<score;
SetPos(31, 13);
cout<<"获得称号:"<<title;
SetPos(30, 16);
Sleep(1000);
cout<<"继续? 是(y)| 否(n)";
as:
char x=_getch();
if (x == 'n') // 退出游戏
exit(0);
else if (x == 'y') // 重新开始
{
system("cls");
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
else goto as; // 输入错误,重新等待
}
/*================== 主函数 ==================*/
int main()
{
// 游戏初始化
srand((int)time(0)); // 设置随机数种子
HideCursor(); // 隐藏光标
Game game; // 创建游戏对象
int a = drawMenu(); // 显示菜单,获取难度
if(a == 2)
game.rank = 20; // 难度2:敌机速度更快
system("cls");
drawPlaying(); // 绘制游戏界面
game.Playing(); // 启动游戏主循环
}飞机大战
这是 2026年4月17日 存档的第 1 版,不是当前版本。查看当前版本