从零开发游戏需要学习的c#模块,第二十八章(血条显示 —— 敌人与玩家生命可视化)

本节课学习内容

  1. 敌人头顶显示红色血条

  2. 屏幕左上角显示玩家图形化血条

  3. 血条长度和宽度随血量变化

  4. 史莱姆、骷髅、蝙蝠各有不同颜色血条


完整代码

需要改动两个文件:Enemy.csGame1.cs


第一步:给 Enemy.cs 添加绘制血条的方法

Enemy 类的末尾添加以下方法:

csharp

复制代码
// ★ 绘制敌人血条
public virtual void DrawHealthBar(SpriteBatch spriteBatch)
{
    if (!IsAlive) return;

    int barWidth = 30;
    int barHeight = 4;
    int barY = (int)(Position.Y - texture.Height / 2 - 10);

    // 背景(黑色)
    Rectangle bgRect = new Rectangle(
        (int)Position.X - barWidth / 2,
        barY,
        barWidth,
        barHeight
    );

    // 血量(根据剩余比例计算宽度)
    float healthPercent = (float)Hp / MaxHp;
    int currentBarWidth = (int)(barWidth * healthPercent);
    Rectangle healthRect = new Rectangle(
        (int)Position.X - barWidth / 2,
        barY,
        currentBarWidth,
        barHeight
    );

    // 创建临时纹理(简单方法)
    Texture2D pixel = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
    pixel.SetData(new[] { Color.White });

    spriteBatch.Draw(pixel, bgRect, Color.Black);
    spriteBatch.Draw(pixel, healthRect, color); // 使用敌人自身颜色
}

第二步:改造 Game1.cs 的血量显示

Game1.cs 里的 UI 部分从文字血条改成图形血条。以下是完整的 Game1.cs

为了节省篇幅,这里只贴改动过的部分。用这些方法替换你现有文件中的对应方法。

1. 添加绘制血条的方法:

cs 复制代码
// ★ 画玩家图形血条
private void DrawPlayerHealthBar(SpriteBatch spriteBatch, int x, int y, int width, int height)
{
    float healthPercent = (float)player.Hp / player.MaxHp;

    // 背景
    Texture2D pixel = new Texture2D(GraphicsDevice, 1, 1);
    pixel.SetData(new[] { Color.White });
    spriteBatch.Draw(pixel, new Rectangle(x, y, width, height), Color.DarkGray);

    // 血量颜色:绿 → 黄 → 红
    Color barColor;
    if (healthPercent > 0.5f)
        barColor = Color.LimeGreen;
    else if (healthPercent > 0.25f)
        barColor = Color.Yellow;
    else
        barColor = Color.Red;

    // 当前血量
    int currentWidth = (int)(width * healthPercent);
    spriteBatch.Draw(pixel, new Rectangle(x, y, currentWidth, height), barColor);

    // 边框
    spriteBatch.Draw(pixel, new Rectangle(x, y, width, 1), Color.White);
    spriteBatch.Draw(pixel, new Rectangle(x, y + height - 1, width, 1), Color.White);
    spriteBatch.Draw(pixel, new Rectangle(x, y, 1, height), Color.White);
    spriteBatch.Draw(pixel, new Rectangle(x + width - 1, y, 1, height), Color.White);
}

2. 替换 DrawGameUI 方法:

cs 复制代码
private void DrawGameUI()
{
    // 分数文字
    _spriteBatch.DrawString(font, $"分数:{score}", new Vector2(10, 10), Color.White);
    _spriteBatch.DrawString(font, $"最高分:{saveData.HighScore}", new Vector2(10, 35), Color.Gold);

    // ★ 图形血条
    DrawPlayerHealthBar(_spriteBatch, 10, 65, 200, 20);
    _spriteBatch.DrawString(font, $"{player.Hp}/{player.MaxHp}", new Vector2(220, 63), Color.White);

    _spriteBatch.DrawString(font, $"敌人:{enemies.Count} | 子弹:{bullets.Count}",
        new Vector2(10, 95), Color.Yellow);
    _spriteBatch.DrawString(font, "WASD移动 | 鼠标瞄准左键射击 | M静音",
        new Vector2(10, 570), Color.LightGray);
}

3. 在 DrawGameWorld 里给敌人画血条:

DrawGameWorld 方法中,敌人绘制那一行后面加上血条:

cs 复制代码
private void DrawGameWorld()
{
    tileMap.Draw(_spriteBatch);

    foreach (Vector2 coinPos in coins)
        _spriteBatch.Draw(coinTexture, coinPos, null, Color.White,
            0f, new Vector2(12, 12), 1f, SpriteEffects.None, 0f);

    foreach (Enemy enemy in enemies)
    {
        enemy.Draw(_spriteBatch);
        enemy.DrawHealthBar(_spriteBatch);  // ★ 画血条
    }

    foreach (Bullet bullet in bullets)
        bullet.Draw(_spriteBatch);

    particleSystem.Draw(_spriteBatch);
    player.Draw(_spriteBatch);
}

本节课学习到此结束,我是魔法阵维护师,关注我,下期更精彩!

相关推荐
2501_936415692 小时前
可变参数&综合练习&斗地主游戏
java·windows·游戏
MartinYeung52 小时前
[论文学习]自主性如何重塑个性化对LLM智能体隐私关切与信任的影响
人工智能·学习
FellAveal3 小时前
【Go语言入门学习笔记】Part18.工作池与WaitGroup(也即协程池)
笔记·学习·golang
菜鸟‍4 小时前
【论文学习】MICCAI 2024 || SGSeg:通过自引导机制实现胸部X光片语言引导分割的无文本推理
人工智能·深度学习·学习
lazy H4 小时前
Git clone 怎么用?克隆项目及常见问题完整教程
大数据·git·后端·学习·搜索引擎·github
菜鸟‍4 小时前
【论文学习】arxiv 2024 || RoSIS:基于鲁棒框架重新审视文本提示式手术器械分割
人工智能·学习·计算机视觉
颜x小4 小时前
[C#] C++与c#语法对比
开发语言·c++·c#
weixin_423652135 小时前
C#使用JObject
开发语言·c#
笨鸟先飞的橘猫5 小时前
只会脚本语言的游戏后端自我提升之路——c++学习(开篇)
学习·游戏
Z5998178416 小时前
c#软件开发学习笔记--Modbus-RTU通讯
笔记·学习·c#