太空射击2.0

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#include<bits/stdc++.h>
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>

using namespace std;

// 全局常量定义
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 30
#define PLAYER_WIDTH 3
#define PLAYER_HEIGHT 2
#define MAX_ENEMIES 20
#define MAX_BULLETS 50
#define MAX_EXPLOSIONS 10

// 颜色定义宏
#define SET_COLOR(color) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color)
#define COLOR_WHITE 7
#define COLOR_RED 12
#define COLOR_GREEN 10
#define COLOR_BLUE 9
#define COLOR_YELLOW 14
#define COLOR_CYAN 11
#define COLOR_MAGENTA 13

// 坐标结构体
struct Point {
    int x, y;
    Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
};

// 玩家结构体
struct Player {
    Point pos;          // 位置
    int hp;             // 血量
    int max_hp;         // 最大血量
    int level;          // 等级
    int exp;            // 经验
    int exp_next;       // 升级所需经验
    int score;          // 分数
    bool invulnerable;  // 无敌状态
    int invul_time;     // 无敌剩余时间
} player;

// 敌机结构体
struct Enemy {
    Point pos;          // 位置
    int hp;             // 血量
    int speed;          // 速度
    int type;           // 类型(0-普通,1-快速,2-重型)
    bool alive;         // 是否存活
    int shoot_timer;    // 射击计时器
} enemies[MAX_ENEMIES];

// 子弹结构体
struct Bullet {
    Point pos;          // 位置
    int dir;            // 方向(1-玩家子弹向上,-1-敌机子弹向下)
    int speed;          // 速度
    bool alive;         // 是否存活
    int damage;         // 伤害
} bullets[MAX_BULLETS];

// 爆炸效果结构体
struct Explosion {
    Point pos;          // 位置
    int frame;          // 当前帧
    int max_frames;     // 总帧数
    bool alive;         // 是否存活
} explosions[MAX_EXPLOSIONS];

// 全局变量
int wave = 1;          // 当前波数
int enemies_killed = 0;// 本波击杀数
int enemies_needed = 10;// 本波需要击杀的敌机数
bool game_over = false;// 游戏结束标志
bool game_win = false; // 游戏胜利标志
int game_timer = 0;    // 游戏计时器

// 设置控制台光标位置
void set_cursor_pos(int x, int y) {
    COORD coord = { (SHORT)x, (SHORT)y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

// 隐藏控制台光标
void hide_cursor() {
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

// 清屏(高效版,避免闪烁)
void clear_screen() {
    set_cursor_pos(0, 0);
    for (int i = 0; i < SCREEN_HEIGHT; i++) {
        for (int j = 0; j < SCREEN_WIDTH; j++) {
            cout << ' ';
        }
        cout << endl;
    }
    set_cursor_pos(0, 0);
}

// 绘制玩家
void draw_player() {
    if (player.invulnerable && (game_timer % 10) < 5) return; // 无敌闪烁
    
    SET_COLOR(COLOR_GREEN);
    // 绘制战机形状
    set_cursor_pos(player.pos.x, player.pos.y);     cout << '^';
    set_cursor_pos(player.pos.x-1, player.pos.y+1); cout << '<';
    set_cursor_pos(player.pos.x, player.pos.y+1);   cout << '|';
    set_cursor_pos(player.pos.x+1, player.pos.y+1); cout << '>';
    SET_COLOR(COLOR_WHITE);
}

// 初始化玩家
void init_player() {
    player.pos = Point(SCREEN_WIDTH/2, SCREEN_HEIGHT-5);
    player.hp = 100;
    player.max_hp = 100;
    player.level = 1;
    player.exp = 0;
    player.exp_next = 100;
    player.score = 0;
    player.invulnerable = false;
    player.invul_time = 0;
}

// 初始化敌机
void init_enemies() {
    for (int i = 0; i < MAX_ENEMIES; i++) {
        enemies[i].alive = false;
    }
    enemies_killed = 0;
}

// 生成敌机
void spawn_enemy() {
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (!enemies[i].alive) {
            // 随机位置(顶部)
            enemies[i].pos.x = rand() % (SCREEN_WIDTH - 6) + 3;
            enemies[i].pos.y = rand() % 5 + 1;
            
            // 根据波数调整敌机类型
            int r = rand() % 100;
            if (r < 60 - wave * 5) enemies[i].type = 0;    // 普通
            else if (r < 85 - wave * 3) enemies[i].type = 1;// 快速
            else enemies[i].type = 2;                       // 重型
            
            // 设置属性
            switch (enemies[i].type) {
                case 0: // 普通敌机
                    enemies[i].hp = 10 + wave * 2;
                    enemies[i].speed = 1 + wave / 3;
                    break;
                case 1: // 快速敌机
                    enemies[i].hp = 5 + wave;
                    enemies[i].speed = 2 + wave / 2;
                    break;
                case 2: // 重型敌机
                    enemies[i].hp = 30 + wave * 5;
                    enemies[i].speed = 1;
                    break;
            }
            
            enemies[i].alive = true;
            enemies[i].shoot_timer = rand() % 60 + 30; // 随机射击间隔
            break;
        }
    }
}

// 绘制敌机
void draw_enemies() {
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].alive) {
            // 根据类型设置颜色
            switch (enemies[i].type) {
                case 0: SET_COLOR(COLOR_RED); break;
                case 1: SET_COLOR(COLOR_MAGENTA); break;
                case 2: SET_COLOR(COLOR_YELLOW); break;
            }
            
            // 绘制敌机形状
            set_cursor_pos(enemies[i].pos.x, enemies[i].pos.y);     cout << '#';
            set_cursor_pos(enemies[i].pos.x-1, enemies[i].pos.y+1); cout << '[';
            set_cursor_pos(enemies[i].pos.x+1, enemies[i].pos.y+1); cout << ']';
            
            // 绘制重型敌机额外装甲
            if (enemies[i].type == 2) {
                set_cursor_pos(enemies[i].pos.x-2, enemies[i].pos.y+1); cout << '[';
                set_cursor_pos(enemies[i].pos.x+2, enemies[i].pos.y+1); cout << ']';
            }
            
            SET_COLOR(COLOR_WHITE);
        }
    }
}

// 初始化子弹
void init_bullets() {
    for (int i = 0; i < MAX_BULLETS; i++) {
        bullets[i].alive = false;
    }
}

// 发射子弹
void fire_bullet(int x, int y, int dir, int damage) {
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (!bullets[i].alive) {
            bullets[i].pos = Point(x, y);
            bullets[i].dir = dir;
            bullets[i].damage = damage;
            
            // 设置速度
            if (dir == 1) bullets[i].speed = 5;    // 玩家子弹
            else bullets[i].speed = 2 + wave / 3;  // 敌机子弹
            
            bullets[i].alive = true;
            break;
        }
    }
}

// 绘制子弹
void draw_bullets() {
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].alive) {
            SET_COLOR(bullets[i].dir == 1 ? COLOR_CYAN : COLOR_YELLOW);
            set_cursor_pos(bullets[i].pos.x, bullets[i].pos.y);
            cout << (bullets[i].dir == 1 ? '|' : 'v');
            SET_COLOR(COLOR_WHITE);
        }
    }
}

// 初始化爆炸效果
void init_explosions() {
    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        explosions[i].alive = false;
    }
}

// 创建爆炸效果
void create_explosion(int x, int y) {
    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (!explosions[i].alive) {
            explosions[i].pos = Point(x, y);
            explosions[i].frame = 0;
            explosions[i].max_frames = 8;
            explosions[i].alive = true;
            break;
        }
    }
}

// 绘制爆炸效果
void draw_explosions() {
    for (int i = 0; i < MAX_EXPLOSIONS; i++) {
        if (explosions[i].alive) {
            SET_COLOR(COLOR_YELLOW);
            int frame = explosions[i].frame;
            int x = explosions[i].pos.x;
            int y = explosions[i].pos.y;
            
            // 绘制不同帧的爆炸效果
            switch (frame) {
                case 0: set_cursor_pos(x, y); cout << '*'; break;
                case 1: 
                    set_cursor_pos(x-1, y); cout << '*';
                    set_cursor_pos(x+1, y); cout << '*';
                    set_cursor_pos(x, y-1); cout << '*';
                    set_cursor_pos(x, y+1); cout << '*';
                    break;
                case 2: 
                    set_cursor_pos(x-1, y-1); cout << '*';
                    set_cursor_pos(x+1, y-1); cout << '*';
                    set_cursor_pos(x-1, y+1); cout << '*';
                    set_cursor_pos(x+1, y+1); cout << '*';
                    break;
                case 3: 
                    set_cursor_pos(x-2, y); cout << '#';
                    set_cursor_pos(x+2, y); cout << '#';
                    set_cursor_pos(x, y-2); cout << '#';
                    set_cursor_pos(x, y+2); cout << '#';
                    break;
                default:
                    set_cursor_pos(x-frame/2, y); cout << ' ';
                    set_cursor_pos(x+frame/2, y); cout << ' ';
                    set_cursor_pos(x, y-frame/2); cout << ' ';
                    set_cursor_pos(x, y+frame/2); cout << ' ';
                    break;
            }
            
            explosions[i].frame++;
            if (explosions[i].frame >= explosions[i].max_frames) {
                explosions[i].alive = false;
            }
            SET_COLOR(COLOR_WHITE);
        }
    }
}

// 绘制游戏信息
void draw_hud() {
    // 绘制血量条
    SET_COLOR(COLOR_RED);
    set_cursor_pos(2, SCREEN_HEIGHT-1);
    cout << "HP: [";
    int hp_bar = (player.hp * 20) / player.max_hp;
    for (int i = 0; i < 20; i++) {
        cout << (i < hp_bar ? '=' : ' ');
    }
    cout << "] " << player.hp << "/" << player.max_hp;
    
    // 绘制等级和经验
    SET_COLOR(COLOR_BLUE);
    set_cursor_pos(30, SCREEN_HEIGHT-1);
    cout << "LV: " << player.level << " EXP: " << player.exp << "/" << player.exp_next;
    
    // 绘制分数
    SET_COLOR(COLOR_YELLOW);
    set_cursor_pos(55, SCREEN_HEIGHT-1);
    cout << "SCORE: " << player.score;
    
    // 绘制波数和进度
    SET_COLOR(COLOR_GREEN);
    set_cursor_pos(2, SCREEN_HEIGHT-2);
    cout << "WAVE: " << wave << " [" << enemies_killed << "/" << enemies_needed << "]";
    
    SET_COLOR(COLOR_WHITE);
}

// 检测碰撞
bool check_collision(Point p1, Point p2, int size1 = 1, int size2 = 1) {
    return abs(p1.x - p2.x) <= size1 + size2 && abs(p1.y - p2.y) <= size1 + size2;
}

// 更新玩家状态
void update_player() {
    // 处理无敌状态
    if (player.invulnerable) {
        player.invul_time--;
        if (player.invul_time <= 0) {
            player.invulnerable = false;
        }
    }
    
    // 玩家移动控制
    if (GetAsyncKeyState(VK_LEFT) & 0x8000 && player.pos.x > 2) {
        player.pos.x--;
    }
    if (GetAsyncKeyState(VK_RIGHT) & 0x8000 && player.pos.x < SCREEN_WIDTH-3) {
        player.pos.x++;
    }
    if (GetAsyncKeyState(VK_UP) & 0x8000 && player.pos.y > SCREEN_HEIGHT/2) {
        player.pos.y--;
    }
    if (GetAsyncKeyState(VK_DOWN) & 0x8000 && player.pos.y < SCREEN_HEIGHT-3) {
        player.pos.y++;
    }
    
    // 空格键射击
    static int shoot_cooldown = 0;
    if (GetAsyncKeyState(VK_SPACE) & 0x8000 && shoot_cooldown <= 0) {
        fire_bullet(player.pos.x, player.pos.y-1, 1, 5 + player.level);
        shoot_cooldown = 10 - player.level/2; // 等级越高冷却越短
        if (shoot_cooldown < 3) shoot_cooldown = 3;
    }
    if (shoot_cooldown > 0) shoot_cooldown--;
}

// 更新敌机状态
void update_enemies() {
    for (int i = 0; i < MAX_ENEMIES; i++) {
        if (enemies[i].alive) {
            // 敌机移动(简单的左右摇摆+向下)
            enemies[i].pos.y += enemies[i].speed / 2;
            if (game_timer % (20 / enemies[i].speed) == 0) {
                if (rand() % 2) enemies[i].pos.x++;
                else enemies[i].pos.x--;
            }
            
            // 边界检测
            if (enemies[i].pos.x < 2) enemies[i].pos.x = 2;
            if (enemies[i].pos.x > SCREEN_WIDTH-3) enemies[i].pos.x = SCREEN_WIDTH-3;
            if (enemies[i].pos.y > SCREEN_HEIGHT-8) {
                // 敌机到达底部,玩家掉血
                if (!player.invulnerable) {
                    player.hp -= 10;
                    player.invulnerable = true;
                    player.invul_time = 60;
                    create_explosion(player.pos.x, player.pos.y);
                }
                enemies[i].alive = false;
                create_explosion(enemies[i].pos.x, enemies[i].pos.y);
                continue;
            }
            
            // 敌机射击
            enemies[i].shoot_timer--;
            if (enemies[i].shoot_timer <= 0) {
                fire_bullet(enemies[i].pos.x, enemies[i].pos.y+2, -1, 3 + wave/2);
                enemies[i].shoot_timer = rand() % (80 - wave * 5) + 40;
                if (enemies[i].shoot_timer < 20) enemies[i].shoot_timer = 20;
            }
            
            // 检测玩家子弹碰撞
            for (int j = 0; j < MAX_BULLETS; j++) {
                if (bullets[j].alive && bullets[j].dir == 1) {
                    if (check_collision(bullets[j].pos, enemies[i].pos, 0, 1)) {
                        // 敌机掉血
                        enemies[i].hp -= bullets[j].damage;
                        bullets[j].alive = false;
                        create_explosion(bullets[j].pos.x, bullets[j].pos.y);
                        
                        // 敌机死亡
                        if (enemies[i].hp <= 0) {
                            enemies[i].alive = false;
                            create_explosion(enemies[i].pos.x, enemies[i].pos.y);
                            enemies_killed++;
                            player.score += 100 * (enemies[i].type + 1);
                            player.exp += 20 * (enemies[i].type + 1);
                            
                            // 检查升级
                            if (player.exp >= player.exp_next) {
                                player.level++;
                                player.exp -= player.exp_next;
                                player.exp_next = player.level * 100;
                                player.max_hp += 20;
                                player.hp = player.max_hp;
                                create_explosion(player.pos.x, player.pos.y-3);
                                SET_COLOR(COLOR_CYAN);
                                set_cursor_pos(player.pos.x-5, player.pos.y-4);
                                cout << "LEVEL UP!";
                                SET_COLOR(COLOR_WHITE);
                            }
                        }
                        break;
                    }
                }
            }
            
            // 检测玩家碰撞
            if (check_collision(player.pos, enemies[i].pos, 1, 1) && !player.invulnerable) {
                player.hp -= 20;
                player.invulnerable = true;
                player.invul_time = 60;
                enemies[i].alive = false;
                create_explosion(enemies[i].pos.x, enemies[i].pos.y);
                create_explosion(player.pos.x, player.pos.y);
            }
        }
    }
    
    // 每波生成敌机
    if (enemies_killed < enemies_needed && game_timer % (60 - wave * 5) == 0) {
        spawn_enemy();
        if (wave >= 5) spawn_enemy(); // 高波数生成更多敌机
    }
    
    // 检查本波完成
    if (enemies_killed >= enemies_needed) {
        wave++;
        if (wave > 10) { // 10波后胜利
            game_win = true;
            game_over = true;
        } else {
            // 进入下一波
            init_enemies();
            enemies_needed = 10 + wave * 5;
            player.hp = min(player.hp + 30, player.max_hp); // 回血
            SET_COLOR(COLOR_GREEN);
            set_cursor_pos(SCREEN_WIDTH/2 - 5, SCREEN_HEIGHT/2);
            cout << "WAVE " << wave << "!";
            SET_COLOR(COLOR_WHITE);
            Sleep(1000);
        }
    }
}

// 更新子弹状态
void update_bullets() {
    for (int i = 0; i < MAX_BULLETS; i++) {
        if (bullets[i].alive) {
            // 移动子弹
            bullets[i].pos.y += bullets[i].dir * bullets[i].speed;
            
            // 边界检测
            if (bullets[i].pos.y < 0 || bullets[i].pos.y >= SCREEN_HEIGHT) {
                bullets[i].alive = false;
                continue;
            }
            
            // 检测敌机子弹击中玩家
            if (bullets[i].dir == -1 && check_collision(bullets[i].pos, player.pos, 0, 1) && !player.invulnerable) {
                player.hp -= bullets[i].damage;
                bullets[i].alive = false;
                player.invulnerable = true;
                player.invul_time = 60;
                create_explosion(bullets[i].pos.x, bullets[i].pos.y);
            }
        }
    }
}

// 更新游戏状态
void update_game() {
    game_timer++;
    
    // 更新所有游戏元素
    update_player();
    update_enemies();
    update_bullets();
    
    // 检查游戏结束
    if (player.hp <= 0) {
        game_over = true;
    }
}

// 绘制游戏画面
void draw_game() {
    clear_screen();          // 清屏
    draw_player();          // 绘制玩家
    draw_enemies();         // 绘制敌机
    draw_bullets();         // 绘制子弹
    draw_explosions();      // 绘制爆炸
    draw_hud();             // 绘制信息栏
}

// 游戏开始界面
void game_start() {
    system("cls");
    SET_COLOR(COLOR_YELLOW);
    cout << "==============================================" << endl;
    cout << "              太空战机生存战                  " << endl;
    cout << "==============================================" << endl;
    SET_COLOR(COLOR_WHITE);
    cout << "                游戏操作说明:                " << endl;
    cout << "          ← → 左右移动  ↑ ↓ 上下移动          " << endl;
    cout << "              空格键 发射子弹                 " << endl;
    cout << "              坚持10波即为胜利!              " << endl;
    cout << "                                              " << endl;
    SET_COLOR(COLOR_GREEN);
    cout << "                按任意键开始游戏              " << endl;
    SET_COLOR(COLOR_WHITE);
    _getch();
    
    // 初始化游戏
    srand(time(NULL));
    hide_cursor();
    init_player();
    init_enemies();
    init_bullets();
    init_explosions();
    
    // 设置控制台窗口大小
    system("mode con cols=80 lines=32");
}

// 游戏结束界面
void game_end() {
    clear_screen();
    if (game_win) {
        SET_COLOR(COLOR_GREEN);
        cout << "==============================================" << endl;
        cout << "                恭喜你!游戏胜利!            " << endl;
        cout << "==============================================" << endl;
    } else {
        SET_COLOR(COLOR_RED);
        cout << "==============================================" << endl;
        cout << "                  游戏结束                    " << endl;
        cout << "==============================================" << endl;
    }
    SET_COLOR(COLOR_WHITE);
    cout << "                最终得分:" << player.score << endl;
    cout << "                最高等级:" << player.level << endl;
    cout << "                坚持到第" << wave << "波" << endl;
    cout << "                                              " << endl;
    cout << "                按任意键退出                  " << endl;
    _getch();
}

// 主函数
int main() {
    game_start();
    
    // 游戏主循环
    while (!game_over) {
        update_game();
        draw_game();
        Sleep(30); // 控制帧率
    }
    
    game_end();
    return 0;
}