基本功能

· · 个人记录

#include<iostream>
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string>
#include<iomanip>
#define REP(i, x, y) for(int i = (x);i <= (y);i++)
using namespace std;


int dialogX, dialogY;//记录对话框的左上角位置

struct Player {
    int x, y, preX, preY;
    int anim;
    }plane;//控制台小飞机


void Handle(bool flag) { 
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut, &cci);
    cci.bVisible = flag;
    SetConsoleCursorInfo(hOut, &cci);
    }
//隐藏控制台光标


void SetConsole(int x, int y) 

    {

    COORD position;
    position.X = y;//坐标轴与行列不同
    position.Y = x;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);

    }
//建立函数 移动光标 第一个参数为行

void Clear(int x, int y, int lenth) { 
    SetConsole(x, y);
    REP(i, 1, lenth)
        printf(" ");
    }
//清除第x行y列开始的控制台输出 注:清除完后需要重定向

void Print(string s, int t) { 
    for (int i = 0; i < s.size(); i++) { //使用UNICODE
        if (s[i] < 0) { //汉字
            cout << s[i] << s[i + 1];
            i++;
            }
        else cout << s[i];//字母
        Sleep(t);
        }
    }
//实现间隔一定时间输出一个字

void Loading() {
    for (int i = 1; i <= 5; i++) { 
        printf("/ ");
        Sleep(200); printf("\b\b");
        printf("- ");
        Sleep(200); printf("\b\b");
        printf("\\ ");
        Sleep(200); printf("\b\b");
        printf("| ");
        Sleep(200); printf("\b\b");
        }
    printf("  ");
    }
//转圈圈

void DrawDialogBox(int x, int y, int numLine) { 
    SetConsole(x, y);
    printf(" ————————");
    for (int i = 1; i <= numLine - 2; i++) {
        SetConsole(x + i, y);
        if (i == 1) {
            printf("| ");
            cout << std::left << setw(15) << "";
            printf("\\___________________________________________");
            }
        else if (i == 2) {
            printf("| ");
            cout << std::left << setw(16) << " ";
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY
                                    | FOREGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_RED);
            printf("                                           ");//输出背景色为黄色
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
                                    | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
            }
        else if (i == 8) {
            printf("|                                         ");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY
                                    | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
            printf(" ///G&K MWCS V1.28 ");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
                                    | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
            printf("|");
            }
        else printf("|                                                            |");
        }
    SetConsole(x + numLine - 1, y);
    printf(" ——————————————————————————————");
    }
//从第x行y列开始绘制一个高度为numLine的对话框

void AnimDialogue(int x, int y) {
    dialogX = x, dialogY = y;
    SetConsole(x + 5, y);
    Print(" ——————————————————————————————", 10);
    Clear(x + 5, y, 80);
    SetConsole(x + 4, y);
    printf(" ——————————————————————————————");
    SetConsole(x + 5, y);
    printf("|                                                            |");
    SetConsole(x + 6, y);
    printf(" ——————————————————————————————");


    Sleep(60);
    for(int i = 4;i <= 6;i++)Clear(x + i, y, 80);
    DrawDialogBox(x + 3, y, 5);

    Sleep(60);
    for (int i = 3; i <= 7; i++)Clear(x + i, y, 80);
    DrawDialogBox(x + 2, y, 7);

    Sleep(60);
    for (int i = 2; i <= 8; i++)Clear(x + i, y, 80);
    DrawDialogBox(x + 1, y, 9);

    Sleep(60);
    for (int i = 1; i <= 9; i++)Clear(x + i, y, 80);
    DrawDialogBox(x, y, 10);
    }
//从第x行开始模拟动画并绘制对话窗口

void DrawMap(int x1, int x2, int y, int lenth) {
    SetConsole(x1, y);
    printf(" ");
    REP(i, 1, lenth)
        printf("—");
    REP(i, x1 + 1, x2 - 1) {
        SetConsole(i, y);
        printf("|");
        REP(j, 1, lenth * 2)
            printf(" ");
        printf("|");
        }
    SetConsole(x2, y);
    printf(" ");
    REP(i, 1, lenth)
        printf("—");
    }
//绘制从x1到x2的控制台操作窗口

void AnimMap(int x1, int x2, int y, int lenth) { 
    int mid = (x1 + x2) / 2;
    SetConsole(mid, y);
    Print(" ", 3);
    REP(i, 1, lenth)
        Print("—", 3);
    REP(i, 1, (x2 - x1) / 2) { 
        DrawMap(mid - i, mid + i, y, lenth);
        Sleep(10);
        }
    }
//绘制控制台操控窗口(x2 - x1 + 1是奇数才行)

void BackgroundWhite() {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY
                            | FOREGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_RED
                            | BACKGROUND_BLUE);
    }
void BackgroundBlack() {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
                            | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    }
void ForeGreen() {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
                            | FOREGROUND_GREEN);
    }
void ForeYellow() {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
                            | FOREGROUND_RED | FOREGROUND_GREEN);
    }

void DrawG(int x, int y) {
    SetConsole(x, y); BackgroundWhite();
    printf("           "); SetConsole(x + 1, y);
    printf("  "); SetConsole(x + 2, y);
    printf("  "); SetConsole(x + 3, y);
    printf("  "); BackgroundBlack(); printf("   "); BackgroundWhite(); printf("      "); SetConsole(x + 4, y);
    printf("  "); BackgroundBlack(); printf("       "); BackgroundWhite(); printf("  "); SetConsole(x + 5, y);
    printf("  "); BackgroundBlack(); printf("       "); BackgroundWhite(); printf("  "); SetConsole(x + 6, y);
    printf("           "); 
    BackgroundBlack();
    }
void Drawand(int x, int y) { 
    x++;//比其他两个字母少一行
    y -= 2;//画的时候多了两格出来
    SetConsole(x, y + 5); BackgroundWhite();
    printf("   "); 
    SetConsole(x + 1, y + 4); printf(" ");
    SetConsole(x + 1, y + 8); printf(" ");
    SetConsole(x + 2, y + 5); printf("   ");
    SetConsole(x + 3, y + 3); printf("  "); SetConsole(x + 3, y + 7); printf(" ");
    SetConsole(x + 3, y + 9); printf(" ");
    SetConsole(x + 4, y + 2); printf("  "); SetConsole(x + 4, y + 8); printf(" ");
    SetConsole(x + 5, y + 3); printf("     "); SetConsole(x + 5, y + 9); printf("  ");
    BackgroundBlack();
    }
void DrawK(int x, int y) { 
    BackgroundWhite();
    SetConsole(x, y);printf("  "); SetConsole(x, y + 8); printf("  ");
    SetConsole(x + 1, y); printf("  "); SetConsole(x + 1, y + 6); printf("  ");
    SetConsole(x + 2, y); printf("  "); SetConsole(x + 2, y + 4); printf("  ");
    SetConsole(x + 3, y); printf("    ");
    SetConsole(x + 4, y); printf("  "); SetConsole(x + 4, y + 4); printf("  ");
    SetConsole(x + 5, y); printf("  "); SetConsole(x + 5, y + 6); printf("  ");
    SetConsole(x + 6, y); printf("  "); SetConsole(x + 6, y + 8); printf("   ");
    BackgroundBlack();
    }
void DrawGandK(int x, int y) {
    DrawG(x, y);
    Drawand(x, y + 14);
    DrawK(x, y + 26);
    }
//绘制格里芬

void PrintDialogue(string speaker, string dialogue, int speed) {
    int x = dialogX, y = dialogY;
    SetConsole(x + 1, y + 6);
    printf("         ");
    SetConsole(x + 1, y + 6);
    cout << speaker;
    Clear(x + 5, y + 4, 57);
    SetConsole(x + 5, y + 4);
    Print(dialogue, speed);
    //SetConsole(x + 10, y);
    GetAsyncKeyState(VK_SPACE);//得先把缓存区的东西吃掉
    while (!GetAsyncKeyState(VK_SPACE));
    }
//输出对话,定位跟随对话框

int consoleX, consoleY;
void PrintConsole(int x, string content, int speed, bool stop) { 
    SetConsole(consoleX + x, consoleY);
    cout << "                                 ";
    SetConsole(consoleX + x, consoleY);
    Print(content, speed);
    if (stop) {
        GetAsyncKeyState(VK_SPACE);//得先把缓存区的东西吃掉
        while (!GetAsyncKeyState(VK_SPACE));
        }
    }
//从控制台第x行输出控制台内容(从0开始)


void PrintPlane() { 
    //(x, y)为机头坐标 所以-2
    switch (plane.anim) { //先写绘制
        case 0 :
            SetConsole(plane.x, plane.y - 2);
            printf("  |");
            SetConsole(plane.x + 1, plane.y - 2);
            printf("———");
            SetConsole(plane.x + 2, plane.y - 2);
            printf(" |  |");
            break;
        case 1 :
            SetConsole(plane.x, plane.y - 2);
            printf("  |/");
            SetConsole(plane.x + 1, plane.y - 2);
            printf("  /|");
            SetConsole(plane.x + 2, plane.y - 2);
            printf(" /|");
            break;
        case 2 :
            SetConsole(plane.x, plane.y - 2);
            printf(" \\|");
            SetConsole(plane.x + 1, plane.y - 2);
            printf(" |\\");
            SetConsole(plane.x + 2, plane.y - 2);
            printf("  |\\");
            break;
        }
    }
//每循环擦除上一次动画并绘制本次动画

void PositionJudge() {
    if (plane.x < 1)plane.x = 1;
    if (plane.x > 37)plane.x = 37;
    if (plane.y < 3)plane.y = 3;
    if (plane.y > 61)plane.y = 61;
    }

void MovePlane(int speed) { 
    plane.preX = plane.x, plane.preY = plane.y;
    Clear(plane.preX, plane.preY - 2, 6);
    Clear(plane.preX + 1, plane.preY - 2, 6);
    Clear(plane.preX + 2, plane.preY - 2, 6);
    if (GetAsyncKeyState('W'))plane.x -= speed;
    if (GetAsyncKeyState('S'))plane.x += speed;
    if (GetAsyncKeyState('A'))plane.y -= speed;
    if (GetAsyncKeyState('D'))plane.y += speed;
    PositionJudge();
    if (plane.y == plane.preY)plane.anim = 0;
    else if(plane.y < plane.preY)plane.anim = 1;
    else plane.anim = 2;
    PrintPlane();
    }


void Init() {
    system("mode con cols=130 lines=45");//修改控制台大小防bug
    plane.x = plane.preX = 37;
    plane.y = plane.preY = 33;
    plane.anim = 0;

    consoleX = 21, consoleY = 68;//初始化输出控制台位置(对话框单独函数初始化过位置了
    }

int consolePlaneLeftTime = 10000;
bool ConsolePlane() {
    MovePlane(1);
    Sleep(100);
    consolePlaneLeftTime -= 100;
    if (consolePlaneLeftTime < 0)return 0;
    if (consolePlaneLeftTime < 7000) {
        SetConsole(consoleX + 1, consoleY);
        cout << "通讯记录云同步信号: ";
        ForeYellow(); cout << "一般"; BackgroundBlack();

        }
    if (consolePlaneLeftTime < 6400) {
        SetConsole(consoleX + 1, consoleY);
        cout << "通讯记录云同步信号: ";
        ForeGreen(); cout << "良好"; BackgroundBlack();
        }
    return 1;
    }

void Chapter1() { 
    puts("GRIFFIN & KRYUGER Combat Operation System[版本 10.0.19042.1348]");
    puts("(c) GRIFFIN & KRYUGER Corporation。保留所有权利。\n");
    printf("C:\\Users\\Commander>");
    Sleep(3000);
    Print("-", 1000);
    Print("init", 150);
    Print(" ", 1500);
    Print("-MWCS", 150);
    Print(" ", 600);
    Print("access_416", 100);
    Sleep(2500);
    Handle(FALSE);
    Print("\n通讯模块初始化", 60);
    Sleep(500);
    Loading();
    puts("");
    Print("通讯模块加载完毕", 60);
    Print("...", 500);
    Sleep(1000);
    for (int i = 0; i < 6; i++)Clear(i, 0, 80);
    SetConsole(0, 0);
    Handle(TRUE);
    Sleep(3000);
    Handle(FALSE);
    DrawGandK(1, 79);
    Sleep(1000);
    AnimDialogue(10, 66);
    AnimMap(20, 40, 66, 30);
    SetConsole(20, 66); printf("|");
    BackgroundWhite();
    printf(" ▅  C:\\COS\\System32\\cmd.exe                                 ");
    BackgroundBlack();
    printf("|");
    PrintDialogue("格琳娜", "微波通讯系统(MWCS)启动,在你的系统上按下空格进行通讯。", 60);
    //没写分行 这句差不多是单句最长了
    PrintConsole(1, "通讯记录云同步信号: 测速中", 60, false);
    Sleep(1000);
    PrintConsole(1, "通讯记录云同步信号: ", 0, false);
    ForeGreen(); cout << "良好"; BackgroundBlack();
    Sleep(400);
    PrintDialogue("格琳娜", "进行身份认证,", 60);
    PrintDialogue("格琳娜", "格里芬直属404小队通讯员,格琳娜。", 60);
    PrintDialogue("指挥官", "404小队飞行系统操作员,代号“指挥官”。", 60);
    PrintConsole(2, " >声纹代码认证成功", 60, true);
    SetConsole(consoleX + 1, consoleY);
    cout << "通讯记录云同步信号: ";
    ForeYellow(); cout << "一般"; BackgroundBlack();
    Sleep(600);
    SetConsole(consoleX + 1, consoleY);
    cout << "通讯记录云同步信号: ";
    ForeGreen(); cout << "良好"; BackgroundBlack();
    Sleep(400);
    PrintConsole(2, " >飞行系统接入中", 60, true);
    PrintDialogue("指挥官", "正在接入飞行系统。", 60);
    PrintDialogue("指挥官", "怎么样小格,休息够了么?", 60);
    PrintConsole(2, " >飞行系统连接成功", 60, true);
    PrintDialogue("格琳娜", "接入成功,各模块开始自检。", 60);
    PrintConsole(2, " >模块自检中", 60, false);

    PrintDialogue("格琳娜", "完 全 没 有 休 息 够 !", 100);
    SetConsole(consoleX + 1, consoleY);
    cout << "通讯记录云同步信号: ";
    ForeYellow(); cout << "一般"; BackgroundBlack();
    Sleep(600);
    SetConsole(consoleX + 1, consoleY);
    cout << "通讯记录云同步信号: ";
    ForeGreen(); cout << "良好"; BackgroundBlack();
    Sleep(400);
    PrintDialogue("格琳娜", "天天加班演习演习演习,然后还得熬夜写报告书。", 60);
    PrintDialogue("格琳娜", "老板也不发点加班费意思一下。", 60);
    PrintDialogue("格琳娜", "再这么下去我要离职了啊,天天都掉头发。", 60);
    PrintDialogue("指挥官", "好啦好啦,现在军方和铁血的局势很紧张。", 60);
    PrintDialogue("指挥官", "格里芬又一直和军方有合作,咱任务总会变多的。", 60);
    PrintDialogue("格琳娜", "这我当然知道!", 60);
    PrintDialogue("指挥官", "(苦笑)", 60);
    PrintDialogue("格琳娜", "...希望能和平解决吧。铁血那边也有我很好的朋友。", 60);
    PrintDialogue("指挥官", "......嗯。", 60);
    PrintConsole(2, " >模块自检完成", 60, true);
    PrintConsole(3, " >正在初始化", 60, true);
    PrintDialogue("格琳娜", "各模块自检完毕,开始初始化。现在颁布演习任务,", 60);

    PrintDialogue("格琳娜", "404小队将在演习中对抗敌机以测试格里芬最新原型机的性能,", 60);
    PrintDialogue("格琳娜", "敌机为军方制造的一批仿真铁血机型,", 60);
    PrintDialogue("格琳娜", "以下为敌机清单...", 60);
    PrintConsole(5, "任务目标:歼灭敌机", 60, false);
    PrintConsole(6, "敌方机型:", 30, false);
    PrintConsole(7, "Scarecrow", 30, false);
    PrintConsole(8, "Executioner", 30, false);
    PrintConsole(9, "Hunter", 30, false);
    PrintConsole(10, "Disruptor", 30, false);
    PrintConsole(11, "Destroyer", 30, false);
    PrintConsole(12, "Alchemist", 30, true);

    PrintDialogue("指挥官", "收到。", 60);
    PrintDialogue("格琳娜", "格里芬的新原型机啊...", 60);
    PrintDialogue("格琳娜", "再不加工资的话,咱给它开走拿去卖了。", 60);
    PrintDialogue("指挥官", "哈小格,你知道通讯记录是会实时同步到格里芬云端的吧。", 60);
    PrintDialogue("格琳娜", "我当然知道。", 60);
    PrintDialogue("格琳娜", "嘿嘿老板不给加班费拿人手短,他不会说我什么的。", 60);
    PrintDialogue("格琳娜", "不开玩笑了,我看了下清单,这批铁血机型做得很逼真啊。", 60);
    PrintDialogue("指挥官", "是的。", 60);

    PrintConsole(12, " ", 60, false);
    PrintConsole(11, " ", 60, false);
    PrintConsole(10, " ", 60, false);
    PrintConsole(9, " ", 60, false);
    PrintConsole(8, " ", 60, false);
    PrintConsole(7, " ", 60, false);
    PrintConsole(6, " ", 60, false);
    PrintConsole(5, " ", 60, true);
    PrintConsole(3, " >初始化完成", 60, true);
    PrintConsole(4, " >加载操作模块", 60, true);
    PrintDialogue("格琳娜", "好,初始化完成,正在加载操作模块。", 60);
    PrintConsole(4, " >操作模块加载完成", 60, true);
    PrintDialogue("格琳娜", "加载完成,映射到你的系统上,使用WASD控制原型机。", 60);
    SetConsole(0, 0);
    AnimMap(0, 40, 0, 32);//行0-19 20 21-40

    while (ConsolePlane());
    plane.anim = 0;//最后把飞机姿态摆正
    plane.preX = plane.x, plane.preY = plane.y;
    Clear(plane.preX, plane.preY - 2, 6);
    Clear(plane.preX + 1, plane.preY - 2, 6);
    Clear(plane.preX + 2, plane.preY - 2, 6);
    PrintPlane();

    PrintDialogue("格琳娜", "操作模块工作正常,即将载入视觉模块。", 60);
    PrintConsole(5, " >加载视觉模块", 60, true);
    PrintDialogue("格琳娜", "指挥官,准备好了吗?", 60);
    PrintDialogue("指挥官", "呼,", 120);
    Print("准备好了。", 60);
    Sleep(1000);
    PrintConsole(5, " >视觉模块加载完成", 60, false);
    BackgroundWhite();
    REP(i, 0, 45) {
        SetConsole(i, 0);
        REP(j, 0, 129) {
            cout << " ";
            }
        Sleep(20);
        }
    }

/// <summary>
/// 下面是图形界面部分
/// </summary>

int runTime;//异步计时器

IMAGE img_background;//背景图 850 * 1700
IMAGE img_backbackground;//1280 * 720 底图
IMAGE img_gundam[2];//单图 70 * 85
IMAGE img_rush[2];//同上
IMAGE img_idle[3];

IMAGE img_dialoguebox;//338 * 163
IMAGE img_GriLogo;//368 * 460
IMAGE img_CMD;//337 * 307

IMAGE img_bullet[2];//我方子弹

IMAGE img_white;
IMAGE img_rushEnemy[2];//撞击敌人 60 * 60

IMAGE img_explode[2];


void LoadResource() { 
    loadimage(&img_background, _T("./image/background/Background.jpg"));
    loadimage(&img_backbackground, _T("./image/background/backbackground4.png"));

    loadimage(&img_dialoguebox, _T("./image/UI/dialoguebox.png"));
    loadimage(&img_GriLogo, _T("./image/UI/GriLogo.png"));
    loadimage(&img_CMD, _T("./image/UI/cmd1.png"));

    loadimage(&img_gundam[0], _T("./image/player/Gundamxor2.jpg"));
    loadimage(&img_gundam[1], _T("./image/player/Gundam.png"));

    loadimage(&img_rush[0], _T("./image/player/rushxor.jpg"));
    loadimage(&img_rush[1], _T("./image/player/rush2.png"));

    loadimage(&img_idle[0], _T("./image/player/Idlexor.jpg"));
    loadimage(&img_idle[1], _T("./image/player/Idle.png"));
    loadimage(&img_idle[2], _T("./image/player/Idledark1.png"));

    loadimage(&img_bullet[0], _T("./image/Item/bulletxor.jpg"));
    loadimage(&img_bullet[1], _T("./image/Item/bullet.jpg"));

    loadimage(&img_white, _T("./image/background/white.jpeg"));

    loadimage(&img_bullet[0], _T("./image/Item/bulletxor.jpg"));
    loadimage(&img_bullet[1], _T("./image/Item/bullet.jpg"));

    loadimage(&img_rushEnemy[0], _T("./image/enemy/rushEnemyxor.jpg"));
    loadimage(&img_rushEnemy[1], _T("./image/enemy/rushEnemy.jpg"));

    loadimage(&img_explode[0], _T("./image/Item/expxor.jpg"));
    loadimage(&img_explode[1], _T("./image/Item/exp.jpg"));
    }
//载入图片资源

int lastUpdateBackground, backgroundY = 981;
void PrintBackground(int t) { 
    if (runTime - lastUpdateBackground > t && backgroundY > 0) {
        backgroundY--;
        lastUpdateBackground = runTime;
        }
    putimage(59, 0, 850, 720, &img_background, 0, backgroundY);
    }
//每隔t ms刷新一次背景

int lastSingle = -9999, singleCD = 1;//之前是4, 调试改成1
wchar_t dialogue[100];
int singleX, singleY, pos, lenChar;
bool singleFinish;
void DrawSingle() {
    if (runTime - lastSingle < singleCD || singleFinish)return;
    lastSingle = runTime;
    outtextxy(singleX, singleY, dialogue[pos]);
    if (dialogue[pos] >= 8000) singleX += 17;
    else singleX += 10;
    if (singleX > 1230)singleY += 20, singleX = 925;
    pos++;
    if (pos == lenChar)singleFinish = true;
    }
//输出单字

struct DialogueGroup {
    wchar_t speaker[10];
    wchar_t content[100];
    }dialogues[300];//应该没300句话吧。。
int indexDialogue, numDialogue;




void LoadDialogue(const wchar_t temp_speaker[], const wchar_t temp_dialogue[]) {
    outtextxy(925, 255, temp_speaker);
    singleX = 925, singleY = 295;
    lenChar = wcslen(temp_dialogue);
    REP(i, 0, lenChar - 1) {
        dialogue[i] = temp_dialogue[i];
        }
    pos = 0;
    singleFinish = false;
    }
//载入准备输出下一句对话

bool continueDialogue = 1;
void NextDialogue() {
    if (continueDialogue) {//true的时候可以继续剧情,一句话运行一次
        if (indexDialogue == numDialogue)return;
        putimage(910, 250, &img_dialoguebox);
        LoadDialogue(dialogues[++indexDialogue].speaker, dialogues[indexDialogue].content);
        continueDialogue = false;
        }
    }



struct PlayerG {
    int x, y;
    int anim = 0;
    int rush = 0;
    int speed = 5;
    int opt = -1;//空格操作模式 1对话 0待机 2战斗 -1无法操作
    int hp = 6;
    int lastHitPlayer = -9999, hitPlayerCD = 100;
    int animHit;//受击动画
    }player;

struct Bullet {
    int x, y;
    bool alive;
    int speedY, speedX;
    }bullet[40];

int enemyWidth[5] = { 60, 0 };
int enemyHeight[5] = { 60, 0 };
int enemyHp[5] = { 5, 0 };
int numEnemy;
struct Enemy {
    int x, y;
    int type;//0冲撞
    int hp;
    bool alive;
    int width, height;
    int animHit;
    int speedX, speedY;
    bool out;
    }enemy[40];

int lastRushAnim, animRush = 0;
void DrawPlayer(){

    if (!player.animHit) {
        putimage(player.x + 59, player.y, 70, 85, &img_gundam[0], (player.anim + 2) * 70, 0, NOTSRCERASE);//绘制高达本体
        putimage(player.x + 59, player.y, 70, 85, &img_gundam[1], (player.anim + 2) * 70, 0, SRCINVERT);
        }
    else {
        player.animHit = (player.animHit + 1) % 16;
        int deltaX = 7 * pow(-1, player.animHit);
        putimage(player.x + 59 + deltaX, player.y, 70, 85, &img_gundam[0], (player.anim + 2) * 70, 0, NOTSRCERASE);
        putimage(player.x + 59 + deltaX, player.y, 70, 85, &img_white, (player.anim + 2) * 70, 0, SRCINVERT);
        }
    int t;//喷射闪烁时间
    if (player.rush == -1)t = 10;//惩罚闪烁变慢
    else t = 5;

    if (runTime - lastRushAnim > t) { 
        animRush = (animRush + 70) % 140;
        lastRushAnim = runTime;
        }
    if (player.rush == 1) {//冲刺
        putimage(player.x + 59, player.y, 70, 85, &img_rush[0], animRush, 0, NOTSRCERASE);//绘制火焰动画
        putimage(player.x + 59, player.y, 70, 85, &img_rush[1], animRush, 0, SRCINVERT);
        }
    else if(player.rush == 0){//正常
        putimage(player.x + 1 + 59, player.y - 6, 70, 85, &img_idle[0], animRush, 0, NOTSRCERASE);//绘制火焰动画
        putimage(player.x + 1 + 59, player.y - 6, 70, 85, &img_idle[1], animRush, 0, SRCINVERT);
        }
    else {//惩罚
        putimage(player.x + 1 + 59, player.y - 6, 70, 85, &img_idle[0], animRush, 0, NOTSRCERASE);
        putimage(player.x + 1 + 59, player.y - 6, 70, 85, &img_idle[2], animRush, 0, SRCINVERT);
        }
    }
//绘制高达 t为火焰喷射循环时间

void DrawBullet() { 
    REP(i, 0, 39) {
        if (bullet[i].alive) { 
            putimage(bullet[i].x + 59, bullet[i].y, &img_bullet[0], NOTSRCERASE);
            putimage(bullet[i].x + 59, bullet[i].y, &img_bullet[1], SRCINVERT);
            }
        }
    }

void DrawEnemy() {
    REP(i, 0, 39) {
        if (!enemy[i].alive)continue;
        if (enemy[i].type == 0) {
            if (enemy[i].animHit) {
                enemy[i].animHit = (enemy[i].animHit + 1) % 4;
                putimage(enemy[i].x + 59, enemy[i].y, &img_rushEnemy[0], NOTSRCERASE);
                putimage(enemy[i].x + 59, enemy[i].y, 60, 60, &img_white, 0, 0, SRCINVERT);
                }
            else {
                putimage(enemy[i].x + 59, enemy[i].y, &img_rushEnemy[0], NOTSRCERASE);
                putimage(enemy[i].x + 59, enemy[i].y, &img_rushEnemy[1], SRCINVERT);
                }
            }
        }
    }

struct Exp {
    int x, y;//爆炸中心
    int indexAnim;//播放帧数共六帧
    }explode[40];

void DrawExplode() {
    REP(i, 0, 39) {
        if (explode[i].indexAnim) {
            int expX = explode[i].x - 50, expY = explode[i].y - 50;
            int anim = (explode[i].indexAnim + 2) / 3;//放慢三倍播放
            expX = max(50, expX);
            expX = min(800, expX);//处理边界
            putimage(expX + 59, expY, 100, 100, &img_explode[0], (anim - 1) * 100, 0, NOTSRCERASE);
            putimage(expX + 59, expY, 100, 100, &img_explode[1], (anim - 1) * 100, 0, SRCINVERT);
            explode[i].indexAnim = (explode[i].indexAnim + 1) % 19;
            }
        }
    }

void CreatExplode(int x, int y) {
    REP(i, 0, 39) {
        if (!explode[i].indexAnim) {
            explode[i].indexAnim = 1;
            explode[i].x = x, explode[i].y = y;
            break;
            }
        }
    }

void GundamPositionJudge() { 
    if (player.x < 0)player.x = 0;
    if (player.y < 0)player.y = 0;
    if (player.x + 70 > 850)player.x = 850 - 70;
    if (player.y + 85 > 720)player.y = 720 - 85;
    }


int rushCD = 999999999, rushLastTime = 500, rushPunishTime = 250;
int lastRushTime = -9999, lastRushPunishTime = -9999;
int punishReady, punishing;
void StartRush() {
    if (runTime - lastRushTime < rushCD) {
        //printf("CD----%d%%\n", (runTime - lastRushTime) * 100 / rushCD);
        return;//还在CD
        }
    lastRushTime = runTime;//开冲!
    player.speed = 7;
    player.rush = 1;
    punishReady = 1;
    }

bool Collider(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {//对敌机j的碰撞检测
    return max(x1, x3) <= min(x2, x4) && max(y1, y3) <= min(y2, y4);
    }
//矩形碰撞检测

void ChangeEnemyHp(int i, int x) {
    enemy[i].hp -= x;
    enemy[i].animHit = 1;
    if (enemy[i].hp <= 0) {
        enemy[i].alive = false, numEnemy--;
        CreatExplode(enemy[i].x + enemy[i].width / 2, enemy[i].y + enemy[i].height / 2);
        }
    }
//编号为i的敌机减少x点生命值

void Check() {
    GundamPositionJudge();//检查位置合法性
    if (runTime - lastRushTime > rushLastTime && punishReady) { //检查结束冲刺状态
        player.speed = 3;
        player.rush = -1;
        lastRushPunishTime = runTime;
        punishing = 1;
        punishReady = 0;
        }
    if (punishing) { 
        //printf("punish----%d%%\n", (runTime - lastRushPunishTime) * 100 / rushPunishTime);
        }
    if (runTime - lastRushPunishTime > rushPunishTime && punishing) {//检查结束惩罚
        punishing = 0;
        player.speed = 5;
        player.rush = 0;
        }
    REP(i, 0, 39) { //子弹相关
        if (bullet[i].alive) { 
            bullet[i].x += bullet[i].speedX;
            bullet[i].y += bullet[i].speedY;
            if (bullet[i].y < -30 || bullet[i].x < -4 || bullet[i].x > 831)bullet[i].alive = false;
            REP(j, 0, 39) {
                if (!enemy[j].alive)continue;
                int hitX = bullet[i].x + 11, hitY = bullet[i].y + 8;
                if (Collider(hitX, hitY, hitX, hitY, enemy[j].x, enemy[j].y, enemy[j].x + enemy[j].width, enemy[j].y + enemy[j].height)) {
                    //这里拿hit点代替了hit矩形
                    ChangeEnemyHp(j, 1);
                    bullet[i].alive = false;
                    }
                }
            }
        }

    REP(i, 0, 39) {//敌机相关
        if (enemy[i].alive) {
            enemy[i].x += enemy[i].speedX;//更新位置
            enemy[i].y += enemy[i].speedY;
            if (runTime > player.lastHitPlayer + player.hitPlayerCD) {
                if (Collider(player.x, player.y + 20, player.x + 70, player.y + 75, enemy[i].x, enemy[i].y, enemy[i].x + enemy[i].width, enemy[i].y + enemy[i].height)) {
                    player.lastHitPlayer = runTime;
                    player.hp --;
                    if(player.rush)ChangeEnemyHp(i, 2);
                    else ChangeEnemyHp(i, 1);
                    player.animHit = 1;
                    }
                }

            if (enemy[i].out) {//可以出下边界,出边界死亡
                if (enemy[i].y > 720) { enemy[i].alive = false; numEnemy--; }
                }
            if (enemy[i].x <= 0)enemy[i].speedX *= -1;//先这么写左右转向
            if (enemy[i].x + enemy[i].width >= 850)enemy[i].speedX *= -1;
            }
        }
    }
//检查位置合法、检查状态更新等

void CreatBullet(int x, int y, bool isplayer, int speedX) {
    static int lastCreatBulletTime = -999, creatBulletCD = 5;//太快重复bug
    REP(i, 0, 39) { 
        if (!bullet[i].alive && runTime - lastCreatBulletTime > creatBulletCD) {
            lastCreatBulletTime = runTime;
            bullet[i].x = x + 21, bullet[i].y = y;
            bullet[i].speedX = speedX;
            if(isplayer)
                bullet[i].speedY = -7;
            bullet[i].alive = true;
            break;
            }
        }
    }
//生成子弹

void CreatEnemy(int type, int x, int y, int speedX, int speedY, bool out) {
    REP(i, 0, 39) {
        if (enemy[i].alive)continue;
        numEnemy++;
        enemy[i].alive = 1;
        enemy[i].type = type;
        enemy[i].width = enemyWidth[type];
        enemy[i].height = enemyHeight[type];
        enemy[i].x = x, enemy[i].y = y;
        enemy[i].hp = enemyHp[type];
        enemy[i].animHit = false;
        enemy[i].speedX = speedX, enemy[i].speedY = speedY;
        enemy[i].out = out;
        enemy[i].animHit = 0;
        break;
        }
    }

int animValue, bulletDegree, degreeValue;
void MoveGundam(int speed) { 
    if (player.opt == -1)return;
    bool flag1 = false, flag2 = false;
    if (GetAsyncKeyState('W'))player.y -= speed;
    if (GetAsyncKeyState('S'))player.y += speed;
    if (GetAsyncKeyState('A')) {
        player.x -= speed;
        animValue--;
        flag1 ^= 1;
        }
    if (GetAsyncKeyState('J')) {
        degreeValue--;
        }
    if (GetAsyncKeyState('D')) {
        player.x += speed;
        animValue++;
        flag1 ^= 1;
        }
    if (GetAsyncKeyState('K')) {
        degreeValue++;
        }
    if (GetAsyncKeyState('Q')) {
        StartRush();
        }
    if (GetAsyncKeyState(VK_SPACE)) {
        if (player.opt == 1 && singleFinish) {
            continueDialogue = true;
            }
        else if (player.opt == 2) {//战斗模式 
            CreatBullet(player.x, player.y, true, bulletDegree);
            }
        }
    if (!flag1) {//AD没按或者全按,复位
        if (animValue > 0)animValue--;
        else if (animValue < 0)animValue++;
        }
    if (animValue <= -5)animValue = -5, player.anim = -2;
    else if (animValue < 0)player.anim = -1;
    else if (animValue == 0)player.anim = 0;
    else if (animValue >= 5)animValue = 5, player.anim = 2;
    else player.anim = 1;

    if (degreeValue == -3)bulletDegree--, degreeValue = 0;
    if (degreeValue ==  3)bulletDegree++, degreeValue = 0;
    if (bulletDegree < -5)bulletDegree = -5;
    if (bulletDegree >  5)bulletDegree = 5;

    }
//高达移动函数


int tongxun = 1, caozuo = 1, zhandou = 1;
//UI界面的一些参数

void WriteDialogue() {
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  视觉模块运作正常,正在加载运动模块。") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  格里芬新型原型机“机兵”具有超强的瞬时机动能力,映射到你的系统上按下Q以进入机动模式,可以大幅增加移动速度。需要注意的是,机动模式结束后机兵会局部过载,移动速度受损一段时间。") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  运动模块加载完毕,正在上线。") };//3
    dialogues[++numDialogue] = { _T(""), _T("   —— 按下Q键启动机动模式 ——") };//4
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  机动模式运作正常。") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  其冷却时间和过载时间可以在右下角的控制台实时监控,") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  请合理利用“机兵”的机动功能。") };
    dialogues[++numDialogue] = { _T("指挥官"), _T("  收到。") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  正在加载武器模块。") };//9
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  加载成功,机兵由通讯模式切换至战斗模式,指挥官在飞行系统上按下空格发射子弹,J/K调整发射角度。") };//9
    dialogues[++numDialogue] = { _T(""), _T("    —— 按下空格发射子弹 ——") };//11
    dialogues[++numDialogue] = { _T(""), _T("   —— 按下J/K调整发射角度 ——") };//12
    dialogues[++numDialogue] = { _T("指挥官"), _T("  武器系统运作正常。") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  敌人的每一次攻击对机兵都是致命的。为此,机兵搭载了16Lab开发的自动闪避系统,在闪避时还可以对敌机造成伤害。") };//14
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  指挥官可以在右下角查看自动闪避系统的耐久度。除此之外,击落一些特殊机型的敌机可以回复自动闪避系统的耐久值。") };//15
    dialogues[++numDialogue] = { _T("指挥官"), _T("  收到。准备好大干一场了吗小格?") };//16
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  打完收工早点下班!让军方看看我们的厉害吧。") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  第四季度第23次演习任务,代号“紧急除颤”,现在开始。") };
    dialogues[++numDialogue] = { _T("格琳娜"), _T("  敌机来袭!") };//19


    indexDialogue = 0;
    }
//写入剧情

int stage;
void ContinueStage() {
    switch (stage) {
        case 0:
            if (player.y > 720 - 85)player.y--;
            else if (player.y == 720 - 85) {
                player.opt = 1;
                stage+= 10;
                }
            break;
        case 10:
            if (indexDialogue < 4)return;//到第三句前不触发
            if (indexDialogue == 4) { 
                player.opt = 0;//对话禁止
                bulletDegree = 0;
                rushCD = 2000;//CD 999999 -> 2000 就是开启这个功能
                if (player.rush) {
                    player.opt = 1;
                    continueDialogue = true;
                    stage+= 10;
                    }
                }
            break;
        case 20:
            if (indexDialogue < 11)return;
            if (indexDialogue == 11) {
                player.opt = 2;//战斗模式
                static int stageTime0 = runTime;
                REP(i, 0, 39) {
                    if (bullet[i].alive && runTime - stageTime0 > 250) {//发射了子弹,玩够250
                        //player.opt = 1;
                        continueDialogue = true;
                        stage+= 10;
                        break;
                        }
                    }
                }
            break;
        case 30:
            static int stageTime1 = runTime;
            if (runTime - stageTime1 > 250) {
                player.opt = 1;
                continueDialogue = true;
                stage+= 5;
                }
            break;
        case 35:
            if (indexDialogue < 14)return;
            if (indexDialogue == 14) {
                player.opt = -1;
                static bool flagCreat1 = false;
                if (!flagCreat1) {
                    CreatEnemy(0, player.x + 3, -20, 0, 1, true);//造一个出来装一下闪避系统
                    enemy[0].hp = 9999999;
                    flagCreat1 = true;
                    }
                if (numEnemy == 0) {
                    continueDialogue = true;
                    player.opt = 1;//对话模式
                    stage += 1;
                    }
                }
            break;
        case 36:
            if (indexDialogue == 16) {
                player.hp = 6;//回复下教学掉的血
                stage += 4;
                }
            break;
        case 40:
            if (indexDialogue < 19)return;
            if (indexDialogue == 19) {
                player.opt = 2;
                REP(i, 0, 9) {
                    CreatEnemy(0, i * 85, 0, 0, 4, 1);
                    }
                stage+= 10;
                }
            break;
        case 50:
            if (numEnemy == 0)stage+=10;
            break;
        case 60:
            stage = 40;
            break;
        }
    }
//剧情演出和推进都在这了


void InitGraph() {
    initgraph(1280, 720, EW_SHOWCONSOLE);
    LoadResource();
    putimage(0, 0, &img_backbackground);
    WriteDialogue();//加载对话
    player.x = 400, player.y = 720;
    }
//图形界面(第二章以后)的初始化

void ChangeHpAnim(int realHp) {
    static int nowShow = 100;
    if (realHp > nowShow)nowShow++;
    else if (realHp < nowShow)nowShow--;
    TCHAR s[10];
    _stprintf_s(s, _T("%d%%"), nowShow);
    outtextxy(1070, 592, s);
    }

void PrintConsoleText() {
    outtextxy(920, 452, _T("通讯记录云同步信号:"));//第一行
    if (tongxun == 1) setcolor(GREEN),outtextxy(1080, 452, _T("良好")); 
    else if (tongxun == 0) setcolor(YELLOW), outtextxy(1080, 452, _T("一般"));
    else setcolor(RED), outtextxy(1080, 452, _T("极差"));
    setcolor(WHITE);

    if(player.opt == 0 || player.opt == -1)outtextxy(1165, 452, _T("<待机模式>"));
    else if (player.opt == 1)outtextxy(1165, 452, _T("<通讯模式>"));
    else if (player.opt == 2)outtextxy(1165, 452, _T("<战斗模式>"));

    outtextxy(920, 472, _T("  >操作模块:"));//第二行
    if (caozuo == 1)setcolor(GREEN), outtextxy(1020, 472, _T("ONLINE"));
    else setcolor(RED), outtextxy(1020, 472, _T("OFFLINE"));
    setcolor(WHITE);

    outtextxy(920, 492, _T("  >视觉模块:"));//第三行不变了
    setcolor(GREEN), outtextxy(1020, 492, _T("ONLINE"));setcolor(WHITE);

    if (indexDialogue >= 1) {//运动模块
        if (indexDialogue < 3) {
            outtextxy(920, 512, _T("  >运动模块:"));//第四行
            setcolor(RGB(255, 215, 0));
            outtextxy(1020, 512, _T("LOADING"));
            static int lastPointTime = -999, numPoint = -1;
            if (runTime - lastPointTime > 30) {
                lastPointTime = runTime;
                numPoint = (numPoint + 1) % 4;
                }
            if (numPoint == 0)outtextxy(1080, 512, _T(""));
            else if (numPoint == 1)outtextxy(1080, 512, _T("."));
            else if (numPoint == 2)outtextxy(1080, 512, _T(".."));
            else if (numPoint == 3)outtextxy(1080, 512, _T("..."));
            setcolor(WHITE);
            }
        else {
            outtextxy(920, 512, _T("  >运动模块:"));//第四行
            if (rushCD == 2000)setcolor(GREEN), outtextxy(1020, 512, _T("ONLINE"));
            else setcolor(RED), outtextxy(1020, 512, _T("OFFLINE"));
            setcolor(WHITE);
            outtextxy(920, 548, _T("     I"));
            outtextxy(943, 532, _T("---- 机动模式:"));
            outtextxy(920, 542, _T("     I"));
            outtextxy(920, 532, _T("     I"));
            outtextxy(943, 552, _T("---- 局部过载:"));

            if (rushCD != 2000) outtextxy(1040, 532, _T("N/A")), outtextxy(1040, 552, _T("N/A"));
            else {
                TCHAR s[10];
                int x = (runTime - lastRushTime) * 100 / rushCD;
                _stprintf_s(s, _T("%d%%"), x);
                if (x >= 100)setcolor(RGB(0, 191, 255)), outtextxy(1040, 532, _T("READY")), setcolor(WHITE);
                else if (x >= 25)outtextxy(1040, 532, s);
                else setcolor(RGB(0, 191, 255)), outtextxy(1040, 532, s), setcolor(WHITE);

                x = (runTime - lastRushPunishTime) * 100 / rushPunishTime;
                _stprintf_s(s, _T("%d%%"), x);
                if (x >= 100)outtextxy(1040, 552, _T("N/A"));
                else setcolor(RGB(255, 215, 0)), outtextxy(1040, 552, s), setcolor(WHITE);
                }
            }
        }

    if (indexDialogue >= 9) { 
        outtextxy(920, 572, _T("  >战斗模块:"));
        if (indexDialogue == 9) {
            setcolor(RGB(255, 215, 0));
            outtextxy(1020, 572, _T("LOADING"));
            static int lastPointTime1 = -999, numPoint = -1;
            if (runTime - lastPointTime1 > 30) {
                lastPointTime1 = runTime;
                numPoint = (numPoint + 1) % 4;
                }
            if (numPoint == 0)outtextxy(1080, 572, _T(""));
            else if (numPoint == 1)outtextxy(1080, 572, _T("."));
            else if (numPoint == 2)outtextxy(1080, 572, _T(".."));
            else if (numPoint == 3)outtextxy(1080, 572, _T("..."));
            setcolor(WHITE);
            }
        else {
            if (zhandou == 1)setcolor(GREEN), outtextxy(1020, 572, _T("ONLINE"));
            else setcolor(RED), outtextxy(1020, 572, _T("OFFLINE"));
            setcolor(WHITE);
            }
        setcolor(WHITE);
        outtextxy(920, 588, _T("     I"));
        outtextxy(943, 592, _T("---- 闪避系统耐久:"));
        int x = player.hp;
        //_stprintf_s(s, _T("%d%%"), x * 100 / 6);//这里等下写个渐变
        if (x >= 5)setcolor(GREEN);
        else if (x >= 3)setcolor(RGB(255, 215, 0));
        else setcolor(RED);
        ChangeHpAnim(x * 100 / 6);
        setcolor(WHITE);
        }
    }



void DrawUI() {
    putimage(971, -5, 368, 250, &img_GriLogo, 0, 0);
    putimage(910, 432, &img_CMD);
    NextDialogue();//是否载入下一句对话
    DrawSingle();//检测是否有新对话,进行绘制
    PrintConsoleText();
    }
//绘制右侧UI

void GameDraw() {
    PrintBackground(30);
    DrawUI();
    MoveGundam(player.speed);
    Check();
    ContinueStage();
    DrawEnemy();
    DrawPlayer();
    DrawBullet();
    DrawExplode();
    }
//总绘制函数

void Chapter2() { 
    InitGraph();
    BeginBatchDraw();//双缓冲绘图
    while (1) { 
        GameDraw();
        FlushBatchDraw();
        runTime++;
        Sleep(1);
        }
    EndBatchDraw();
    }


int main() { 
    //Init();
    //_getch();

    //Chapter1();
    Chapter2();

    //Handle(FALSE);
    //test1();


    getchar();
    return 0;
    }