跳到正文

休闲·娱乐

【C++/EasyX】飞机大战小游戏源码分享与讲解

MVP_7625约 102 分钟
原文

这是 2026年4月30日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年4月29日
收录
2026年4月30日
更新
2026年11月9日
篇幅
约 102 分钟
获赞
1

版本

  1. v2当前
  2. v1

前置说明:

请先下载:EasyX2023大暑版

编辑器:DEV c++ 6.3 及以上(最好 9.2.0 最新版)

如何安装:参见 HydrogenOxygen 的文章https://www.luogu.com.cn/article/uus4k89p

感谢夸克AI☺️

正文

最近用 C++ 和 EasyX 库重写了一个经典的飞机大战小游戏,增加了许多有趣的功能和视觉效果,感觉非常有趣,于是决定把它分享出来,希望能给同样热爱编程和游戏的朋友带来一些启发和乐趣!

主要包含:

  • 丰富的视觉效果:实现了粒子系统,包括爆炸效果、飞机喷气轨迹、敌人受伤时的流血效果以及炸弹释放时的冲击波效果,让战斗画面更加炫酷。

  • 多样化的技能系统:玩家可以使用多种技能来应对不同的局面:

操作讲解:

  • 炸弹 (F键):释放一个大范围的冲击波,对屏幕内所有敌人造成伤害。

  • 激光 (Z键):发射一道贯穿屏幕的激光,持续对敌人造成伤害。

  • 狂暴模式 (X键):短时间内大幅提升攻击力,并进入狂暴状态。

  • 追踪导弹 (H键):一次性发射 5 枚自动追踪敌人的导弹。

  • 散射子弹 (G键):发射扇形扩散的子弹,覆盖更大范围。

  • 动态难度与成长系统:随着得分增加,游戏等级提升,敌人变得更强大,同时玩家的基础攻击力也会增长。击杀敌人会获得技能能量,用于释放技能。

  • 连击系统:在短时间内连续击杀敌人可以获得连击加成,分数增长更快,视觉上也有提示。

  • 道具掉落:击败敌人有一定概率掉落道具,可以提升攻击力、恢复生命值或增加炸弹能量。

  • Boss 战:当达到一定等级后,会触发 Boss 战,Boss 会发射激光攻击玩家,更具挑战性。

  • 城市背景:底部添加了简单的城市天际线背景,增加游戏场景的真实感。

核心代码

以下是游戏的完整源代码。为了方便阅读,我已经移除了大部分注释。

核心代码
C++
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;

#define W 480
#define H 600
#define MAX_BULLETS 100
#define MAX_ENEMIES 50
#define MAX_POWERUPS 10
#define MAX_EXPLOSIONS 20
#define MAX_PARTICLES 300
#define FRAME_RATE 60
#define MS_PER_FRAME (1000 / FRAME_RATE)
#define LASER_DURATION_FRAMES 10
#define BOMB_SHOCKWAVE_MAX_RADIUS 800
#define BOMB_SHOCKWAVE_LIFETIME 20
#define SCREEN_SHAKE_DURATION 10
#define COMBO_TIMEOUT_FRAMES 120
#define BOSS_LEVEL 10
#define ACHIEVEMENT_COUNT 3
#define BOSS_LASER_DURATION_FRAMES 30
#define BOSS_ATTACK_INTERVAL_FRAMES 600
#define ENEMY_SHOOT_INTERVAL_FRAMES 180
#define HOMING_MISSILE_SPEED 5
#define SPREAD_SHOT_ANGLE_INCREMENT 15
#define NUM_SPREAD_SHOTS 7
#define CITY_BUILDING_WIDTH 20
#define CITY_BUILDING_HEIGHT_MIN 20
#define CITY_BUILDING_HEIGHT_MAX 60

enum { START, PLAY, OVER, PAUSE } gs = START;
enum { POWERUP_ATTACK, POWERUP_HP, POWERUP_BOMB };
enum { SKILL_NORMAL, SKILL_BOMB, SKILL_LASER, SKILL_RAGE, SKILL_HOMING_MISSILE, SKILL_SPREAD_SHOT };
enum { PARTICLE_EXPLOSION, PARTICLE_JET_TRAIL, PARTICLE_BLEED, PARTICLE_BOMB_WAVE };
enum { ENEMY_NORMAL, ENEMY_BOSS };
enum { BULLET_NORMAL, BULLET_HOMING, BULLET_SPREAD };

struct Bullet {
    int x, y;
    bool live;
    int shooterType;
    int direction;
    int type;
    int targetEnemyIndex;
    double angle;
};

struct Enemy {
    int x, y;
    bool live;
    int hp;
    int initialHp;
    int type;
    int width, height;
    int laserTimer;
    bool isFiringLaser;
    int targetX;
    int shootTimer;
};

struct Plane {
    int x, y;
    int hp;
    int maxHp;
};

struct PowerUp {
    int x, y;
    bool live;
    int type;
};

struct Explosion {
    int x, y;
    bool active;
    int frame;
    int maxFrame;
};

struct Particle {
    int x, y;
    double vx, vy;
    int life;
    int color;
    bool active;
    int type;
    double size;
};

struct SkillSystem {
    double bombEnergy;
    double laserEnergy;
    double rageEnergy;
    double homingMissileEnergy;
    double spreadShotEnergy;
    bool rageActive;
    int rageTimer;
    double originalDamage;
};

struct Achievement {
    const char* name;
    const char* desc;
    bool unlocked;
    function<bool()> checkCondition;
};

Plane p;
Bullet b[MAX_BULLETS];
Enemy e[MAX_ENEMIES];
PowerUp pu[MAX_POWERUPS];
Explosion pxe[MAX_EXPLOSIONS];
Particle particles[MAX_PARTICLES];
SkillSystem skills;

int bc = 0, ec = 0, sc = 0, lv = 1;
DWORD gameOverTime = 0;
double shanghai = 2.0;
vector<int> enemiesHitByBomb;

bool laserActive = false;
int laserTimer = 0;

bool bombShockwaveActive = false;
int shockwaveCenterX, shockwaveCenterY;
int shockwaveRadius = 0;
int shockwaveAge = 0;

int screenShakeOffsetX = 0;
int screenShakeOffsetY = 0;
int screenShakeTimer = 0;

int combo = 0;
int comboTimer = 0;
int lastKillTime = 0;

bool bossSpawnedThisLevel = false;
int bossAttackPattern = 0;
int bossAttackTimer = 0;
char bossWarningText[100] = "";
int bossWarningTimer = 0;

vector<Achievement> achievements = {
    {
        "First Blood",
        "Kill your first enemy",
        false,
        []() { return sc >= 5; }
    },
    {
        "Combo Master",
        "Achieve a 10x combo",
        false,
        []() { return combo >= 10; }
    },
    {
        "Boss Slayer",
        "Defeat a boss",
        false,
        []() { return sc >= 1000; }
    }
};

int highScore = 0;

vector<int> cityBuildingHeights;

void InitGame() {
    p.x = W / 2 - 10;
    p.y = H - 80;
    p.hp = 10;
    p.maxHp = 10;

    for (int i = 0; i < MAX_BULLETS; i++) {
        b[i].live = false;
        b[i].shooterType = 0;
        b[i].direction = 0;
        b[i].type = BULLET_NORMAL;
        b[i].targetEnemyIndex = -1;
        b[i].angle = 0.0;
    }
    for (int i = 0; i < MAX_ENEMIES; i++) {
        e[i].live = false;
        e[i].type = ENEMY_NORMAL;
        e[i].width = 30;
        e[i].height = 30;
        e[i].laserTimer = 0;
        e[i].isFiringLaser = false;
        e[i].targetX = 0;
        e[i].shootTimer = 0;
    }
    for (int i = 0; i < MAX_POWERUPS; i++) pu[i].live = false;
    for (int i = 0; i < MAX_EXPLOSIONS; i++) pxe[i].active = false;
    for (int i = 0; i < MAX_PARTICLES; i++) {
        particles[i].active = false;
        particles[i].type = PARTICLE_EXPLOSION;
        particles[i].size = 2.0;
    }

    skills.bombEnergy = 0.0;
    skills.laserEnergy = 0.0;
    skills.rageEnergy = 0.0;
    skills.homingMissileEnergy = 0.0;
    skills.spreadShotEnergy = 0.0;
    skills.rageActive = false;
    skills.rageTimer = 0;
    skills.originalDamage = shanghai;

    laserActive = false;
    laserTimer = 0;

    bombShockwaveActive = false;
    shockwaveRadius = 0;
    shockwaveAge = 0;

    screenShakeTimer = 0;
    screenShakeOffsetX = 0;
    screenShakeOffsetY = 0;

    combo = 0;
    comboTimer = 0;
    lastKillTime = 0;

    bossSpawnedThisLevel = false;
    bossAttackPattern = 0;
    bossAttackTimer = 0;
    strcpy(bossWarningText, "");
    bossWarningTimer = 0;

    bc = ec = sc = lv = 1;
    srand((unsigned int)time(NULL));

    cityBuildingHeights.clear();
    int numBuildings = W / CITY_BUILDING_WIDTH;
    for (int i = 0; i < numBuildings; i++) {
        cityBuildingHeights.push_back(rand() % (CITY_BUILDING_HEIGHT_MAX - CITY_BUILDING_HEIGHT_MIN + 1) + CITY_BUILDING_HEIGHT_MIN);
    }
}

void DrawStart() {
    cleardevice();
    setfillcolor(BLACK);
    bar(0, 0, W, H);

    setcolor(YELLOW);
    settextstyle(30, 0, "Arial");
    outtextxy(W / 2 - 100, H / 3, "Aircraft Battle");

    setcolor(WHITE);
    settextstyle(16, 0, "Consolas");
    outtextxy(W / 2 - 60, H / 2, "Press SPACE to start");
    outtextxy(W / 2 - 60, H / 2 + 30, "Arrow keys: move");
    outtextxy(W / 2 - 60, H / 2 + 60, "SPACE: shoot");
    outtextxy(W / 2 - 60, H / 2 + 90, "F: use BOMB");
    outtextxy(W / 2 - 60, H / 2 + 120, "Z: use LASER");
    outtextxy(W / 2 - 60, H / 2 + 150, "X: use RAGE (10s +5 damage)");
    outtextxy(W / 2 - 60, H / 2 + 180, "H: use HOMING MISSILE (5 missiles)");
    outtextxy(W / 2 - 60, H / 2 + 210, "G: use SPREAD SHOT (7 bullets)");
    outtextxy(W / 2 - 60, H / 2 + 240, "P: pause");
    outtextxy(W / 2 - 60, H / 2 + 270, "ESC: quit");

    char hsStr[50];
    sprintf(hsStr, "High Score: %d", highScore);
    outtextxy(W / 2 - 60, H / 2 + 300, hsStr);
}

void DrawCity() {
    int offset_y = screenShakeOffsetY;

    setfillcolor(RGB(30, 30, 50));
    bar(0, H + offset_y, W, H + 20 + offset_y);

    setfillcolor(RGB(40, 40, 60));
    for (int i = 0; i < cityBuildingHeights.size(); i++) {
        int buildingX = i * CITY_BUILDING_WIDTH;
        int buildingHeight = cityBuildingHeights[i];
        bar(buildingX, H - buildingHeight + offset_y, buildingX + CITY_BUILDING_WIDTH, H + offset_y);
    }
}

void DrawPlayer() {
    int offset_x = screenShakeOffsetX;
    int offset_y = screenShakeOffsetY;

    int drawX = p.x + offset_x;
    int drawY = p.y + offset_y;

    POINT playerBody[] = {
        {drawX + 10, drawY},
        {drawX, drawY + 30},
        {drawX + 20, drawY + 30}
    };
    setfillcolor(RGB(30, 144, 255));
    fillpolygon(playerBody, 3);

    POINT core[] = {
        {drawX + 10, drawY + 5},
        {drawX + 5, drawY + 25},
        {drawX + 15, drawY + 25}
    };
    setfillcolor(WHITE);
    fillpolygon(core, 3);

    POINT leftWing[] = {
        {drawX - 10, drawY + 15},
        {drawX, drawY + 8},
        {drawX + 5, drawY + 15},
        {drawX, drawY + 23}
    };
    setfillcolor(RGB(100, 149, 237));
    fillpolygon(leftWing, 4);

    POINT rightWing[] = {
        {drawX + 20 + 10, drawY + 15},
        {drawX + 20, drawY + 8},
        {drawX + 20 - 5, drawY + 15},
        {drawX + 20, drawY + 23}
    };
    fillpolygon(rightWing, 4);

    setfillcolor(RED);
    bar(drawX + 3, drawY - 5, drawX + 6, drawY);
    bar(drawX + 14, drawY - 5, drawX + 17, drawY);
    bar(drawX + 8, drawY - 8, drawX + 12, drawY - 2);

    setfillcolor(YELLOW);
    fillcircle(drawX + 10, drawY + 10, 3);

    if (p.hp > 0) {
        setfillcolor(GREEN);
        int w = (int)((20.0 * p.hp) / 10);
        bar(drawX, drawY + 32, drawX + w, drawY + 36);
        setcolor(WHITE);
        rectangle(drawX, drawY + 32, drawX + 20, drawY + 36);
    }
}

void DrawBullets() {
    int offset_x = screenShakeOffsetX;
    int offset_y = screenShakeOffsetY;

    for (int i = 0; i < MAX_BULLETS; i++) {
        if (b[i].live) {
            if (b[i].shooterType == 0) {
                if (b[i].type == BULLET_NORMAL) {
                    setfillcolor(BLUE);
                } else if (b[i].type == BULLET_HOMING) {
                    setfillcolor(RGB(0, 255, 255));
                } else if (b[i].type == BULLET_SPREAD) {
                    setfillcolor(RGB(255, 255, 0));
                }
            } else if (b[i].shooterType == 1) {
                setfillcolor(RED);
            } else {
                setfillcolor(YELLOW);
            }

            if (b[i].type == BULLET_HOMING) {
                POINT missile[] = {
                    {b[i].x + offset_x, b[i].y - 3 + offset_y},
                    {b[i].x - 3 + offset_x, b[i].y + offset_y},
                    {b[i].x + offset_x, b[i].y + 3 + offset_y},
                    {b[i].x + 3 + offset_x, b[i].y + offset_y}
                };
                fillpolygon(missile, 4);
            } else {
                bar(b[i].x + offset_x, b[i].y + offset_y, b[i].x + 4 + offset_x, b[i].y + 10 + offset_y);
            }
        }
    }
}

void DrawEnemies() {
    int offset_x = screenShakeOffsetX;
    int offset_y = screenShakeOffsetY;

    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (e[i].live) {
            int drawX = e[i].x + offset_x;
            int drawY = e[i].y + offset_y;

            if (e[i].type == ENEMY_BOSS) {
                POINT bossBody[] = {
                    {drawX + e[i].width / 2, drawY + e[i].height},
                    {drawX, drawY},
                    {drawX + e[i].width, drawY}
                };
                setfillcolor(RGB(40, 40, 40));
                fillpolygon(bossBody, 3);

                POINT core[] = {
                    {drawX + e[i].width / 2, drawY + e[i].height - 5},
                    {drawX + e[i].width / 4, drawY + 5},
                    {drawX + 3 * e[i].width / 4, drawY + 5}
                };
                setfillcolor(RGB(139, 0, 0));
                fillpolygon(core, 3);

                POINT leftWing[] = {
                    {drawX - 15, drawY + 3 * e[i].height / 4},
                    {drawX, drawY + e[i].height / 2},
                    {drawX - 10, drawY + e[i].height / 4},
                    {drawX - 20, drawY + e[i].height / 2}
                };
                setfillcolor(RGB(60, 60, 60));
                fillpolygon(leftWing, 4);

                POINT rightWing[] = {
                    {drawX + e[i].width + 15, drawY + 3 * e[i].height / 4},
                    {drawX + e[i].width, drawY + e[i].height / 2},
                    {drawX + e[i].width + 10, drawY + e[i].height / 4},
                    {drawX + e[i].width + 20, drawY + e[i].height / 2}
                };
                fillpolygon(rightWing, 4);

                setfillcolor(RGB(0, 191, 255));
                bar(drawX + e[i].width / 2 - 2, drawY + 5, drawX + e[i].width / 2 + 2, drawY + e[i].height - 5);

                setfillcolor(RED);
                bar(drawX + 5, drawY + e[i].height, drawX + 15, drawY + e[i].height + 10);
                bar(drawX + e[i].width - 15, drawY + e[i].height, drawX + e[i].width - 5, drawY + e[i].height + 10);
                bar(drawX + e[i].width / 2 - 5, drawY + e[i].height, drawX + e[i].width / 2 + 5, drawY + e[i].height + 10);

                setfillcolor(BLACK);
                bar(drawX + e[i].width / 2 - 3, drawY - 5, drawX + e[i].width / 2 + 3, drawY);

                if (e[i].isFiringLaser) {
                    setcolor(RGB(0, 191, 255));
                    setlinestyle(PS_SOLID, 5);
                    line(drawX + 10, drawY + e[i].height + 10, drawX + 10, H);
                    line(drawX + e[i].width / 2, drawY + e[i].height + 10, drawX + e[i].width / 2, H);
                    line(drawX + e[i].width - 10, drawY + e[i].height + 10, drawX + e[i].width - 10, H);
                    setlinestyle(PS_SOLID, 1);
                }

            } else {
                POINT enemyBody[] = {
                    {drawX + 15, drawY + 30},
                    {drawX, drawY},
                    {drawX + 30, drawY}
                };
                setfillcolor(RGB(255, 105, 180));
                fillpolygon(enemyBody, 3);

                POINT leftWing[] = {
                    {drawX - 5, drawY + 20},
                    {drawX, drawY + 10},
                    {drawX + 5, drawY + 20},
                    {drawX, drawY + 25}
                };
                setfillcolor(RGB(255, 20, 147));
                fillpolygon(leftWing, 4);

                POINT rightWing[] = {
                    {drawX + 25, drawY + 20},
                    {drawX + 30, drawY + 10},
                    {drawX + 35, drawY + 20},
                    {drawX + 30, drawY + 25}
                };
                fillpolygon(rightWing, 4);

                setfillcolor(RGB(220, 20, 60));
                bar(drawX + 13, drawY - 5, drawX + 17, drawY);

                setfillcolor(RED);
                bar(drawX + 14, drawY + 30, drawX + 16, drawY + 35);
            }

            if (e[i].hp > 0) {
                if (e[i].type == ENEMY_BOSS) {
                    setfillcolor(RGB(0, 191, 255));
                } else {
                    setfillcolor(RED);
                }

                int w = (int)((static_cast<double>(e[i].width) * e[i].hp) / e[i].initialHp);
                bar(drawX, drawY - 6, drawX + w, drawY - 2);
                setcolor(WHITE);
                rectangle(drawX, drawY - 6, drawX + e[i].width, drawY - 2);
            }
        }
    }
}

void DrawPowerUps() {
    int offset_x = screenShakeOffsetX;
    int offset_y = screenShakeOffsetY;

    for (int i = 0; i < MAX_POWERUPS; i++) {
        if (pu[i].live) {
            switch (pu[i].type) {
                case POWERUP_ATTACK:
                    setfillcolor(RED);
                    solidcircle(pu[i].x + offset_x, pu[i].y + offset_y, 6);
                    setcolor(YELLOW);
                    settextstyle(10, 0, "Arial");
                    outtextxy(pu[i].x - 3 + offset_x, pu[i].y - 5 + offset_y, "A");
                    break;
                case POWERUP_HP:
                    setfillcolor(GREEN);
                    solidcircle(pu[i].x + offset_x, pu[i].y + offset_y, 6);
                    setcolor(WHITE);
                    settextstyle(10, 0, "Arial");
                    outtextxy(pu[i].x - 2 + offset_x, pu[i].y - 5 + offset_y, "+");
                    break;
                case POWERUP_BOMB:
                    setfillcolor(YELLOW);
                    solidcircle(pu[i].x + offset_x, pu[i].y + offset_y, 6);
                    setcolor(WHITE);
                    settextstyle(10, 0, "Arial");
                    outtextxy(pu[i].x - 3 + offset_x, pu[i].y - 5 + offset_y, "B");
                    break;
            }
        }
    }
}

void DrawExplosions() {
    int offset_x = screenShakeOffsetX;
    int offset_y = screenShakeOffsetY;

    for (int i = 0; i < MAX_PARTICLES; i++) {
        if (particles[i].active) {
            int drawX = (int)particles[i].x + offset_x;
            int drawY = (int)particles[i].y + offset_y;

            if (particles[i].type == PARTICLE_EXPLOSION) {
                setfillcolor(particles[i].color);
                solidcircle(drawX, drawY, (int)particles[i].size);
            } else if (particles[i].type == PARTICLE_JET_TRAIL) {
                int gray = 255 - (int)((particles[i].life * 255.0) / 20.0);
                if (gray < 0) gray = 0;
                if (gray > 255) gray = 255;
                setfillcolor(RGB(gray, gray, gray));
                solidcircle(drawX, drawY, (int)particles[i].size);
            } else if (particles[i].type == PARTICLE_BLEED) {
                setfillcolor(RED);
                solidcircle(drawX, drawY, (int)particles[i].size);
            } else if (particles[i].type == PARTICLE_BOMB_WAVE) {
                setfillcolor(RGB(255, 255, 200));
                solidcircle(drawX, drawY, (int)particles[i].size);
            }
        }
    }

    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (pxe[i].active) {
            int drawX = pxe[i].x + offset_x;
            int drawY = pxe[i].y + offset_y;
            setcolor(RGB(255, 100, 0));
            circle(drawX, drawY, pxe[i].frame);
        }
    }
}

void CreateExplosion(int x, int y) {
    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (!pxe[i].active) {
            pxe[i].x = x;
            pxe[i].y = y;
            pxe[i].active = true;
            pxe[i].frame = 0;
            pxe[i].maxFrame = 10;
            break;
        }
    }

    for (int j = 0; j < 20; j++) {
        int idx = rand() % MAX_PARTICLES;
        if (!particles[idx].active) {
            particles[idx].x = x;
            particles[idx].y = y;
            particles[idx].vx = (rand() % 11) - 5;
            particles[idx].vy = (rand() % 11) - 5;
            particles[idx].life = 20 + rand() % 20;
            int hue = rand() % 40;
            particles[idx].color = RGB(255, 150 - (hue * 3), 0);
            particles[idx].active = true;
            particles[idx].type = PARTICLE_EXPLOSION;
            particles[idx].size = 2.0;
        }
    }
}

const double M_PI = 3.14159265358979323846;
void CreateBombWaveParticles(int centerX, int centerY, int numParticles) {
    for (int j = 0; j < numParticles; j++) {
        int idx = rand() % MAX_PARTICLES;
        if (!particles[idx].active) {
            double angle = (rand() % 360) * M_PI / 180.0;
            double speed = 3.0 + (rand() % 5);
            particles[idx].x = centerX;
            particles[idx].y = centerY;
            particles[idx].vx = cos(angle) * speed;
            particles[idx].vy = sin(angle) * speed;
            particles[idx].life = 10 + rand() % 10;
            particles[idx].color = RGB(255, 255, 200);
            particles[idx].active = true;
            particles[idx].type = PARTICLE_BOMB_WAVE;
            particles[idx].size = 1.0 + (rand() % 3);
        }
    }
}

void CreateJetTrail(int x, int y, int direction) {
    int idx = rand() % MAX_PARTICLES;
    if (!particles[idx].active) {
        particles[idx].x = x + rand() % 6 - 3;
        particles[idx].y = y + (direction ? 5 : -5);
        particles[idx].vx = (rand() % 3) - 1;
        particles[idx].vy = (direction ? 1 : -1) * (rand() % 2 + 1);
        particles[idx].life = 15 + rand() % 10;
        particles[idx].color = RGB(200, 200, 200);
        particles[idx].active = true;
        particles[idx].type = PARTICLE_JET_TRAIL;
        particles[idx].size = 1.5;
    }
}

void CreateBleedingParticles(int x, int y, int count) {
    for (int j = 0; j < count; j++) {
        int idx = rand() % MAX_PARTICLES;
        if (!particles[idx].active) {
            particles[idx].x = x + rand() % 20 - 10;
            particles[idx].y = y + rand() % 20 - 10;
            particles[idx].vx = (rand() % 5) - 2;
            particles[idx].vy = (rand() % 5) - 2;
            particles[idx].life = 10 + rand() % 10;
            particles[idx].color = RGB(200, 0, 0);
            particles[idx].active = true;
            particles[idx].type = PARTICLE_BLEED;
            particles[idx].size = 1.0 + (rand() % 2);
        }
    }
}

void UpdateExplosions() {
    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (pxe[i].active) {
            pxe[i].frame++;
            if (pxe[i].frame >= pxe[i].maxFrame) {
                pxe[i].active = false;
            }
        }
    }

    for (int i = 0; i < MAX_PARTICLES; i++) {
        if (particles[i].active) {
            particles[i].x += particles[i].vx;
            particles[i].y += particles[i].vy;
            particles[i].life--;
            if (particles[i].life <= 0) {
                particles[i].active = false;
            }
        }
    }
}

void DrawGameOver() {
    setfillcolor(BLACK);
    bar(0, 0, W, H);

    setcolor(RED);
    settextstyle(40, 0, "Arial");
    outtextxy(W / 2 - 80, H / 3, "GAME OVER");

    setcolor(YELLOW);
    settextstyle(24, 0, "Arial");
    char s[50];
    sprintf(s, "END SCORE: %d", sc);
    outtextxy(W / 2 - 60, H / 2, s);

    if (sc > highScore) {
        highScore = sc;
    }
    sprintf(s, "HIGH SCORE: %d", highScore);
    outtextxy(W / 2 - 60, H / 2 + 30, s);

    setcolor(WHITE);
    settextstyle(16, 0, "Consolas");
    outtextxy(W / 2 - 70, H / 2 + 70, "Returning to menu...");

    outtextxy(W / 2 - 70, H / 2 + 110, "Achievements Unlocked:");
    int achCount = 0;
    for (const auto& ach : achievements) {
        if (ach.unlocked) {
             outtextxy(W / 2 - 70, H / 2 + 130 + achCount * 20, ach.name);
             achCount++;
             if (achCount >= 3) break;
        }
    }
}

void GameDraw() {
    BeginBatchDraw();
    cleardevice();
    setfillcolor(BLACK);
    bar(0, 0, W, H);

    DrawCity();
    DrawEnemies();
    DrawBullets();
    DrawPowerUps();
    DrawExplosions();
    DrawPlayer();

    if (laserActive) {
        setcolor(RGB(0, 255, 255));
        setlinestyle(PS_SOLID, 3);
        for (int i = 0; i < MAX_ENEMIES; i++) {
            if (e[i].live) {
                int px = p.x + 10 + screenShakeOffsetX;
                int py = p.y - 5 + screenShakeOffsetY;
                int ex = e[i].x + e[i].width / 2 + screenShakeOffsetX;
                int ey = e[i].y + e[i].height / 2 + screenShakeOffsetY;
                line(px, py, ex, ey);
            }
        }
        setlinestyle(PS_SOLID, 1);
    }

    setcolor(WHITE);
    settextstyle(16, 0, "Consolas");
    char s[30];
    sprintf(s, "score : %d", sc);
    outtextxy(10 + screenShakeOffsetX, 10 + screenShakeOffsetY, s);
    sprintf(s, "level : %d", lv);
    outtextxy(10 + screenShakeOffsetX, 30 + screenShakeOffsetY, s);

    sprintf(s, "attack: %.1f", shanghai);
    outtextxy(10 + screenShakeOffsetX, 50 + screenShakeOffsetY, s);

    sprintf(s, "HP: %d/%d", p.hp, p.maxHp);
    outtextxy(10 + screenShakeOffsetX, 70 + screenShakeOffsetY, s);

    if (combo > 1) {
        setcolor(YELLOW);
        settextstyle(20, 0, "Arial");
        sprintf(s, "%dx COMBO!", combo);
        int flashIntensity = (GetTickCount() / 100) % 2;
        if (flashIntensity) {
            outtextxy(W / 2 - textwidth(s) / 2 + screenShakeOffsetX, 10 + screenShakeOffsetY, s);
        }
        settextstyle(16, 0, "Consolas");
        setcolor(WHITE);
    }

    if (bossWarningTimer > 0) {
        setcolor(RED);
        settextstyle(24, 0, "Arial");
        int flashIntensity = (GetTickCount() / 100) % 2;
        if (flashIntensity) {
            outtextxy(W / 2 - textwidth(bossWarningText) / 2 + screenShakeOffsetX, H / 4 + screenShakeOffsetY, bossWarningText);
        }
        settextstyle(16, 0, "Consolas");
        setcolor(WHITE);
    }

    setcolor(WHITE);
    outtextxy(10 + screenShakeOffsetX, 90 + screenShakeOffsetY, "Skills:");

    setfillcolor(BLUE);
    int bombWidth = (int)((100.0 * skills.bombEnergy) / 100.0);
    bar(10 + screenShakeOffsetX, 110 + screenShakeOffsetY, 10 + bombWidth + screenShakeOffsetX, 120 + screenShakeOffsetY);
    setcolor(WHITE);
    rectangle(10 + screenShakeOffsetX, 110 + screenShakeOffsetY, 110 + screenShakeOffsetX, 120 + screenShakeOffsetY);
    outtextxy(115 + screenShakeOffsetX, 110 + screenShakeOffsetY, "Bomb(F)");

    setfillcolor(GREEN);
    int laserWidth = (int)((100.0 * skills.laserEnergy) / 100.0);
    bar(10 + screenShakeOffsetX, 130 + screenShakeOffsetY, 10 + laserWidth + screenShakeOffsetX, 140 + screenShakeOffsetY);
    setcolor(WHITE);
    rectangle(10 + screenShakeOffsetX, 130 + screenShakeOffsetY, 110 + screenShakeOffsetX, 140 + screenShakeOffsetY);
    outtextxy(115 + screenShakeOffsetX, 130 + screenShakeOffsetY, "Laser(Z)");

    setfillcolor(RED);
    int rageWidth = (int)((100.0 * skills.rageEnergy) / 100.0);
    bar(10 + screenShakeOffsetX, 150 + screenShakeOffsetY, 10 + rageWidth + screenShakeOffsetX, 160 + screenShakeOffsetY);
    setcolor(WHITE);
    rectangle(10 + screenShakeOffsetX, 150 + screenShakeOffsetY, 110 + screenShakeOffsetX, 160 + screenShakeOffsetY);
    outtextxy(115 + screenShakeOffsetX, 150 + screenShakeOffsetY, "Rage(X)");

    setfillcolor(RGB(0, 255, 255));
    int homingWidth = (int)((100.0 * skills.homingMissileEnergy) / 100.0);
    bar(10 + screenShakeOffsetX, 170 + screenShakeOffsetY, 10 + homingWidth + screenShakeOffsetX, 180 + screenShakeOffsetY);
    setcolor(WHITE);
    rectangle(10 + screenShakeOffsetX, 170 + screenShakeOffsetY, 110 + screenShakeOffsetX, 180 + screenShakeOffsetY);
    outtextxy(115 + screenShakeOffsetX, 170 + screenShakeOffsetY, "Homing(H)");

    setfillcolor(RGB(255, 255, 0));
    int spreadWidth = (int)((100.0 * skills.spreadShotEnergy) / 100.0);
    bar(10 + screenShakeOffsetX, 190 + screenShakeOffsetY, 10 + spreadWidth + screenShakeOffsetX, 200 + screenShakeOffsetY);
    setcolor(WHITE);
    rectangle(10 + screenShakeOffsetX, 190 + screenShakeOffsetY, 110 + screenShakeOffsetX, 200 + screenShakeOffsetY);
    outtextxy(115 + screenShakeOffsetX, 190 + screenShakeOffsetY, "Spread(G)");

    if (skills.rageActive) {
        setcolor(YELLOW);
        settextstyle(14, 0, "Arial");
        outtextxy(W / 2 - 50 + screenShakeOffsetX, H - 30 + screenShakeOffsetY, "RAGE MODE!");
        settextstyle(16, 0, "Consolas");
        setcolor(WHITE);
    }

    if (bombShockwaveActive) {
        int currentRadius = (shockwaveRadius * shockwaveAge) / BOMB_SHOCKWAVE_LIFETIME;

        int waveThickness = 10 - (shockwaveAge * 10 / BOMB_SHOCKWAVE_LIFETIME);
        if (waveThickness < 1) waveThickness = 1;

        setcolor(WHITE);
        setlinestyle(PS_SOLID, waveThickness);
        circle(shockwaveCenterX + screenShakeOffsetX, shockwaveCenterY + screenShakeOffsetY, currentRadius);
        setlinestyle(PS_SOLID, 1);
    }

    if (gs == OVER) {
        DrawGameOver();
    } else if (gs == PAUSE) {
        setcolor(WHITE);
        settextstyle(30, 0, "Arial");
        outtextxy(W / 2 - 60 + screenShakeOffsetX, H / 2 + screenShakeOffsetY, "PAUSED");
        settextstyle(16, 0, "Consolas");
        outtextxy(W / 2 - 80 + screenShakeOffsetX, H / 2 + 40 + screenShakeOffsetY, "Press P to resume");
    }

    EndBatchDraw();
}

bool Hit(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
    return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
}

void TriggerScreenShake(int duration, int intensity) {
    screenShakeTimer = duration;
    screenShakeOffsetX = (rand() % (intensity * 2 + 1)) - intensity;
    screenShakeOffsetY = (rand() % (intensity * 2 + 1)) - intensity;
}

void UseSkill(int skill) {
    if (skill == SKILL_BOMB && skills.bombEnergy >= 100.0) {
        bombShockwaveActive = true;
        shockwaveCenterX = p.x + 10;
        shockwaveCenterY = p.y + 15;
        shockwaveRadius = BOMB_SHOCKWAVE_MAX_RADIUS;
        shockwaveAge = 0;
        CreateBombWaveParticles(shockwaveCenterX, shockwaveCenterY, 50);

        TriggerScreenShake(SCREEN_SHAKE_DURATION, 3);

        skills.bombEnergy = 0.0;
    } else if (skill == SKILL_LASER && skills.laserEnergy >= 100.0) {
        laserActive = true;
        laserTimer = LASER_DURATION_FRAMES;
        skills.laserEnergy = 0.0;
    } else if (skill == SKILL_RAGE && skills.rageEnergy >= 100.0 && !skills.rageActive) {
        skills.rageActive = true;
        skills.rageTimer = 600;
        shanghai += 10.0;
        skills.rageEnergy = 0.0;
    } else if (skill == SKILL_HOMING_MISSILE && skills.homingMissileEnergy >= 100.0) {
        int missilesFired = 0;
        for (int i = 0; i < MAX_BULLETS && missilesFired < 5; i++) {
            if (!b[i].live) {
                b[i].x = p.x + 8;
                b[i].y = p.y;
                b[i].live = true;
                b[i].shooterType = 0;
                b[i].direction = 0;
                b[i].type = BULLET_HOMING;
                b[i].targetEnemyIndex = -1;
                b[i].angle = -M_PI / 2;

                double minDist = DBL_MAX;
                int closestEnemyIdx = -1;
                for (int j = 0; j < MAX_ENEMIES; j++) {
                    if (e[j].live) {
                        double dist = sqrt(pow(b[i].x - (e[j].x + e[j].width / 2), 2) + pow(b[i].y - (e[j].y + e[j].height / 2), 2));
                        if (dist < minDist) {
                            minDist = dist;
                            closestEnemyIdx = j;
                        }
                    }
                }
                b[i].targetEnemyIndex = closestEnemyIdx;

                missilesFired++;
            }
        }
        skills.homingMissileEnergy = 0.0;
    } else if (skill == SKILL_SPREAD_SHOT && skills.spreadShotEnergy >= 100.0) {
        int centerBulletIdx = -1;
        for (int i = 0; i < MAX_BULLETS; i++) {
            if (!b[i].live) {
                centerBulletIdx = i;
                b[i].x = p.x + 8;
                b[i].y = p.y;
                b[i].live = true;
                b[i].shooterType = 0;
                b[i].direction = 0;
                b[i].type = BULLET_SPREAD;
                b[i].targetEnemyIndex = -1;
                b[i].angle = -M_PI / 2;
                break;
            }
        }
        if (centerBulletIdx != -1) {
            int shotsPerSide = NUM_SPREAD_SHOTS / 2;
            for (int side = -shotsPerSide; side <= shotsPerSide; side++) {
                if (side == 0) continue;

                for (int i = 0; i < MAX_BULLETS; i++) {
                    if (!b[i].live) {
                        b[i].x = p.x + 8;
                        b[i].y = p.y;
                        b[i].live = true;
                        b[i].shooterType = 0;
                        b[i].direction = 0;
                        b[i].type = BULLET_SPREAD;
                        b[i].targetEnemyIndex = -1;
                        b[i].angle = -M_PI / 2 + (side * SPREAD_SHOT_ANGLE_INCREMENT * M_PI / 180.0);
                        break;
                    }
                }
            }
        }
        skills.spreadShotEnergy = 0.0;
    }
}

void Update() {
    if (GetAsyncKeyState(VK_LEFT) & 0x8000 && p.x > 5) p.x -= 5;
    if (GetAsyncKeyState(VK_RIGHT) & 0x8000 && p.x < W - 25) p.x += 5;
    if (GetAsyncKeyState(VK_UP) & 0x8000 && p.y > 5) p.y -= 5;
    if (GetAsyncKeyState(VK_DOWN) & 0x8000 && p.y < H - 30) p.y += 5;

    static bool pPressedLastFrame = false;
    bool pCurrentState = (GetAsyncKeyState('P') & 0x8000) != 0;
    if (pCurrentState && !pPressedLastFrame) {
        if (gs == PLAY) {
            gs = PAUSE;
        } else if (gs == PAUSE) {
            gs = PLAY;
        }
    }
    pPressedLastFrame = pCurrentState;

    if (gs == PLAY) {
        static bool fPressedLastFrame = false;
        static bool zPressedLastFrame = false;
        static bool xPressedLastFrame = false;
        static bool hPressedLastFrame = false;
        static bool gPressedLastFrame = false;

        bool fCurrentState = (GetAsyncKeyState('F') & 0x8000) != 0;
        bool zCurrentState = (GetAsyncKeyState('Z') & 0x8000) != 0;
        bool xCurrentState = (GetAsyncKeyState('X') & 0x8000) != 0;
        bool hCurrentState = (GetAsyncKeyState('H') & 0x8000) != 0;
        bool gCurrentState = (GetAsyncKeyState('G') & 0x8000) != 0;

        if (fCurrentState && !fPressedLastFrame && skills.bombEnergy >= 100.0 && !bombShockwaveActive) {
            UseSkill(SKILL_BOMB);
        }
        if (zCurrentState && !zPressedLastFrame && skills.laserEnergy >= 100.0 && !laserActive) {
            UseSkill(SKILL_LASER);
        }
        if (xCurrentState && !xPressedLastFrame && skills.rageEnergy >= 100.0 && !skills.rageActive) {
            UseSkill(SKILL_RAGE);
        }
        if (hCurrentState && !hPressedLastFrame && skills.homingMissileEnergy >= 100.0) {
            UseSkill(SKILL_HOMING_MISSILE);
        }
        if (gCurrentState && !gPressedLastFrame && skills.spreadShotEnergy >= 100.0) {
            UseSkill(SKILL_SPREAD_SHOT);
        }

        fPressedLastFrame = fCurrentState;
        zPressedLastFrame = zCurrentState;
        xPressedLastFrame = xCurrentState;
        hPressedLastFrame = hCurrentState;
        gPressedLastFrame = gCurrentState;
    }

    static DWORD lastShotTime = 0;
    if ((GetAsyncKeyState(VK_SPACE) & 0x8000) && GetTickCount() - lastShotTime > 150 && !laserActive && !bombShockwaveActive) {
        for (int i = 0; i < MAX_BULLETS; i++) {
            if (!b[i].live) {
                b[i].x = p.x + 8;
                b[i].y = p.y;
                b[i].live = true;
                b[i].shooterType = 0;
                b[i].direction = 0;
                b[i].type = BULLET_NORMAL;
                b[i].targetEnemyIndex = -1;
                b[i].angle = -M_PI / 2;
                break;
            }
        }
        lastShotTime = GetTickCount();
    }

    if (laserActive) {
        for (int i = 0; i < MAX_ENEMIES; i++) {
            if (e[i].live) {
                e[i].hp -= (int)(shanghai * 3.0);
                if (e[i].hp <= 0) {
                    sc += 5 * lv * e[i].initialHp;
                    combo++;
                    comboTimer = COMBO_TIMEOUT_FRAMES;
                    CreateExplosion(e[i].x + e[i].width / 2, e[i].y + e[i].height / 2);
                    e[i].live = false;
                }
            }
        }
        laserTimer--;
        if (laserTimer <= 0) {
            laserActive = false;
        }
    }

    if (bombShockwaveActive) {
        shockwaveAge++;
        if (shockwaveAge >= BOMB_SHOCKWAVE_LIFETIME) {
            bombShockwaveActive = false;

            for (int id : enemiesHitByBomb) {
                if (e[id].live) {
                    sc += 5 * lv * e[id].initialHp;
                    combo++;
                    comboTimer = COMBO_TIMEOUT_FRAMES;
                    CreateExplosion(e[id].x + e[id].width / 2, e[id].y + e[id].height / 2);

                    skills.bombEnergy += 10.0;
                    skills.laserEnergy += 8.0;
                    skills.rageEnergy += 5.0;

                    if (skills.bombEnergy > 100.0) skills.bombEnergy = 100.0;
                    if (skills.laserEnergy > 100.0) skills.laserEnergy = 100.0;
                    if (skills.rageEnergy > 100.0) skills.rageEnergy = 100.0;

                    e[id].live = false;

                    if (e[id].type == ENEMY_BOSS) {
                        achievements[2].unlocked = achievements[2].checkCondition();
                    }
                }
            }
            enemiesHitByBomb.clear();
        } else {
            int currentRadius = (shockwaveRadius * shockwaveAge) / BOMB_SHOCKWAVE_LIFETIME;
            for (int i = 0; i < MAX_ENEMIES; i++) {
                if (e[i].live) {
                    int dx = (e[i].x + e[i].width / 2) - shockwaveCenterX;
                    int dy = (e[i].y + e[i].height / 2) - shockwaveCenterY;
                    double distance = sqrt(dx * dx + dy * dy);

                    if (distance <= currentRadius) {
                         bool alreadyRecorded = false;
                         for (int recordedId : enemiesHitByBomb) {
                             if (recordedId == i) {
                                 alreadyRecorded = true;
                                 break;
                             }
                         }
                         if (!alreadyRecorded) {
                             enemiesHitByBomb.push_back(i);
                             CreateExplosion(e[i].x + e[i].width / 2, e[i].y + e[i].height / 2);
                         }
                    }
                }
            }
        }
    }

    if (screenShakeTimer > 0) {
        screenShakeTimer--;
        if (screenShakeTimer > 0) {
            screenShakeOffsetX = (rand() % 7) - 3;
            screenShakeOffsetY = (rand() % 7) - 3;
        } else {
            screenShakeOffsetX = 0;
            screenShakeOffsetY = 0;
        }
    }

    if (comboTimer > 0) {
        comboTimer--;
    } else {
        if (combo > 1) {
            combo = 0;
        }
    }

    if (bossWarningTimer > 0) {
        bossWarningTimer--;
    }

    for (int i = 0; i < MAX_BULLETS; i++) {
        if (b[i].live) {
            if (b[i].type == BULLET_HOMING) {
                if (b[i].targetEnemyIndex != -1 && e[b[i].targetEnemyIndex].live) {
                    int targetX = e[b[i].targetEnemyIndex].x + e[b[i].targetEnemyIndex].width / 2;
                    int targetY = e[b[i].targetEnemyIndex].y + e[b[i].targetEnemyIndex].height / 2;

                    double targetAngle = atan2(targetY - b[i].y, targetX - b[i].x);

                    double angleDiff = targetAngle - b[i].angle;
                    while (angleDiff > M_PI) angleDiff -= 2 * M_PI;
                    while (angleDiff < -M_PI) angleDiff += 2 * M_PI;

                    double maxTurn = 0.1;
                    if (angleDiff > maxTurn) angleDiff = maxTurn;
                    if (angleDiff < -maxTurn) angleDiff = -maxTurn;

                    b[i].angle += angleDiff;
                }

                b[i].x += cos(b[i].angle) * HOMING_MISSILE_SPEED;
                b[i].y += sin(b[i].angle) * HOMING_MISSILE_SPEED;

                if (b[i].x < -10 || b[i].x > W + 10 || b[i].y < -10 || b[i].y > H + 10) {
                    b[i].live = false;
                }
            } else if (b[i].type == BULLET_SPREAD) {
                b[i].x += cos(b[i].angle) * 8;
                b[i].y += sin(b[i].angle) * 8;

                if (b[i].x < -10 || b[i].x > W + 10 || b[i].y < -10 || b[i].y > H + 10) {
                    b[i].live = false;
                }
            } else {
                if (b[i].direction == 0) {
                    b[i].y -= 8;
                } else {
                    b[i].y += 8;
                }
                if (b[i].y < -10 || b[i].y > H + 10) b[i].live = false;
            }
        }
    }

    const int ENEMY_SPAWN_RATE = 60;
    if (ec++ % ENEMY_SPAWN_RATE == 0) {
        for (int i = 0; i < MAX_ENEMIES; i++) {
            if (!e[i].live) {
                if (lv >= BOSS_LEVEL && !bossSpawnedThisLevel) {
                    e[i].x = W / 2 - 50;
                    e[i].y = -60;
                    e[i].live = true;
                    e[i].type = ENEMY_BOSS;
                    e[i].hp = 200 + lv * 30;
                    e[i].initialHp = e[i].hp;
                    e[i].width = 100;
                    e[i].height = 60;
                    e[i].laserTimer = 0;
                    e[i].isFiringLaser = false;
                    e[i].targetX = rand() % (W - e[i].width);
                    e[i].shootTimer = 0;

                    bossSpawnedThisLevel = true;

                    strcpy(bossWarningText, "BOSS APPEARS!");
                    bossWarningTimer = 120;
                    break;
                } else {
                    e[i].x = rand() % (W - 30);
                    e[i].y = -30;
                    e[i].live = true;
                    e[i].type = ENEMY_NORMAL;
                    e[i].hp = rand() % 11 + 20 + lv;
                    e[i].initialHp = e[i].hp;
                    e[i].width = 30;
                    e[i].height = 30;
                    e[i].laserTimer = 0;
                    e[i].isFiringLaser = false;
                    e[i].targetX = 0;
                    e[i].shootTimer = 0;
                    break;
                }
            }
        }
    }

    if (bossSpawnedThisLevel) {
        for (int i = 0; i < MAX_ENEMIES; i++) {
            if (e[i].live && e[i].type == ENEMY_BOSS) {
                if (abs(e[i].x - e[i].targetX) < 2) {
                    e[i].targetX = rand() % (W - e[i].width);
                } else {
                    if (e[i].x < e[i].targetX) {
                        e[i].x += 1;
                    } else if (e[i].x > e[i].targetX) {
                        e[i].x -= 1;
                    }
                }
                e[i].y = 50;

                e[i].laserTimer++;
                if (e[i].laserTimer > BOSS_ATTACK_INTERVAL_FRAMES && !e[i].isFiringLaser) {
                    e[i].isFiringLaser = true;
                    e[i].laserTimer = 0;
                    if (screenShakeTimer <= 0) {
                        TriggerScreenShake(5, 1);
                    }
                }

                if (e[i].isFiringLaser) {
                    e[i].laserTimer++;
                    if (e[i].laserTimer >= BOSS_LASER_DURATION_FRAMES) {
                        e[i].isFiringLaser = false;
                    }
                }
                break;
            }
        }
    }

    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (e[i].live && e[i].type == ENEMY_NORMAL) {
            e[i].shootTimer++;
            if (e[i].shootTimer > ENEMY_SHOOT_INTERVAL_FRAMES) {
                e[i].shootTimer = 0;

                for (int j = 0; j < MAX_BULLETS; j++) {
                    if (!b[j].live) {
                        b[j].x = e[i].x + e[i].width / 2 - 2;
                        b[j].y = e[i].y + e[i].height;
                        b[j].live = true;
                        b[j].shooterType = 1;
                        b[j].direction = 1;
                        b[j].type = BULLET_NORMAL;
                        b[j].targetEnemyIndex = -1;
                        b[j].angle = M_PI / 2;
                        break;
                    }
                }
            }
        }
    }


    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (e[i].live) {
            if (e[i].type == ENEMY_BOSS) {
            } else {
                e[i].y += 2;
            }
            if (e[i].y > H) e[i].live = false;
        }
    }

    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (e[i].live && e[i].type == ENEMY_BOSS && e[i].isFiringLaser) {
            int laserLeft = e[i].x + 5;
            int laserRight = e[i].x + e[i].width - 5;
            int laserTop = e[i].y + e[i].height;

            int playerCenterX = p.x + 10;
            int playerCenterY = p.y + 15;

            if (playerCenterX >= laserLeft && playerCenterX <= laserRight && playerCenterY >= laserTop) {
                p.hp -= 1;

                CreateBleedingParticles(p.x + 10, p.y + 15, 2);

                if (p.hp <= 0) {
                    CreateExplosion(p.x + 10, p.y + 15);
                    Sleep(1000);
                    gs = OVER;
                    gameOverTime = GetTickCount();
                }
            }
        }
    }


    if (!laserActive && !bombShockwaveActive) {
        for (int i = 0; i < MAX_BULLETS; i++) {
            if (b[i].live && b[i].shooterType == 0) {
                for (int j = 0; j < MAX_ENEMIES; j++) {
                    if (e[j].live && Hit(b[i].x, b[i].y, 4, 10, e[j].x, e[j].y, e[j].width, e[j].height)) {
                        b[i].live = false;
                        e[j].hp -= (int)shanghai;

                        if (e[j].hp > 0) {
                             CreateBleedingParticles(e[j].x + e[j].width / 2, e[j].y + e[j].height / 2, 3);
                        }

                        if (e[j].hp <= 0) {
                            e[j].live = false;
                            sc += 5 * lv * e[j].initialHp;

                            combo++;
                            comboTimer = COMBO_TIMEOUT_FRAMES;

                            skills.bombEnergy += 10.0;
                            skills.laserEnergy += 8.0;
                            skills.rageEnergy += 5.0;
                            skills.homingMissileEnergy += 10.0;
                            skills.spreadShotEnergy += 10.0;

                            if (skills.bombEnergy > 100.0) skills.bombEnergy = 100.0;
                            if (skills.laserEnergy > 100.0) skills.laserEnergy = 100.0;
                            if (skills.rageEnergy > 100.0) skills.rageEnergy = 100.0;
                            if (skills.homingMissileEnergy > 100.0) skills.homingMissileEnergy = 100.0;
                            if (skills.spreadShotEnergy > 100.0) skills.spreadShotEnergy = 100.0;

                            CreateExplosion(e[j].x + e[j].width / 2, e[j].y + e[j].height / 2);

                            if (sc >= lv * 150) {
                                lv++;
                                shanghai += 1;
                                shanghai = min(lv / 2 + 1, (int)shanghai);
                            }

                            achievements[0].unlocked = achievements[0].checkCondition();
                            achievements[1].unlocked = achievements[1].checkCondition();
                            if (e[j].type == ENEMY_BOSS) {
                                achievements[2].unlocked = achievements[2].checkCondition();
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    if (!bombShockwaveActive) {
        for (int i = 0; i < MAX_BULLETS; i++) {
            if (b[i].live && (b[i].shooterType == 1 || b[i].shooterType == 2) && b[i].direction == 1) {
                if (Hit(p.x, p.y, 20, 30, b[i].x, b[i].y, 4, 10)) {
                    b[i].live = false;
                    p.hp -= 1;

                    CreateBleedingParticles(p.x + 10, p.y + 15, 10);

                    if (screenShakeTimer <= 0) {
                        TriggerScreenShake(5, 1);
                    }

                    if (p.hp <= 0) {
                        CreateExplosion(p.x + 10, p.y + 15);
                        Sleep(1000);
                        gs = OVER;
                        gameOverTime = GetTickCount();
                    }
                }
            }
        }
    }

    if (!bombShockwaveActive) {
        for (int i = 0; i < MAX_ENEMIES; i++) {
            if (e[i].live && Hit(p.x, p.y, 20, 30, e[i].x, e[i].y, e[i].width, e[i].height)) {
                e[i].live = false;
                p.hp -= 1;

                 CreateBleedingParticles(p.x + 10, p.y + 15, 10);

                if (screenShakeTimer <= 0) {
                    TriggerScreenShake(5, 1);
                }

                if (p.hp <= 0) {
                    CreateExplosion(p.x + 10, p.y + 15);
                    Sleep(1000);
                    gs = OVER;
                    gameOverTime = GetTickCount();
                }
            }
        }
    }

    if (rand() % 100 == 0) {
        for (int i = 0; i < MAX_POWERUPS; i++) {
            if (!pu[i].live) {
                pu[i].x = rand() % (W - 12) + 6;
                pu[i].y = -10;
                pu[i].live = true;
                pu[i].type = rand() % 3;
                break;
            }
        }
    }

    for (int i = 0; i < MAX_POWERUPS; i++) {
        if (pu[i].live) {
            pu[i].y += 3;
            if (pu[i].y > H + 10) pu[i].live = false;

            if (Hit(p.x, p.y, 20, 30, pu[i].x - 6, pu[i].y - 6, 12, 12)) {
                switch (pu[i].type) {
                    case POWERUP_ATTACK:
                        shanghai *= 1.05;
                        break;
                    case POWERUP_HP:
                        p.hp += 2;
                        if (p.hp > p.maxHp) p.hp = p.maxHp;
                        break;
                    case POWERUP_BOMB:
                        skills.bombEnergy += 30.0;
                        if (skills.bombEnergy > 100.0) skills.bombEnergy = 100.0;
                        break;
                }
                pu[i].live = false;
            }
        }
    }

    if (skills.rageActive) {
        skills.rageTimer--;
        if (skills.rageTimer <= 0) {
            skills.rageActive = false;
            shanghai -= 10.0;
        }
    }

    if (gs == PLAY && !bombShockwaveActive) {
        if (rand() % 2 == 0) {
            CreateJetTrail(p.x + 5, p.y + 30, 1);
        }
        if (rand() % 2 == 0) {
            CreateJetTrail(p.x + 15, p.y + 30, 1);
        }

        for (int i = 0; i < MAX_ENEMIES; i++) {
            if (e[i].live && e[i].type != ENEMY_BOSS && rand() % 3 == 0) {
                CreateJetTrail(e[i].x + e[i].width / 4, e[i].y, 1);
            }
            if (e[i].live && e[i].type != ENEMY_BOSS && rand() % 3 == 0) {
                CreateJetTrail(e[i].x + 3 * e[i].width / 4, e[i].y, 1);
            }
        }
    }

    UpdateExplosions();
}

int main() {
    initgraph(W, H);

    DWORD lastTime = GetTickCount();
    while (true) {
        DWORD currentTime = GetTickCount();
        if (currentTime - lastTime >= MS_PER_FRAME) {
            if (gs == START) {
                if (GetAsyncKeyState(VK_SPACE) & 0x8000) {
                    gs = PLAY;
                    InitGame();
                }
            } else if (gs == OVER) {
                if (currentTime - gameOverTime >= 2000) {
                    gs = START;
                }
            } else if (gs == PAUSE) {
                if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
                    break;
                }
            } else {
                if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
                    break;
                }
            }

            switch (gs) {
                case START:
                    DrawStart();
                    break;
                case PLAY:
                    Update();
                    GameDraw();
                    break;
                case OVER:
                    GameDraw();
                    break;
                case PAUSE:
                    GameDraw();
                    break;
            }
            lastTime = currentTime;
        }
        Sleep(1);
    }
    closegraph();
    return 0;
}

如果有建议请在评论区给出