首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

XNA Game Studio Express 開發教學(6):實作護盾特效與碰撞邏輯

     編輯:關於C語言

繪製護盾 Sprite

在 Ship.Render 中加入護盾繪製邏輯:

if (inShield && evolved)
{
    var shieldTex = SpacewarGame.ContentManager.Load<Texture2D>(
        SpacewarGame.Settings.MediaPath + @"textures\circle");
    batch.Begin(SpriteBlendMode.AlphaBlend);
    // 3D 座標投影至 2D 螢幕空間
    Vector3 loc = device.Viewport.Project(position,
        SpacewarGame.Camera.Projection, SpacewarGame.Camera.View, Matrix.Identity);
    batch.Draw(shieldTex,
        new Vector2(loc.X - shieldTex.Width / 2f, loc.Y - shieldTex.Height / 2f),
        new Rectangle(0, 0, shieldTex.Width, shieldTex.Height),
        new Color(new Vector4(0f, 1f, 0f, 0.5f)),  // 半透明綠
        0f,
        new Vector2(shieldTex.Width / 2f, shieldTex.Height / 2f),
        (3f * (float)Math.Abs(Math.Cos(exitShieldTime))) + 2f, // 脈動縮放
        SpriteEffects.None, 0f);
    batch.End();
}

關鍵參數解析

  • Viewport.Project:將 3D 世界座標轉為 2D 螢幕座標。
  • Vector4(0,1,0,0.5):RGBA,Alpha=0.5 達成半透明。
  • Cos(exitShieldTime):隨時間脈動縮放,增強視覺效果。
  • 層深 0f:確保繪製在飛船之上。

碰撞邏輯整合

修改 SpacewarScreen.cs 的 HitPlayer1/2:

// HitPlayer2 開頭
if (ship2.Shield)
{
    Sound.PlayCue(Sounds.WeaponPickup); // 彈跳音效
    return; // 忽略傷害
}
// HitPlayer1 同理

遊戲體驗

  • 每回合每玩家可用一次護盾,持續 5 秒。
  • 可吸收所有子彈、小行星撞擊、甚至太陽傷害。
  • 進階想法:修改為反射子彈、或護盾期間傷害加倍。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved