A Good Game Or A Good Way Of Killing Your Poor Computer

#include <windows.h>
#include <conio.h>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
const int WIDTH = 100;
const int HEIGHT = 25;
const int GROUND_LEVEL = HEIGHT - 3;
const int MAX_OBJECTS = 1000;
int GAME_SPEED = 50;
int DIFFICULTY = 1;
enum ObjectType {
    PLAYER_CHAR = 'T',
    PLAYER_JUMP = '^',
    GROUND_CHAR = '=',
    OBSTACLE = '#',
    ENEMY_SOLDIER = 'S',
    ENEMY_TANK = 'T',
    ENEMY_GUNNER = 'G',
    BULLET = '*',
    MISSILE = '>',
    PARTICLE = '+',
    WALL = '|',
    DAMAGED_WALL = ':',
    CRATER = 'O',
    HEALTH = 'H',
    AMMO = 'A',
    SHIELD = '@'
};
struct GameObject {
    int x, y;
    int health, damage;
    int speed, direction;
    ObjectType type;
    bool active;
};
struct Player {
    int x = 5, y = GROUND_LEVEL;
    int health = 100, ammo = 30, score = 0;
    int jumpHeight = 0;
    bool isJumping = false, isFalling = false;
    bool isShooting = false, hasShield = false;
    int shieldTimer = 0;
    char weapon = 'p'; 
} player;
GameObject enemies[MAX_OBJECTS];
int enemyCount = 0;
GameObject bullets[MAX_OBJECTS];
int bulletCount = 0;
GameObject particles[MAX_OBJECTS];
int particleCount = 0;
GameObject walls[MAX_OBJECTS];
int wallCount = 0;
GameObject powerups[MAX_OBJECTS];
int powerupCount = 0;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR_INFO consoleBuffer[HEIGHT][WIDTH];
COORD bufferSize = { WIDTH, HEIGHT };
COORD bufferCoord = { 0, 0 };
SMALL_RECT writeRegion = { 0, 0, WIDTH - 1, HEIGHT - 1 };
void disableCursorBlink() {
    CONSOLE_CURSOR_INFO cursorInfo;
    GetConsoleCursorInfo(hConsole, &cursorInfo);
    cursorInfo.bVisible = false;
    SetConsoleCursorInfo(hConsole, &cursorInfo);
}
void initGame() {
    enemyCount = bulletCount = particleCount = powerupCount = 0;
    wallCount = 0;
    GAME_SPEED = 50;
    DIFFICULTY = 1;
    for (int i = 0; i < 10; i++) {
        int wallHeight = rand() % 3 + 2;
        int wallX = rand() % 60 + 20;
        for (int j = 0; j < wallHeight; j++) {
            walls[wallCount++] = { wallX, GROUND_LEVEL - j, 10, 0, 0, 0, WALL, true };
        }
    }
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            consoleBuffer[y][x].Char.AsciiChar = ' ';
            consoleBuffer[y][x].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
        }
    }
}
void createExplosion(int x, int y, int size) {
    for (int i = 0; i < size * 5; i++) {
        int px = x + rand() % (size * 2 + 1) - size;
        int py = y + rand() % (size * 2 + 1) - size;
        particles[particleCount++] = { px, py, 0, 0, rand() % 5 - 2, rand() % 5 - 2, PARTICLE, true };
    }
}
void playerShoot() {
    if (player.ammo <= 0) return;
    player.ammo--;
    player.isShooting = true;
    GameObject bullet;
    bullet.x = player.x + 2;
    bullet.y = player.y;
    bullet.direction = 1;
    bullet.active = true;
    switch (player.weapon) {
        case 'p': 
            bullet.type = BULLET;
            bullet.speed = 5;
            bullet.damage = 10;
            bullets[bulletCount++] = bullet;
            break;
        case 'r':
            bullet.type = BULLET;
            bullet.speed = 7;
            bullet.damage = 15;
            bullets[bulletCount++] = bullet;
            break;
        case 'm': 
            bullet.type = MISSILE;
            bullet.speed = 3;
            bullet.damage = 30;
            bullets[bulletCount++] = bullet;
            player.ammo -= 2;
            break;
        case 'l':
            for (int i = 0; i < 3; i++) {
                bullet.y = player.y - 1 + i;
                bullet.type = PARTICLE;
                bullet.speed = 10;
                bullet.damage = 5;
                bullets[bulletCount++] = bullet;
            }
            player.ammo -= 3;
            break;
    }
}
void spawnEnemy() {
    if (enemyCount >= MAX_OBJECTS) return;
    
    GameObject enemy;
    enemy.x = WIDTH - 5;
    enemy.y = GROUND_LEVEL;
    enemy.direction = -1;
    enemy.active = true;
    
    int enemyType = rand() % 100;
    if (enemyType < 50) { 
        enemy.type = ENEMY_SOLDIER;
        enemy.speed = 1 + DIFFICULTY / 2;
        enemy.health = 20;
        enemy.damage = 10;
    } else if (enemyType < 80) { 
        enemy.type = ENEMY_GUNNER;
        enemy.speed = 1;
        enemy.health = 30;
        enemy.damage = 20;
    } else {
        enemy.type = ENEMY_TANK;
        enemy.speed = 1;
        enemy.health = 100;
        enemy.damage = 40;
    }
    
    enemies[enemyCount++] = enemy;
}
void spawnPowerup() {
    if (powerupCount >= MAX_OBJECTS || rand() % 100 > 15) return;
    
    GameObject powerup;
    powerup.x = WIDTH - 5;
    powerup.y = rand() % (GROUND_LEVEL - 5) + 3;
    powerup.speed = 1;
    powerup.active = true;
    
    int type = rand() % 100;
    if (type < 40) {
        powerup.type = HEALTH;
    } else if (type < 70) {
        powerup.type = AMMO;
    } else if (type < 90) {
        powerup.type = SHIELD;
    } else {
        char weapons[] = { 'p', 'r', 'm', 'l' };
        powerup.type = static_cast<ObjectType>(weapons[min(DIFFICULTY - 1, 3)]);
    }
    
    powerups[powerupCount++] = powerup;
}

// 敌人AI
void enemyAI() {
    for (int i = 0; i < enemyCount; i++) {
        GameObject& enemy = enemies[i];
        if (!enemy.active) continue;
        if (rand() % 100 < 5 + DIFFICULTY * 2 && enemy.x < WIDTH - 10) {
            if (bulletCount < MAX_OBJECTS) {
                GameObject bullet;
                bullet.x = enemy.x - 1;
                bullet.y = enemy.y;
                bullet.type = BULLET;
                bullet.speed = 3 + DIFFICULTY / 2;
                bullet.damage = enemy.damage;
                bullet.direction = -1;
                bullet.active = true;
                bullets[bulletCount++] = bullet;
                if (enemy.type == ENEMY_TANK && rand() % 100 < 30) {
                    bullet.type = MISSILE;
                    bullet.damage = 30;
                    bullets[bulletCount++] = bullet;
                }
            }
        }
        enemy.x -= enemy.speed;
    }
}
void updateGame() {
    if (player.hasShield) {
        player.shieldTimer--;
        if (player.shieldTimer <= 0) {
            player.hasShield = false;
        }
    }
    player.isShooting = false;
    for (int i = 0; i < bulletCount; i++) {
        GameObject& bullet = bullets[i];
        if (!bullet.active) continue;
        bullet.x += bullet.speed * bullet.direction;
        for (int j = 0; j < wallCount; j++) {
            GameObject& wall = walls[j];
            if (!wall.active) continue;
            if (abs(bullet.x - wall.x) < 1 && abs(bullet.y - wall.y) < 1) {
                wall.health -= bullet.damage;
                if (wall.health <= 0) {
                    wall.active = false;
                    createExplosion(wall.x, wall.y, 2);
                    player.score += 5;
                } else if (wall.health < 5) {
                    wall.type = DAMAGED_WALL;
                }
                bullet.active = false;
                break;
            }
        }
        for (int j = 0; j < enemyCount; j++) {
            GameObject& enemy = enemies[j];
            if (!enemy.active) continue;
            
            if (abs(bullet.x - enemy.x) < 2 && abs(bullet.y - enemy.y) < 1) {
                enemy.health -= bullet.damage;
                if (enemy.health <= 0) {
                    enemy.active = false;
                    createExplosion(enemy.x, enemy.y, 3);
                    if (enemy.type == ENEMY_SOLDIER) player.score += 10;
                    else if (enemy.type == ENEMY_GUNNER) player.score += 20;
                    else player.score += 50;
                }
                bullet.active = false;
                break;
            }
        }
        if (bullet.direction == -1 && abs(bullet.x - player.x) < 1 && abs(bullet.y - player.y) < 1) {
            if (!player.hasShield) {
                player.health -= bullet.damage;
            }
            bullet.active = false;
            createExplosion(player.x, player.y, 2);
        }
    }
    for (int i = 0; i < particleCount; i++) {
        GameObject& p = particles[i];
        if (!p.active) continue;
        p.x += p.speed;
        p.y += p.direction;
    }
    for (int i = 0; i < powerupCount; i++) {
        GameObject& powerup = powerups[i];
        if (!powerup.active) continue;
        powerup.x -= powerup.speed;
        if (abs(powerup.x - player.x) < 2 && abs(powerup.y - player.y) < 2) {
            switch (powerup.type) {
                case HEALTH:
                    player.health = min(100, player.health + 30);
                    break;
                case AMMO:
                    player.ammo += 20;
                    break;
                case SHIELD:
                    player.hasShield = true;
                    player.shieldTimer = 200;
                    break;
                default: 
                    player.weapon = static_cast<char>(powerup.type);
                    break;
            }
            powerup.active = false;
            player.score += 10;
        }
    }
    if (rand() % 100 < 15 + DIFFICULTY * 3) {
        spawnEnemy();
    }
    spawnPowerup();
    enemyAI();
    if (player.score > DIFFICULTY * 500) {
        DIFFICULTY++;
        GAME_SPEED = max(20, GAME_SPEED - 5);
    }
}
void updatePlayer() {
    if (player.isJumping) {
        player.y--;
        player.jumpHeight++;
        if (player.jumpHeight > 5) {
            player.isJumping = false;
            player.isFalling = true;
        }
    }
    else if (player.isFalling) {
        player.y++;
        player.jumpHeight--;
        if (player.y >= GROUND_LEVEL) {
            player.y = GROUND_LEVEL;
            player.isFalling = false;
            player.jumpHeight = 0;
        }
    }
    if (!player.isJumping && !player.isFalling && player.y < GROUND_LEVEL) {
        player.isFalling = true;
    }
    player.y = max(1, min(HEIGHT - 2, player.y));
}

// 绘制游戏界面(使用双缓冲技术)
void draw() {
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            consoleBuffer[y][x].Char.AsciiChar = ' ';
        }
    }
    for (int x = 0; x < WIDTH; x++) {
        consoleBuffer[GROUND_LEVEL + 1][x].Char.AsciiChar = GROUND_CHAR;
    }
    for (int i = 0; i < wallCount; i++) {
        GameObject& wall = walls[i];
        if (wall.active) {
            consoleBuffer[wall.y][wall.x].Char.AsciiChar = wall.type;
        }
    }
    for (int i = 0; i < particleCount; i++) {
        GameObject& p = particles[i];
        if (p.active && p.x >= 0 && p.x < WIDTH && p.y >= 0 && p.y < HEIGHT) {
            consoleBuffer[p.y][p.x].Char.AsciiChar = p.type;
        }
    }
    for (int i = 0; i < powerupCount; i++) {
        GameObject& powerup = powerups[i];
        if (powerup.active && powerup.x >= 0 && powerup.x < WIDTH && powerup.y >= 0 && powerup.y < HEIGHT) {
            consoleBuffer[powerup.y][powerup.x].Char.AsciiChar = powerup.type;
        }
    }
    for (int i = 0; i < enemyCount; i++) {
        GameObject& enemy = enemies[i];
        if (enemy.active && enemy.x >= 0 && enemy.x < WIDTH && enemy.y >= 0 && enemy.y < HEIGHT) {
            consoleBuffer[enemy.y][enemy.x].Char.AsciiChar = enemy.type;
        }
    }
    for (int i = 0; i < bulletCount; i++) {
        GameObject& bullet = bullets[i];
        if (bullet.active && bullet.x >= 0 && bullet.x < WIDTH && bullet.y >= 0 && bullet.y < HEIGHT) {
            consoleBuffer[bullet.y][bullet.x].Char.AsciiChar = bullet.type;
        }
    }
    if (player.y >= 0 && player.y < HEIGHT && player.x >= 0 && player.x < WIDTH) {
        consoleBuffer[player.y][player.x].Char.AsciiChar = 
            player.isJumping ? PLAYER_JUMP : PLAYER_CHAR;
        if (player.hasShield) {
            consoleBuffer[player.y][player.x].Char.AsciiChar = SHIELD;
        }
        if (player.isShooting && player.x + 1 < WIDTH) {
            consoleBuffer[player.y][player.x + 1].Char.AsciiChar = 
                (player.weapon == 'l') ? '|' : '-';
        }
    }
    WriteConsoleOutput(hConsole, (CHAR_INFO*)consoleBuffer, bufferSize, bufferCoord, &writeRegion);
    COORD pos = { 0, HEIGHT };
    SetConsoleCursorPosition(hConsole, pos);
    printf("SCORE: %d HEALTH: %d AMMO: %d WEAPON: ", player.score, player.health, player.ammo);
    switch (player.weapon) {
        case 'p': printf("PISTOL"); break;
        case 'r': printf("RIFLE"); break;
        case 'm': printf("MISSILE"); break;
        case 'l': printf("LASER"); break;
    }
    printf(" DIFFICULTY: %d", DIFFICULTY);
    if (player.hasShield) printf(" SHIELD: %d", player.shieldTimer / 20);
    printf(" ");
    if (player.health <= 0) {
        COORD endPos = { 0, HEIGHT + 1 };
        SetConsoleCursorPosition(hConsole, endPos);
        printf("GAME OVER! Press 'r' to restart");
    }
}
void runGame() {
    srand(static_cast<unsigned int>(time(nullptr)));
    initGame();
    disableCursorBlink();
    
    while (true) {
        if (player.health > 0) {
            if (_kbhit()) {
                char key = _getch();
                switch (key) {
                    case ' ': 
                        if (!player.isJumping && !player.isFalling && player.y == GROUND_LEVEL) {
                            player.isJumping = true;
                        }
                        break;
                    case 'f': // 射击
                        playerShoot();
                        break;
                    case 'w': // 上移
                        player.y = max(1, player.y - 1);
                        break;
                    case 's': // 下移
                        player.y = min(HEIGHT - 2, player.y + 1);
                        break;
                    case '1': // 武器切换
                        player.weapon = 'p';
                        break;
                    case '2':
                        player.weapon = 'r';
                        break;
                    case '3':
                        player.weapon = 'm';
                        break;
                    case '4':
                        player.weapon = 'l';
                        break;
                    case 'q':
                        exit(0);
                }
            }
            updatePlayer();
            updateGame();
        } else {
            if (_kbhit()) {
                char key = _getch();
                if (key == 'r') initGame();
                if (key == 'q') exit(0);
            }
        }
        draw();
        Sleep(GAME_SPEED);
    }
}

int main() {
    system(("mode con cols=" + to_string(WIDTH) + " lines=" + to_string(HEIGHT + 3)).c_str());
    runGame();
    return 0;
}

一个

激进

的跑酷战斗游戏

电脑性能不强的电脑爆掉

食用方法:

控制方式

  1. 移动控制

    • 空格键:跳跃
    • W:向上移动
    • S:向下移动
  2. 武器控制

    • F:射击
    • 1:切换到手枪
    • 2:切换到步枪
    • 3:切换到导弹
    • 4:切换到激光
  3. 游戏控制

    • R:重新开始游戏
    • Q:退出游戏

游戏元素

  1. 玩家角色T(站立时)或(跳跃时)
  2. 敌人
    • 士兵:S
    • 坦克:
    • 炮手:G
  3. 武器效果
    • 子弹:
    • 导弹:
    • 激光:|
  4. 环境
    • 墙壁:(完整)或(受损)
    • 地面:=
    • 弹坑:
  5. 能量道具
    • 生命:+
    • 弹药:
    • 护盾:

游戏策略

  1. 优先收集能量道具保持战斗力
  2. 使用不同武器应对不同敌人:
    • 手枪对付普通士兵
    • 步枪对付炮手
    • 导弹和激光对付坦克
  3. 利用墙壁作为掩体躲避子弹
  4. 注意护盾剩余时间(显示在HUD上)

难度系统

游戏会随着得分增加而变难:

  • 敌人更频繁出现
  • 敌人移动速度更快
  • 敌人攻击更猛烈
  • 每500分提升一个难度等级
posted @ 2025-06-08 11:12  lee_liang  阅读(8)  评论(0)    收藏  举报