成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

游戲人生Silverlight:星際競技場

開發 后端
使用 Silverlight 2.0(c#, Farseer Physics Engine) 開發一個射擊游戲:星際競技場。思路:使用一個開源的 Silverlight 物理引擎:Farseer Physics Engine.....

介紹

使用 Silverlight 2.0(c#, Farseer Physics Engine) 開發一個射擊游戲:星際競技場

玩法

W 或者 ↑ = 前進;S 或者 ↓ = 后退:A 或者 ← = 左轉;D 或者 → = 右轉;J 或者 Ctrl = 開火

思路

1、使用一個開源的 Silverlight 物理引擎:Farseer Physics Engine

2、將 Farseer Physics Engine 中的物理運算器 PhysicsSimulator 放到一個全局變量中,對 Body 和 Geom 做即時運算,

3、寫個 IPhysicsControl 接口,用于描述物理對象的各個屬性,需要運動和碰撞的對象,要實現該接口抽象出來的各個屬性

4、寫個抽象類(Sprite),在其內封裝好物理引擎。各種類型的物理對象的模擬器,都需要重寫該抽象類的兩個方法GetForce()和GetTorque()即可,其分別要返回對象在當前時刻所受到的牽引力和力矩

5、寫個 IFire 接口,所有可開火的對象都要實現該接口

6、寫個控件 PhysicsBox,用于包裝 IPhysicsControl,從而將模擬器計算出的運動和碰撞結果呈現到界面上

關鍵代碼Sprite.cs(Sprite 模擬器的基類)

  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11.  
  12. using FarseerGames.FarseerPhysics;  
  13. using FarseerGames.FarseerPhysics.Mathematics;  
  14. using FarseerGames.FarseerPhysics.Dynamics;  
  15. using FarseerGames.FarseerPhysics.Collisions;  
  16.  
  17. namespace YYArena.Core  
  18. {  
  19.     /**//// <summary> 
  20.     /// Sprite 基類  
  21.     /// </summary> 
  22.     public abstract class Sprite  
  23.     {  
  24.         private PhysicsSimulator _physicsSimulator;  
  25.  
  26.         protected PhysicsBox playerBox;  
  27.         protected Geom playerGeometry;  
  28.  
  29.         /**//// <summary> 
  30.         /// 構造函數  
  31.         /// </summary> 
  32.         /// <param name="physicsSimulator">PhysicsSimulator</param> 
  33.         /// <param name="physicsControl">IPhysicsControl</param> 
  34.         /// <param name="position">初始位置</param> 
  35.         /// <param name="angle">初始轉角</param> 
  36.         /// <param name="originalVelocity">初始速度</param> 
  37.         public Sprite(PhysicsSimulator physicsSimulator,  
  38.             IPhysicsControl physicsControl, Vector2 position, float angle, float originalVelocity)  
  39.         {  
  40.             _physicsSimulator = physicsSimulator;  
  41.  
  42.             playerBox = new PhysicsBox(physicsControl);  
  43.             playerBox.Body.Position = position;  
  44.             playerBox.Body.Rotation = (float)Helper.Angle2Radian(angle);  
  45.             playerBox.Body.LinearVelocity = Helper.Convert2Vector(originalVelocity, (float)Helper.Angle2Radian(angle));  
  46.  
  47.             // Body 和 Geom 的 Tag 保存為 Sprite,方便引用  
  48.             playerBox.Body.Tag = this;  
  49.             playerBox.Geom.Tag = this;  
  50.  
  51.             playerBox.Update();  
  52.         }  
  53.  
  54.         /**//// <summary> 
  55.         /// 即時計算力和力矩  
  56.         /// </summary> 
  57.         void CompositionTarget_Rendering(object sender, EventArgs e)  
  58.         {  
  59.             if (Enabled)  
  60.             {  
  61.                 var force = GetForce();  
  62.                 var torque = GetTorque();  
  63.  
  64.                 playerBox.Body.ApplyForce(force);  
  65.                 playerBox.Body.ApplyTorque(torque);  
  66.  
  67.                 playerBox.Update();  
  68.             }  
  69.         }  
  70.  
  71.         /**//// <summary> 
  72.         /// 返回 Sprite 當前受的力  
  73.         /// </summary> 
  74.         protected abstract Vector2 GetForce();  
  75.         /**//// <summary> 
  76.         /// 返回 Sprite 當前受的力矩  
  77.         /// </summary> 
  78.         protected abstract float GetTorque();  
  79.  
  80.         public PhysicsBox PhysicsBox  
  81.         {  
  82.             get { return playerBox; }  
  83.         }  
  84.  
  85.         private bool _enabled = false;  
  86.         /**//// <summary> 
  87.         /// 是否啟用此 Sprite  
  88.         /// </summary> 
  89.         public bool Enabled  
  90.         {  
  91.             get { return _enabled; }  
  92.             set  
  93.             {   
  94.                 _enabled = value;  
  95.  
  96.                 if (value)  
  97.                 {  
  98.                     CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);  
  99.  
  100.                     _physicsSimulator.Add(playerBox.Body);  
  101.                     _physicsSimulator.Add(playerBox.Geom);  
  102.                 }  
  103.                 else  
  104.                 {  
  105.                     CompositionTarget.Rendering -new EventHandler(CompositionTarget_Rendering);  
  106.  
  107.                     GC.SuppressFinalize(this);  
  108.                     _physicsSimulator.Remove(playerBox.Body);  
  109.                     _physicsSimulator.Remove(playerBox.Geom);  
  110.                 }  
  111.             }  
  112.         }  
  113.     }  

#p#

PlayerSprite.cs(玩家 Sprite 模擬器)

  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11.  
  12. using System.Collections.Generic;  
  13. using FarseerGames.FarseerPhysics.Mathematics;  
  14. using FarseerGames.FarseerPhysics;  
  15. using FarseerGames.FarseerPhysics.Collisions;  
  16.  
  17. namespace YYArena.Core  
  18. {  
  19.     /**//// <summary> 
  20.     /// 玩家 Sprite  
  21.     /// </summary> 
  22.     public class PlayerSprite : Sprite, IFire  
  23.     {  
  24.         private List<Key> _upKeys { get; set; }  
  25.         private List<Key> _downKeys { get; set; }  
  26.         private List<Key> _leftKeys { get; set; }  
  27.         private List<Key> _rightKeys { get; set; }  
  28.         private List<Key> _fireKeys { get; set; }  
  29.  
  30.         private KeyboardHandler _keyHandler;  
  31.         private IPhysicsControl _physicsControl;  
  32.  
  33.         /**//// <summary> 
  34.         /// 構造函數  
  35.         /// </summary> 
  36.         /// <param name="physicsSimulator">PhysicsSimulator</param> 
  37.         /// <param name="physicsControl">IPhysicsControl</param> 
  38.         /// <param name="position">初始位置</param> 
  39.         /// <param name="angle">初始轉角</param> 
  40.         /// <param name="originalVelocity">初始速度</param> 
  41.         /// <param name="keyboardHandler">KeyboardHandler</param> 
  42.         /// <param name="up">操作玩家向前移動的按鍵集合</param> 
  43.         /// <param name="down">操作玩家向后移動的按鍵集合</param> 
  44.         /// <param name="left">操作玩家向左轉動的按鍵集合</param> 
  45.         /// <param name="right">操作玩家向右轉動的按鍵集合</param> 
  46.         /// <param name="fire">操作玩家開火的按鍵集合</param> 
  47.         public PlayerSprite(PhysicsSimulator physicsSimulator,  
  48.             IPhysicsControl physicsControl, Vector2 position, float angle, float originalVelocity,  
  49.             KeyboardHandler keyboardHandler,  
  50.             List<Key> up, List<Key> down, List<Key> left, List<Key> right, List<Key> fire)  
  51.             : base(physicsSimulator, physicsControl, position, angle, originalVelocity)  
  52.         {  
  53.             PrevFireDateTime = DateTime.MinValue;  
  54.             MinFireInterval = 500d;  
  55.  
  56.             _upKeys = up;  
  57.             _downKeys = down;  
  58.             _leftKeys = left;  
  59.             _rightKeys = right;  
  60.             _fireKeys = fire;  
  61.  
  62.             _keyHandler = keyboardHandler;  
  63.             _physicsControl = physicsControl;  
  64.  
  65.             CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);  
  66.         }  
  67.  
  68.         void CompositionTarget_Rendering(object sender, EventArgs e)  
  69.         {  
  70.             if (Enabled)  
  71.             {  
  72.                 // 如果按了開火鍵,是否可開火  
  73.                 if (_keyHandler.AnyKeyPressed(_fireKeys) && (DateTime.Now - PrevFireDateTime).TotalMilliseconds > MinFireInterval)  
  74.                 {  
  75.                     PrevFireDateTime = DateTime.Now;  
  76.                     if (Fire != null)  
  77.                         Fire(this, EventArgs.Empty);  
  78.                 }  
  79.             }  
  80.         }  
  81.  
  82.         public DateTime PrevFireDateTime { get; set; }  
  83.  
  84.         public double MinFireInterval { get; set; }  
  85.  
  86.         public event EventHandler<EventArgs> Fire;  
  87.  
  88.         protected override Vector2 GetForce()  
  89.         {  
  90.             Vector2 force = Vector2.Zero;  
  91.  
  92.             if (_keyHandler.AnyKeyPressed(_upKeys))  
  93.                 force += Helper.Convert2Vector(_physicsControl.ForceAmount, playerBox.Body.Rotation);  
  94.             if (_keyHandler.AnyKeyPressed(_downKeys))  
  95.                 force += Helper.Convert2Vector(_physicsControl.ForceAmount, playerBox.Body.Rotation - Helper.Angle2Radian(180));  
  96.  
  97.             // 最大線性速度限制  
  98.             if (playerBox.Body.LinearVelocity.Length() > _physicsControl.MaxLinearVelocity)  
  99.                 force = Vector2.Zero;  
  100.  
  101.             return force;  
  102.         }  
  103.  
  104.         protected override float GetTorque()  
  105.         {  
  106.             float torque = 0;  
  107.  
  108.             if (_keyHandler.AnyKeyPressed(_leftKeys))  
  109.                 torque -_physicsControl.TorqueAmount;  
  110.             if (_keyHandler.AnyKeyPressed(_rightKeys))  
  111.                 torque += _physicsControl.TorqueAmount;  
  112.  
  113.             // 用于修正 RotationalDragCoefficient (在沒有任何 Torque 的情況下,如果轉速小于 1.3 則設其為 0)  
  114.             // 如果不做此修正的話,轉速小于 1.3 后還會轉好長時間  
  115.             if (!_keyHandler.AnyKeyPressed(_leftKeys) && !_keyHandler.AnyKeyPressed(_rightKeys) && Math.Abs(playerBox.Body.AngularVelocity) < 1.3)  
  116.                 playerBox.Body.AngularVelocity = 0;  
  117.  
  118.             // 最大轉速限制  
  119.             if (Math.Abs(playerBox.Body.AngularVelocity) > _physicsControl.MaxAngularVelocity)  
  120.                 torque = 0;  
  121.  
  122.             return torque;  
  123.         }  
  124.     }  

#p#

AISprite.cs(敵軍 Sprite 模擬器)

  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11.  
  12. using System.Collections.Generic;  
  13. using FarseerGames.FarseerPhysics.Mathematics;  
  14.  
  15. using FarseerGames.FarseerPhysics;  
  16. using FarseerGames.FarseerPhysics.Collisions;  
  17. using FarseerGames.FarseerPhysics.Dynamics;  
  18.  
  19. namespace YYArena.Core  
  20. {  
  21.     /**//// <summary> 
  22.     /// 敵軍 Sprite  
  23.     /// </summary> 
  24.     public class AISprite : Sprite, IFire  
  25.     {  
  26.         private Sprite _attackTarget;  
  27.         private int _aiLevel;  
  28.         private IPhysicsControl _physicsControl;  
  29.  
  30.         /**//// <summary> 
  31.         /// 構造函數  
  32.         /// </summary> 
  33.         /// <param name="physicsSimulator">PhysicsSimulator</param> 
  34.         /// <param name="physicsControl">IPhysicsControl</param> 
  35.         /// <param name="position">初始位置</param> 
  36.         /// <param name="angle">初始轉角</param> 
  37.         /// <param name="originalVelocity">初始速度</param> 
  38.         /// <param name="attackTarget">攻擊目標</param> 
  39.         /// <param name="aiLevel">ai等級</param> 
  40.         public AISprite(PhysicsSimulator physicsSimulator,  
  41.             IPhysicsControl physicsControl, Vector2 position, float angle, float originalVelocity, Sprite attackTarget, int aiLevel)  
  42.             : base(physicsSimulator, physicsControl, position, angle, originalVelocity)  
  43.         {  
  44.             // 上次開火時間  
  45.             PrevFireDateTime = DateTime.Now.AddSeconds(3);  
  46.             // 最小開火間隔  
  47.             MinFireInterval = 3000d;  
  48.  
  49.             _attackTarget = attackTarget;  
  50.             _aiLevel = aiLevel;  
  51.             _physicsControl = physicsControl;  
  52.  
  53.             InitAI();  
  54.  
  55.             CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);  
  56.         }  
  57.  
  58.         private void InitAI()  
  59.         {  
  60.             // 根據 ai 等級設置最小開火間隔  
  61.             double fireCoefficient = 1 + 30 / _aiLevel;  
  62.             MinFireInterval = Helper.GenerateRandom((int)MinFireInterval, (int)(fireCoefficient * MinFireInterval));  
  63.         }  
  64.  
  65.         void CompositionTarget_Rendering(object sender, EventArgs e)  
  66.         {  
  67.             if (Enabled && AttackTarget.Enabled)  
  68.             {  
  69.                 // 是否開火  
  70.                 if ((DateTime.Now - PrevFireDateTime).TotalMilliseconds > MinFireInterval)  
  71.                 {  
  72.                     PrevFireDateTime = DateTime.Now;  
  73.  
  74.                     if (Fire != null)  
  75.                         Fire(this, EventArgs.Empty);  
  76.                 }  
  77.             }  
  78.         }  
  79.  
  80.         public DateTime PrevFireDateTime { get; set; }  
  81.  
  82.         public double MinFireInterval { get; set; }  
  83.  
  84.         public event EventHandler<EventArgs> Fire;  
  85.  
  86.  
  87.         public Sprite AttackTarget  
  88.         {  
  89.             get { return _attackTarget; }  
  90.             set { _attackTarget = value; }  
  91.         }  
  92.  
  93.         protected override Vector2 GetForce()  
  94.         {  
  95.             Vector2 force = Vector2.Zero;  
  96.  
  97.             if (!_attackTarget.Enabled)  
  98.                 return force;  
  99.  
  100.             force += Helper.Convert2Vector(_physicsControl.ForceAmount, playerBox.Body.Rotation);  
  101.  
  102.             // 根據 ai 等級做最大線性速度限制  
  103.             if (playerBox.Body.LinearVelocity.Length() > _physicsControl.MaxLinearVelocity * Helper.GenerateRandom(50, 200) / 1000)  
  104.                 force = Vector2.Zero;  
  105.  
  106.             return force;  
  107.         }  
  108.  
  109.         protected override float GetTorque()  
  110.         {  
  111.             float torque = 0f;  
  112.  
  113.             if (!_attackTarget.Enabled)  
  114.                 return torque;  
  115.  
  116.             // 按某個方向旋轉,原則是以最小的旋轉角度對準目標  
  117.             Vector2 relativePosition = _attackTarget.PhysicsBox.Body.Position - playerBox.Body.Position;  
  118.             double targetRotation = Helper.Convert2Rotation(relativePosition);  
  119.             double currentRotation = playerBox.Body.Rotation;  
  120.             double relativeAngle = Helper.Radian2Angle(targetRotation - currentRotation);  
  121.             if (relativeAngle < 0)  
  122.                 relativeAngle += 360;  
  123.  
  124.             if (relativeAngle > 1)  
  125.             {  
  126.                 if (relativeAngle < 180 && relativeAngle > 0)  
  127.                     torque += _physicsControl.TorqueAmount;  
  128.                 else if (relativeAngle > 180 && relativeAngle < 360)  
  129.                     torque -_physicsControl.TorqueAmount;  
  130.             }  
  131.             else  
  132.             {  
  133.                 playerBox.Body.AngularVelocity = 0;  
  134.             }  
  135.  
  136.  
  137.             // 最大轉速限制  
  138.             if (Math.Abs(playerBox.Body.AngularVelocity) > _physicsControl.MaxAngularVelocity)  
  139.                 torque = 0;  
  140.              
  141.             return torque;  
  142.         }  
  143.     }  

[源碼下載]

原文鏈接:http://www.cnblogs.com/webabcd/archive/2009/06/22/1508042.html

責任編輯:張偉 來源: webabcd的博客
相關推薦

2024-09-29 14:33:30

數據飛輪數據中臺數字化轉型

2013-09-12 11:17:02

2024-05-31 14:23:15

2014-10-31 15:43:02

華為智慧

2025-02-18 15:09:07

2012-06-05 14:42:57

Silverlight

2022-04-12 18:35:03

元宇宙

2013-03-22 14:08:14

智能手表IT巨頭競技場

2025-06-06 14:23:48

谷歌模型AI

2025-02-17 12:24:43

2024-05-20 15:25:47

2025-04-08 08:50:00

AI模型測試

2024-04-22 08:40:00

LLM模型開源

2024-11-21 12:09:26

2024-06-20 14:04:17

2025-02-28 09:00:00

2025-03-05 09:32:00

2025-04-14 09:06:00

2025-04-07 09:35:00

Meta模型開源

2024-03-08 13:02:56

Claude 3GPT-4Opus
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人精品一区二区三区中文字幕 | 亚洲成人高清 | 日韩欧美不卡 | 91久久国产综合久久91精品网站 | 欧美三级成人理伦 | 综合自拍| 国产色婷婷久久99精品91 | 久久久国产一区二区三区 | 国产精品一区二区三区久久久 | 亚洲人成人一区二区在线观看 | 国产日产精品一区二区三区四区 | 精品亚洲一区二区 | 亚洲国产区 | 国产重口老太伦 | 在线看片福利 | av一级| www.久久.com | 欧美一级片在线看 | 色成人免费网站 | 人人干人人艹 | 九九精品视频在线 | 亚洲国产成人精品女人久久久 | 国产美女特级嫩嫩嫩bbb片 | 81精品国产乱码久久久久久 | 精品久久久久久国产 | 国产成人精品一区二区三区四区 | 精品少妇一区二区三区在线播放 | 久久av资源网 | 国产福利视频网站 | 精品国产乱码久久久久久久久 | 男女啪啪高潮无遮挡免费动态 | 一区二区三区中文字幕 | 97视频在线免费 | 国产精品亚洲精品久久 | 国产成人免费视频 | 亚洲成人精 | 成人久久18免费网站麻豆 | 伊人久久精品一区二区三区 | 一本一道久久a久久精品蜜桃 | 成人视屏在线观看 | 国产在线播 |