You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

大一学生求助:如何在C语言太空侵略者游戏中添加双发射击、射速提升Power Up?

实现太空侵略者的Power Up功能

1. 新增Power Up相关变量

在main函数开头添加以下定义,用于标记Power Up类型和记录玩家状态:

// Power Up相关定义
char powerUpDouble = 'D'; // 双发射击Power Up
char powerUpFast = 'F';   // 射速提升Power Up
int isDoubleFire = 0;     // 玩家是否开启双发射击
int doubleFireTimer = 0;  // 双发射击持续帧数(每帧50ms,120帧=6秒)
int laserCooldown = 3;    // 射击冷却帧数(替代原laserReady)
int baseCooldown = 3;     // 基础射击冷却,用于射速提升后恢复

同时删除原代码中的int laserReady = 1;,用上述laserCooldown替代。

2. 敌人被摧毁时生成Power Up

找到玩家激光击中敌人的逻辑块,在普通敌人被完全摧毁时添加随机掉落Power Up的代码:

if (world[y][x] == playerLaser && world[y-1][x] == enemy) {
    world[y][x] = ' ';
    world[y-1][x] = explosion;
    currentEnemies --;
    score = score + 50;
    
    // 15%概率随机掉落Power Up
    if (rand() % 100 < 15) {
        // 随机选择Power Up类型
        char powerType = (rand() % 2 == 0) ? powerUpDouble : powerUpFast;
        world[y-1][x] = powerType; // 在敌人位置生成Power Up
    }
}

若需要带盾敌人被击破护盾时也掉落,可在击中enemyShielded的逻辑块中添加相同的随机生成代码。

3. 添加Power Up移动与拾取逻辑

在激光处理逻辑之后,新增Power Up的移动和拾取判断,让Power Up向下移动,碰到玩家时触发效果:

// 处理Power Up移动与拾取
for (x = 0; x < sizex; x++) {
    // 从下往上遍历,避免同一帧内多次移动覆盖
    for (y = sizey - 2; y >= 0; y--) {
        if (world[y][x] == powerUpDouble || world[y][x] == powerUpFast) {
            // 下方是空的,继续下落
            if (world[y+1][x] == ' ') {
                world[y+1][x] = world[y][x];
                world[y][x] = ' ';
            }
            // 碰到玩家,触发对应效果
            else if (world[y+1][x] == player) {
                if (world[y][x] == powerUpDouble) {
                    isDoubleFire = 1;
                    doubleFireTimer = 120; // 持续6秒
                } else if (world[y][x] == powerUpFast) {
                    laserCooldown = 1; // 射速提升,冷却从3帧改为1帧
                }
                world[y][x] = ' '; // 移除已拾取的Power Up
            }
            // 落到屏幕底部,自动消失
            else if (y == sizey - 2 && world[y+1][x] != player) {
                world[y][x] = ' ';
            }
        }
    }
}

将这段代码放在/*laser time*/的两个循环之后。

4. 修改玩家射击逻辑,支持双发射击与射速调整

替换原玩家射击代码,支持双发射击,并使用新的冷却变量:
首先将原循环中的laserReady ++;替换为:

laserCooldown++;
// 更新双发射击定时器
if (isDoubleFire) {
    doubleFireTimer--;
    if (doubleFireTimer <= 0) {
        isDoubleFire = 0;
    }
}

然后修改玩家射击的判断代码:

if (keyPress == 'm' && laserCooldown >= baseCooldown) {
    for (x = 0; x < sizex; x++) {
        if (world[sizey-1][x] == player) {
            // 发射中心激光
            if (world[sizey-2][x] == ' ') {
                world[sizey-2][x] = playerLaser;
            }
            // 双发射击:左右各发射一枚激光
            if (isDoubleFire && x > 0 && world[sizey-2][x-1] == ' ') {
                world[sizey-2][x-1] = playerLaser;
            }
            if (isDoubleFire && x < sizex-1 && world[sizey-2][x+1] == ' ') {
                world[sizey-2][x+1] = playerLaser;
            }
            laserCooldown = 0; // 重置射击冷却
        }
    }
}

5. 测试与调整

  • 编译运行游戏,击毁敌人后会随机掉落D或F字符的Power Up
  • 拾取D后,玩家射击会同时发射3枚激光(中心+左右),持续6秒
  • 拾取F后,射击间隔会大幅缩短,可自行调整laserCooldown数值改变射速
  • 可修改随机概率(rand() % 100 < 15中的15)调整掉落频率,或修改定时器数值改变Power Up持续时间

内容的提问来源于stack exchange,提问作者Jeremy Ian Limson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 11:45:29