c 飞机大战

#pragma once
#include<iostream>
#include<Windows.h>//定义控制台应用程序的入口点
 
using namespace std;
 
//界面颜色 
void setcolor(char str[])
  {
      
          
     if(str=="lightblue")
         SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|1);
     if(str=="lightred")
         SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
     if(str=="lightyellow")
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
     if(str=="lightpink"    )
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
 
        
    if(str=="blue")
         SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
     if(str=="red")
         SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
     if(str=="yellow")
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),  FOREGROUND_RED | FOREGROUND_BLUE);
     if(str=="pink")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),  FOREGROUND_GREEN | FOREGROUND_BLUE);
    if(str=="lightgray")
          SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),  FOREGROUND_GREEN |8);    
    if(str=="gray")
          SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),  8);                                        
        
     else return;
          
      
  }
 
//定义敌人结构 其中最后面Frame代表结构体类型 若不加typedef代表定义的结构体变量
typedef struct Frame
{
       COORD position[2];
       //   COORD 是Windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标。
       // 其定义为:
       // typedef struct _COORD {
       //             SHORT X;  
       //             SHORT Y;
       //          } COORD;
       int flag;
}Frame;
 
class Game
{
public:
       COORD position[10];
       COORD bullet[10];//子弹坐标
       Frame enemy[8];//敌人数量
 
       int score;
       int rank;//级别,难度
       int rankf;//等级标志
       string title;
 
       int flag_rank;//等级标志
       //构造函数
       Game();
       //初始化所有   //设定位置
       void initPlane();
       void initBullet();
       void initEnemy();
       //填充所有   --画出形状和消失的形状
       void drawPlane();
       void drawPlaneToNull();
       void drawBullet();
       void drawBulletToNull();
       void drawEnemy();
       void drawEnemyToNull();
       //执行某一个操作
       void Playing();//游戏主循环
       void planeMove(char x);//飞机移动
       void judgePlane();//判断飞机是否与障碍物重叠
       void GameOver();//游戏失败
       void Pause();// 该成员函数用来使得游戏暂停
       void Shoot();//发射子弹
       void bulletMove();//子弹移动
       void drawThisBulletToNull(COORD c);//画出失效子弹
       void judgeEnemy();//判断子弹是否击中障碍物
       void drawThisEnemyToNull(Frame f);       //击败的障碍物清空
       void enemyMove();//障碍物移动
       void printScore();//输出分数
};
//主菜单
int drawMenu();
//隐藏光标
void HideCursor();
 
 
void SetPos(int i, int j);//设置光标
COORD random(COORD a, COORD b);//产生随机障碍物位置
void drawFrame(COORD a, COORD   b, char row, char col);//画出障碍物
//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch);
//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch);
// 绘制游戏界面
void drawPlaying();
void drawFrame(Frame frame, char row, char col);//画坠毁后的战机
// 该函数用来判断战机的某一部分是否与障碍物有接触
bool   judgeCoordInFrame(Frame frame, COORD spot);
 
void drawRow(COORD a, COORD b, char ch);
#include<Windows.h>
#include<conio.h>
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
Game::Game()
{
    // 调用类成员函数来进行初始化
    initPlane();
 
    initBullet();
 
    initEnemy();
 
    this->score = 0;
    rank = 25;
    rankf = 25;
    flag_rank = 0;
}
void Game::initPlane()
{
    COORD centren;
    centren.X = 39;
    centren.Y = 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;
}
void Game::drawPlane()
{
    for (int i = 0; i < 9; i++)
    {
        SetPos(position[i].X,position[i].Y);
        if (i != 5)
        {
            setcolor("yellow");
            cout << "*";
        }
        else if (i == 5)
        {
            setcolor("yellow");
            cout << "|";
        }
    }
}
// 这个成员函数通过将战机的每个坐标处输出" "来代替"0"和"|",
// 来达到将战机消除的目的。
void Game::drawPlaneToNull()
{
    for (int i = 0; i < 9; i++)
    {
        SetPos(position[i].X, position[i].Y);
        cout << " ";
    }
}
// 该成员函数用来初始化子弹,
// 即将每个子弹的Y坐标初始化为30(bullet[i].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)
        {
            SetPos(bullet[i].X,bullet[i].Y);
            setcolor("blue");
            cout << "^";
        }
    }
}
//子弹失效
void Game::drawBulletToNull()
{
    for (int i = 0; i < 10; i++)
        if (bullet[i].Y != 30)
        {
            SetPos(bullet[i].X, bullet[i].Y + 1);
            cout << " ";
        }
}
COORD random(COORD a, COORD b)
{
    int x = rand() % (a.X - b.X) + a.X;
    int y = rand() % (a.Y - b.Y) + a.Y;
    COORD c = { x,y };
    return c;
}
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);
        //  random(a, b)是调用了一个重载的函数,它表示在坐标a、b之间的矩形框
        //  内随机生成一个坐标值,并将该坐标值作为障碍物的左上角的坐标。
        // enemy[i].position[0]中是一个Frame结构体类型的变量,存放了障碍物i的左上角的坐标。
        enemy[i].position[1].X = enemy[i].position[0].X + 3;
        enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
        // enemy[i].position[1]也中是一个Frame结构体类型的变量,存放了障碍物i的右下角的坐标。
    }
}
void Game::drawEnemy()
{
    for (int i = 0; i < 8; i++)
    {
        setcolor("gray");
        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], ' ', ' ');
    }
}
 
//隐藏光标
void HideCursor()
{
    CONSOLE_CURSOR_INFO cursor_info = { 1,0 };//第二个值0表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void SetPos(int i, int j)//设置坐标点位(光标)
{
    HANDLE hout;
    COORD coord;
    coord.X = i;
    coord.Y = j;
    hout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout, coord);
}
 
//左上角坐标、右下角坐标、用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);
}
 
//把第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;
    }
}
//把第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++;
    }
}
 
//主菜单绘制
int drawMenu()
{
    setcolor("lightgreen");
    system("Title        飞 机 大 战");
    SetPos(30,1);
    cout << "飞 机 大 战";
    drawRow(3, 0, 79, '-');
    drawRow(5, 0, 79, '-');
    SetPos(28, 4);
    setcolor("red");
    cout << "↑和↓选择,回车确定";
    int j = 11;
    SetPos(12, j);
    cout << ">>";
    SetPos(15, 11);
    cout << "1. 简单的任务";
    SetPos(15, 13);
    cout << "2. 困难的任务";
    drawRow(20, 0, 79, '-');
    SetPos(47, 11);
    setcolor("yellow");
    cout << "简单的任务:";
    SetPos(51, 13);
    cout << "简单任务的自动飞行速度较慢,任务难度较小。                  ";
    SetPos(30, 21);
    setcolor("lightblue");
    cout << "纳米核心";
    setcolor("red");
    drawRow(22, 0, 79, '-');
 
    while (true)
    {
        if (_kbhit())
        {
            char x = _getch();
            switch (x)
            {
            case 72:
            {
                if (j == 13)
                {
                    SetPos(12, j);
                    cout << "  ";
                    j = 11;
                    SetPos(12, j);
                    setcolor("red");
                    cout << ">>";
                    SetPos(51, 13);
                    cout << "            ";
                    SetPos(47, 11);
                    setcolor("yellow");
                    cout << "简单的任务:";
                    SetPos(51, 13);
                    cout << "简单任务的自动飞行速度较慢,任务难度较小。                  ";
                }
                break;
            }
            case 80:
            {
                if (j == 11)
 
                {
                    SetPos(12, j);
                    cout << " ";
                    j = 13;
                    SetPos(12, j);
                    setcolor("red");
                    cout << ">>";
                    SetPos(51, 13);
                    cout << "                               ";
                    SetPos(47, 11);
                    setcolor("yellow");
                    cout << "困难的任务:";
                    SetPos(51, 13);
                    cout << "困难任务自动飞行速度较快,难操作哟!                         ";
                }
 
                break;
            }
            case 13://回车键:13 
            {
                
                if (j == 11)//源代码为8?
                    return 1;
                else
                    return 2;
            }
            }
        }
    }
 
    return 0;
}
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);
}
// 绘制游戏界面
void drawPlaying()
{
    setcolor("red");
    drawFrame(0,  0, 48, 24, '=', '|');//    draw map frame主界面
    drawFrame(49, 0, 82, 4, '-', '|');//    draw output frame 状态界面
    drawFrame(49, 4, 82, 9, '-', '|');//    draw score frame 分数界面
    drawFrame(49, 9, 82, 20, '-', '|');//    draw operate frame 操作界面
    drawFrame(49,20, 82, 24, '-', '|');//    draw other message frame 提示界面
    setcolor("yellow") ;
    SetPos(61,2);
    cout << "飞机大战" ; 
    SetPos(52, 6);
    cout << "得分:";
    SetPos(52, 7);
    cout << "称号:";
    SetPos(52, 11);
    setcolor("yellow") ;
    cout << "操作方式:";
    SetPos(52, 13);
    cout << "↑,↓,←,→可以控制战机移动。";
    SetPos(52, 15);
    cout << "P和E分别可以暂停游戏或退出游戏。";
    SetPos(52, 17);
    cout << "空格可以发射燃烧弹";
    SetPos(52, 19);
    cout << "你的任务是躲避障碍物并击毁它们!";
    SetPos(52, 22);
    setcolor("lightblue") ;
    cout << " 游戏虽好玩,不要贪多哦 ";

void Game::planeMove(char x)
{
    if (x == 75)
    {
        if (position[1].X != 1)
        {
            for (int i = 0; i <= 9; i++)
            {
                position[i].X -= 2;
            }
        }
    }
    if (x == 80)
    {
        if (position[7].Y != 23)
        {
            for (int i = 0; i <= 9; i++)
            {
                position[i].Y += 1;
            }
        }
    }        
    if (x == 77 && (position[4].X != 47))
    {
        for (int i = 0; i <= 9; i++)
        {
            position[i].X += 2;
        }    
    }        
    if (x == 72&&(position[5].Y != 3))
    {
        for (int i = 0; i <= 9; i++)
        {
            position[i].Y -= 1;
        }
    }
}
bool  judgeCoordInFrame(Frame frame, COORD spot)
{
    if ((spot.X >= frame.position[0].X) && (spot.X <= frame.position[1].X) && (spot.Y >= frame.position[0].Y) && (spot.Y <= frame.position[1].Y))
    {
        return true;
    }
    return false;
}
void drawFrame(Frame frame, char row, char col)
{
    COORD a = frame.position[0];
    COORD b = frame.position[1];
    drawFrame(a, b, row, col);
}
//游戏结束
void Game::GameOver()
{
    setcolor("lightgreen");
    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);
    setcolor("blue");
    cout << "击毁障碍物:" << score / 5 << "个";
    SetPos(31, 12);
    cout << "得  分:" << score;
    SetPos(31, 13);
    cout << "获得称号:" << title;
    SetPos(30, 18);
    Sleep(1000);
    setcolor("green");
    cout << "继续? 是(Y)| 否(N)";
as://goto 语句标签 直接跳转至此
    char x = _getch();
    if (x == 'n' || x == 'N')
    {
        exit(0);
    }
    else if (x == 'y' || x =='Y')
    {
        system("cls");
        Game game;
        int a = drawMenu();       // 绘制游戏开始界面主菜单
        if (a == 2)
            game.rank = 20;
        system("cls");
        drawPlaying();           // 绘制游戏界面框架
        game.Playing();
    }
    else 
        goto as;
}
void Game::judgePlane()
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 9; j++)
            if (judgeCoordInFrame(enemy[i], position[j]))
            {
                SetPos(61, 2);
                cout << "  坠毁      ";
                setcolor("yellow");
                drawFrame(enemy[i], '+', '+');
                Sleep(1000);
                GameOver();
                break;
            }
    }
}
// 该成员函数用来使得游戏暂停
void Game::Pause()
{
    SetPos(61, 2);
    cout << "         ";
    SetPos(61, 2);
    cout << "暂停中...   ";
    char c = _getch();
    while (c != 'p')
    {
        c = _getch();
    }
    SetPos(61, 2);
    cout << "         "; 
    setcolor("green");
    SetPos(61, 2);
    cout << "飞机大战"; 
    
}
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::drawThisBulletToNull(COORD c)
{
    SetPos(c.X,c.Y);
    cout << " ";
}
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::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;
                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::enemyMove()
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 2; j++)
            enemy[i].position[j].Y++; 
        if (enemy[i].position[1].Y==24)
        {
            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::printScore()
{
    if (score <= 100)
    {
        flag_rank = 1;
    }
    else if (score > 100 && score <= 320)
    {
        flag_rank = 2;
    }
    else if (score > 320 && score <= 440)
    {
        flag_rank = 3;
    }
    else if (score > 440)
    {
        flag_rank = 4;
    }
    SetPos(60, 6);
    cout << score;
    SetPos(60, 7);
    if (flag_rank == 1)
    {
        title = "初级飞行员";
    }
    else if (flag_rank == 2)
    {
        title = "中级飞行员";
    }
    else if (flag_rank == 3)
    {
        title = "高级飞行员";
    }
    else if (flag_rank == 4)
    {
        title = "王牌飞行员";
    }
    cout << title;
}
void Game::Playing()
{
    drawEnemy();
    drawPlane();
 
    int flag_bullet = 0;
    int flag_enemy = 0;
 
    while (true)
    {
        Sleep(20);
        if (_kbhit())
        {
            char x = _getch();
            if (75 == x || 80 == x || 77 == x || 72 == x)
            {
                drawPlaneToNull();     // 将战机先擦除
                planeMove(x);          // 根据所输入的操作符,对战机的坐标进行更改
                drawPlane();           // 访问类中的数据成员——战机的坐标,在新的坐标处重新绘制战机
 
                judgePlane();          // 判断战机是否有坠毁
            }
            else if ('p' == x)
            {
                Pause();
            }
            else if (32 == x)
            {
                Shoot();
            }
            else if ('e' == x)
            {
                //CloseHandle(MFUN)
                GameOver();
                break;
            }
        }
        // 接下来处理子弹
        // 判断子弹状态的程序一直在运行
        if (flag_bullet == 0)
        {
            bulletMove();           // 更新界面上有效子弹的坐标
            drawBulletToNull();     // 将处于旧坐标的子弹擦除
            drawBullet();           // 绘制出新坐标上的子弹
            judgeEnemy();          // 判断障碍物是否被子弹击中
        }
        flag_bullet++;
        if (flag_bullet==1)
        {
            flag_bullet = 0;
        }
        //  接下来处理障碍物
        if (flag_enemy==0)
        {
            drawEnemyToNull();     // 将所有的障碍物都擦除
            enemyMove();           //  更新障碍物的坐标
            drawEnemy();           // 绘制出处于新坐标上的障碍物
            judgePlane();          // 判断障碍物是否与战机接触
        }
        flag_enemy++;
        if (flag_enemy >= rank)
        {
            flag_enemy = 0;
        }
        /* 输出得分 */
 
        printScore();
    }
}
 
int main()
{
    cout<<"\t\t****飞机大战游戏正在准备****\n";
    cout<<"\n已加载 0%";//7
    for(int i=0;i<9;i++){
        SetPos((i+1)*2-1,1); cout<<"■ ";
        SetPos(7-1,3-1);cout<<i+1;
        Sleep(200);
    }
    Sleep(200);
    SetPos(19,1);cout<<"■ \n已加载100%";
    Sleep(200);
    system("cls");
    srand((unsigned int)time(NULL));//随机时间种子
    HideCursor();//隐藏光标
    Game game;
    HideCursor();
    int a=drawMenu();
    if (a == 2)
    {
        game.rank = 20;
    }
    system("cls");
    HideCursor();
    drawPlaying();
    HideCursor();
    game.Playing();
 
}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值