#include #include #include #include using namespace std;

// 仅Windows可用,Dev-C++/VS专用 #include <conio.h> #include <windows.h>

const int MAP_W = 40; const int MAP_H = 20;

struct Point { int x, y; Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {} };

struct Bullet : Point { int dx, dy; Bullet(int x_, int y_, int dx_, int dy_) : Point(x_, y_), dx(dx_), dy(dy_) {} };

struct Enemy : Point { int hp; int hurtCD; Enemy(int x_, int y_) : Point(x_, y_), hp(100), hurtCD(0) {} };

struct Box : Point { bool get; Box(int x_, int y_) : Point(x_, y_), get(false) {} };

// 全局变量 int playerX = 5, playerY = 10; int playerHp = 100; int ammo = 30; int supply = 0; vector bullets; vector enemies; vector boxes; Point exitPoint; bool gameOver = false; bool win = false; int aimDir = 1; // 1右 2下 3左 4上

// 隐藏光标 void HideCursor() { CONSOLE_CURSOR_INFO cur; cur.dwSize = 1; cur.bVisible = false; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur); }

// 光标定位,无闪烁 void GotoXY(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }

// 绘制地图 void DrawMap() { GotoXY(0, 0); for (int y = 0; y < MAP_H; y++) { for (int x = 0; x < MAP_W; x++) { bool drawFlag = true;

        if (x == exitPoint.x && y == exitPoint.y) {
            cout << "E";
            drawFlag = false;
        }
        if (drawFlag) {
            // 替换为下标循环 C++98兼容
            for (int i = 0; i < boxes.size(); i++) {
                Box& box = boxes[i];
                if (x == box.x && y == box.y && !box.get) {
                    cout << "□";
                    drawFlag = false;
                    break;
                }
            }
        }
        if (drawFlag) {
            for (int i = 0; i < enemies.size(); i++) {
                Enemy& e = enemies[i];
                if (x == e.x && y == e.y && e.hp > 0) {
                    cout << "●";
                    drawFlag = false;
                    break;
                }
            }
        }
        if (drawFlag) {
            for (int i = 0; i < bullets.size(); i++) {
                Bullet& bul = bullets[i];
                if (x == bul.x && y == bul.y) {
                    cout << "·";
                    drawFlag = false;
                    break;
                }
            }
        }
        if (drawFlag) {
            if (x == playerX && y == playerY)
                cout << "P";
            else
                cout << " ";
        }
    }
    cout << endl;
}
// 状态栏
cout << "=== 三角洲行动 控制台版 ===" << endl;
cout << "血量:" << playerHp << " | 弹药:" << ammo << " | 物资:" << supply << "/5" << endl;
cout << "WASD移动 | 方向键瞄准 | 空格射击 | R换弹";
for (int i = 0; i < 40; i++) cout << " ";

}

void InitGame() { srand((unsigned)time(NULL)); exitPoint.x = MAP_W - 5; exitPoint.y = MAP_H / 2;

boxes.clear();
for (int i = 0; i < 5; i++) {
    int bx = rand() % (MAP_W - 10) + 8;
    int by = rand() % (MAP_H - 2) + 1;
    boxes.push_back(Box(bx, by));
}

enemies.clear();
for (int i = 0; i < 3; i++) {
    int ex = rand() % (MAP_W - 15) + 10;
    int ey = rand() % (MAP_H - 2) + 1;
    enemies.push_back(Enemy(ex, ey));
}

bullets.clear();
playerX = 5; playerY = 10;
playerHp = 100;
ammo = 30;
supply = 0;
gameOver = false;
win = false;

}

void Shoot() { if (ammo <= 0) return; ammo--; int dx = 0, dy = 0; switch (aimDir) { case 1: dx = 1; break; case 2: dy = 1; break; case 3: dx = -1; break; case 4: dy = -1; break; } bullets.push_back(Bullet(playerX + dx, playerY + dy, dx, dy)); }

void UpdateBullet() { vector newBul; for (int i = 0; i < bullets.size(); i++) { Bullet& bul = bullets[i]; bul.x += bul.dx; bul.y += bul.dy; if (bul.x < 0 || bul.x >= MAP_W || bul.y < 0 || bul.y >= MAP_H) continue;

    bool hit = false;
    for (int j = 0; j < enemies.size(); j++) {
        Enemy& e = enemies[j];
        if (e.hp > 0 && bul.x == e.x && bul.y == e.y) {
            e.hp -= 20;
            hit = true;
            break;
        }
    }
    if (!hit) newBul.push_back(bul);
}
bullets.swap(newBul);

}

void EnemyAI() { for (int i = 0; i < enemies.size(); i++) { Enemy& e = enemies[i]; if (e.hp <= 0) continue;

    if (e.hurtCD > 0) e.hurtCD--;
    // 追击玩家
    if (e.x < playerX) e.x++;
    else if (e.x > playerX) e.x--;
    if (e.y < playerY) e.y++;
    else if (e.y > playerY) e.y--;

    if (e.x == playerX && e.y == playerY && e.hurtCD == 0) {
        playerHp -= 10;
        e.hurtCD = 30;
        if (playerHp <= 0) gameOver = true;
    }
}

}

void PickSupply() { for (int i = 0; i < boxes.size(); i++) { Box& box = boxes[i]; if (!box.get && box.x == playerX && box.y == playerY) { box.get = true; supply++; } } }

void CheckWin() { if (supply >= 5 && playerX == exitPoint.x && playerY == exitPoint.y) { win = true; gameOver = true; } }

void Input() { if (!_kbhit()) return; unsigned char ch = _getch(); // 方向键扩展码 if (ch == 0xE0) { ch = _getch(); switch (ch) { case 77: aimDir = 1; break; case 80: aimDir = 2; break; case 75: aimDir = 3; break; case 72: aimDir = 4; break; } return; } // 移动 if (ch == 'w' && playerY > 0) playerY--; if (ch == 's' && playerY < MAP_H - 1) playerY++; if (ch == 'a' && playerX > 0) playerX--; if (ch == 'd' && playerX < MAP_W - 1) playerX++; // 射击换弹 if (ch == ' ') Shoot(); if (ch == 'r' || ch == 'R') ammo = 30; }

int main() { HideCursor(); InitGame(); while (!gameOver) { DrawMap(); Input(); UpdateBullet(); EnemyAI(); PickSupply(); CheckWin(); Sleep(40); } GotoXY(0, MAP_H + 4); if (win) cout << "===== 撤离成功,任务完成!=\n"; else cout << "= 阵亡,任务失败 =====" << endl; system("pause"); return 0; }

1 comments

  • 1