游戏人生Silverlight(3) - 打苍蝇[Silverlight 2.0(c#)]

简介:
[源码下载]


游戏人生Silverlight(3) - 打苍蝇[Silverlight 2.0(c#)]


作者: webabcd


介绍
使用 Silverlight 2.0(c#) 开发一个打苍蝇的游戏


玩法
用鼠标控制瞄准镜的移动,鼠标左键开火


在线DEMO



思路
1、苍蝇的移动轨迹用一个简单的公式计算,即正弦波。正弦波的公式:y = A * sin(ωx + φ)
2、苍蝇控件使用MVP(Model - View - Presenter)模式开发,其目的只是为了说明如何在Silverlight中应用MVP模式


关键代码
1、苍蝇控件的Model
IFlyModel.cs
using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
 
namespace YYFly.Model 

         public  interface IFlyModel 
        { 
                 /// <summary> 
                 /// 让一只苍蝇开始飞 
                 /// </summary> 
                 void Start(); 
                 /// <summary> 
                 /// 让一只苍蝇停止飞 
                 /// </summary> 
                 void Stop(); 
                 /// <summary> 
                 /// 计分方法 
                 /// </summary> 
                 void Score(); 
 
                 /// <summary> 
                 /// 苍蝇改变位置所触发的事件 
                 /// </summary> 
                 event EventHandler<FlyEventArgs> FlyChanging; 
        } 
}
 
FlyModel.cs
using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Threading; 
 
namespace YYFly.Model 

         public  class FlyModel : IFlyModel 
        { 
                 private DispatcherTimer timer;  // 改变苍蝇位置的计时器 
                 private SineWave sineWave;  // 苍蝇的运动轨迹(正弦波)的参数 
                 private FlyEventArgs evt;  // 苍蝇的位置参数 
                 private Random random;    
                 private  double minX = -100, minY = 40, maxY = 300, minZ = 0.1, maxZ = 0.5; 
 
                 private  static  readonly  object objLock =  new  object(); 
 
                 public FlyModel() 
                { 
                        timer =  new DispatcherTimer(); 
                        timer.Tick +=  new EventHandler(timer_Tick); 
                        timer.Interval =  new TimeSpan(0, 0, 0, 0, 33); 
                } 
 
                 void InitData() 
                { 
                        random =  new Random(); 
 
                        sineWave =  new SineWave 
                        { 
                                A = random.Next(40, 60), 
                                OffsetY = random.Next(( int)minY, ( int)maxY), 
                                Omega = 1d / random.Next(20, 50), 
                                Phi = random.Next(( int)minX * 2, ( int)minX), 
                        }; 
 
                        evt =  new FlyEventArgs() 
                        { 
                                X = minX, 
                                Z = (minZ + maxZ) / 2, 
                                Z_Out = Convert.ToBoolean(random.Next(0, 2)) 
                        }; 
                } 
 
                 void timer_Tick( object sender, EventArgs e) 
                { 
                        evt.X += Singleton<Scorer>.Instance.Level * 1.5; 
                        evt.Y = sineWave.OffsetY + sineWave.A * Math.Sin(sineWave.Omega * evt.X + sineWave.Phi); 
 
                         if (evt.Z_Out) 
                        { 
                                 if (evt.Z < maxZ) 
                                        evt.Z += random.Next(0, 3) / 200d; 
                                 else 
                                        evt.Z_Out =  false
                        } 
                         else 
                        { 
                                 if (evt.Z > minZ) 
                                        evt.Z -= random.Next(0, 3) / 200d; 
                                 else 
                                        evt.Z_Out =  true
                        } 
 
                        OnFlyChanging(evt); 
                } 
 
                 public  void Start() 
                { 
                        InitData(); 
                        timer.Start(); 
                } 
 
                 public  void Stop() 
                { 
                        timer.Stop(); 
                } 
 
                 public  void Score() 
                { 
                         try 
                        { 
                                 lock (objLock) 
                                { 
                                        Singleton<Scorer>.Instance.Score += 1; 
                                        Singleton<Scorer>.Instance.Level = ( int)Singleton<Scorer>.Instance.Score / 10 + 1; 
                                } 
                        } 
                         finally {} 
                } 
 
                 public  event EventHandler<FlyEventArgs> FlyChanging; 
                 protected  virtual  void OnFlyChanging(FlyEventArgs e) 
                { 
                        EventHandler<FlyEventArgs> handler = FlyChanging; 
                         if (handler !=  null
                                handler( this, e); 
                } 
        } 
}
 
2、苍蝇控件的View
IFlyView.cs
using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
 
namespace YYFly.View 

         public  interface IFlyView 
        { 
                 /// <summary> 
                 /// 更新苍蝇的位置 
                 /// </summary> 
                 /// <param name="x">X 轴坐标</param> 
                 /// <param name="y">Y 轴坐标</param> 
                 /// <param name="z">Z 轴方向上的缩放比例</param> 
                 void Update( double x,  double y,  double z); 
 
                 /// <summary> 
                 /// 苍蝇开始飞的事件处理器 
                 /// </summary> 
                 event EventHandler Start; 
                 /// <summary> 
                 /// 苍蝇停止飞的事件处理器 
                 /// </summary> 
                 event EventHandler Stop; 
                 /// <summary> 
                 /// 打死苍蝇后的计分事件 
                 /// </summary> 
                 event EventHandler Score; 
        } 
}
 
Fly.xaml.cs
using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
 
namespace YYFly.View 

         public partial  class Fly : UserControl, IFlyView 
        { 
                 public Fly() 
                { 
                        InitializeComponent(); 
 
                         this.Loaded +=  new RoutedEventHandler(Fly_Loaded); 
                } 
 
                 void Fly_Loaded( object sender, RoutedEventArgs e) 
                { 
                         
                } 
 
                 /// <summary> 
                 /// 更新苍蝇的位置 
                 /// </summary> 
                 /// <param name="x">X 轴坐标</param> 
                 /// <param name="y">Y 轴坐标</param> 
                 /// <param name="z">Z 轴方向上的缩放比例</param> 
                 public  void Update( double x,  double y,  double z) 
                { 
                        fly.SetValue(Canvas.LeftProperty, x); 
                        fly.SetValue(Canvas.TopProperty, y); 
                        st.ScaleX = st.ScaleY = z; 
                } 
 
                 /// <summary> 
                 /// 打到苍蝇后 
                 /// </summary> 
                 /// <param name="sender"></param> 
                 /// <param name="e"></param> 
                 private  void fly_MouseLeftButtonDown( object sender, MouseButtonEventArgs e) 
                { 
                         if (aniWing.GetCurrentState() != ClockState.Stopped) 
                        { 
                                aniFlyDisappear.Begin(); 
 
                                OnScore(); 
                        } 
                } 
 
                 /// <summary> 
                 /// 苍蝇消失的动画结束后 
                 /// </summary> 
                 /// <param name="sender"></param> 
                 /// <param name="e"></param> 
                 private  void aniFlyDisappear_Completed( object sender, EventArgs e) 
                { 
                        OnStop(); 
                } 
 
                 /// <summary> 
                 /// 初始化苍蝇的位置 
                 /// </summary> 
                 public  void InitPosition() 
                { 
                        fly.SetValue(Canvas.LeftProperty, -300d); 
                } 
 
                 public  event EventHandler Start; 
                 public  void OnStart() 
                { 
                        InitPosition(); 
 
                        aniFlyDisappear.Stop(); 
 
                         if (Start !=  null
                                Start( this, EventArgs.Empty); 
                } 
 
                 public  event EventHandler Stop; 
                 public  void OnStop() 
                { 
                        InitPosition(); 
 
                         if (Stop !=  null
                                Stop( this, EventArgs.Empty); 
                } 
 
                 public  event EventHandler Score; 
                 public  void OnScore() 
                { 
                         if (Score !=  null
                                Score( this, EventArgs.Empty); 
                } 
        } 
}
 
3、苍蝇控件的Presenter
FlyPresenter.cs
using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
 
using YYFly.Model; 
using YYFly.View; 
 
namespace YYFly.Presenter 

         /// <summary> 
         /// Model - View - Presenter 模式中的 Presenter 
         /// </summary> 
         public  class FlyPresenter 
        { 
                 public FlyPresenter(IFlyView flyView, IFlyModel flyModel) 
                { 
                        FlyView = flyView; 
                        FlyModel = flyModel; 
 
                        FlyView.Start +=  new EventHandler(FlyView_Start); 
                        FlyView.Stop +=  new EventHandler(FlyView_Stop); 
                        flyView.Score +=  new EventHandler(flyView_Score); 
 
                        FlyModel.FlyChanging +=  new EventHandler<FlyEventArgs>(FlyModel_FlyChanging); 
                } 
 
                 void FlyView_Start( object sender, EventArgs e) 
                { 
                        FlyModel.Start(); 
                } 
 
                 void FlyView_Stop( object sender, EventArgs e) 
                { 
                        FlyModel.Stop(); 
                } 
 
                 void flyView_Score( object sender, EventArgs e) 
                { 
                        FlyModel.Score(); 
                } 
 
                 void FlyModel_FlyChanging( object sender, FlyEventArgs e) 
                { 
                        FlyView.Update(e.X, e.Y, e.Z); 
                } 
 
                 public IFlyView FlyView { get; set; } 
                 public IFlyModel FlyModel { get; set; } 
        } 
}
 




     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/345626 ,如需转载请自行联系原作者

相关文章
|
C# 图形学
C#之四十九 游戏编程周每日总结
C#之四十九 游戏编程周每日总结
53 0
|
2月前
|
SQL API 定位技术
基于C#使用winform技术的游戏平台的实现【C#课程设计】
本文介绍了基于C#使用WinForms技术开发的游戏平台项目,包括项目结构、运行截图、实现功能、部分代码说明、数据库设计和完整代码资源。项目涵盖了登录注册、个人信息修改、游戏商城列表查看、游戏管理、用户信息管理、数据分析等功能。代码示例包括ListView和ImageList的使用、图片上传、图表插件使用和SQL工具类封装,以及高德地图天气API的调用。
基于C#使用winform技术的游戏平台的实现【C#课程设计】
|
4月前
|
存储 缓存 C#
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
C#语言编写的仅有8KB大小的简易贪吃蛇开源游戏
|
6月前
|
程序员 C# Python
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
|
6月前
|
存储 程序员 C#
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
|
6月前
|
存储 C# 开发工具
22.C# 中使用变量记录玩家创建的角色名:实现与游戏角色的互动
22.C# 中使用变量记录玩家创建的角色名:实现与游戏角色的互动
58 0
|
6月前
|
定位技术 C# 图形学
Unity和C#游戏编程入门:创建迷宫小球游戏示例
Unity和C#游戏编程入门:创建迷宫小球游戏示例
152 2
C#实现的打飞机游戏(课程设计)
C#实现的打飞机游戏(课程设计)
144 1
|
存储 C# UED
C# 拼图游戏(超详细)
C# 拼图游戏(超详细)
461 0
C# 拼图游戏(超详细)