[索引页]
[源码下载]
游戏人生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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace YYFly.Model
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public interface IFlyModel
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 让一只苍蝇开始飞
/// </summary>
void Start();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 让一只苍蝇停止飞
/// </summary>
void Stop();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 计分方法
/// </summary>
void Score();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 苍蝇改变位置所触发的事件
/// </summary>
event EventHandler<FlyEventArgs> FlyChanging;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace YYFly.Model
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class FlyModel : IFlyModel
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static readonly object objLock = new object();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FlyModel()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 0, 0, 33);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void InitData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
random = new Random();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sineWave = new SineWave
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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),
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
evt = new FlyEventArgs()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
X = minX,
Z = (minZ + maxZ) / 2,
Z_Out = Convert.ToBoolean(random.Next(0, 2))
};
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void timer_Tick(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
evt.X += Singleton<Scorer>.Instance.Level * 1.5;
evt.Y = sineWave.OffsetY + sineWave.A * Math.Sin(sineWave.Omega * evt.X + sineWave.Phi);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (evt.Z_Out)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (evt.Z < maxZ)
evt.Z += random.Next(0, 3) / 200d;
else
evt.Z_Out = false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (evt.Z > minZ)
evt.Z -= random.Next(0, 3) / 200d;
else
evt.Z_Out = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
OnFlyChanging(evt);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Start()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitData();
timer.Start();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Stop()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
timer.Stop();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Score()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (objLock)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Singleton<Scorer>.Instance.Score += 1;
Singleton<Scorer>.Instance.Level = (int)Singleton<Scorer>.Instance.Score / 10 + 1;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
finally
{}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public event EventHandler<FlyEventArgs> FlyChanging;
protected virtual void OnFlyChanging(FlyEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EventHandler<FlyEventArgs> handler = FlyChanging;
if (handler != null)
handler(this, e);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace YYFly.View
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public interface IFlyView
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 苍蝇开始飞的事件处理器
/// </summary>
event EventHandler Start;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 苍蝇停止飞的事件处理器
/// </summary>
event EventHandler Stop;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 打死苍蝇后的计分事件
/// </summary>
event EventHandler Score;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace YYFly.View
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class Fly : UserControl, IFlyView
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Fly()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Loaded += new RoutedEventHandler(Fly_Loaded);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Fly_Loaded(object sender, RoutedEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fly.SetValue(Canvas.LeftProperty, x);
fly.SetValue(Canvas.TopProperty, y);
st.ScaleX = st.ScaleY = z;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 打到苍蝇后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void fly_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (aniWing.GetCurrentState() != ClockState.Stopped)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
aniFlyDisappear.Begin();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
OnScore();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 苍蝇消失的动画结束后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void aniFlyDisappear_Completed(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OnStop();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 初始化苍蝇的位置
/// </summary>
public void InitPosition()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fly.SetValue(Canvas.LeftProperty, -300d);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public event EventHandler Start;
public void OnStart()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitPosition();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
aniFlyDisappear.Stop();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Start != null)
Start(this, EventArgs.Empty);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public event EventHandler Stop;
public void OnStop()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitPosition();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Stop != null)
Stop(this, EventArgs.Empty);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public event EventHandler Score;
public void OnScore()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using YYFly.Model;
using YYFly.View;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace YYFly.Presenter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Model - View - Presenter 模式中的 Presenter
/// </summary>
public class FlyPresenter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public FlyPresenter(IFlyView flyView, IFlyModel flyModel)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FlyView = flyView;
FlyModel = flyModel;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FlyView.Start += new EventHandler(FlyView_Start);
FlyView.Stop += new EventHandler(FlyView_Stop);
flyView.Score += new EventHandler(flyView_Score);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FlyModel.FlyChanging += new EventHandler<FlyEventArgs>(FlyModel_FlyChanging);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void FlyView_Start(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FlyModel.Start();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void FlyView_Stop(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FlyModel.Stop();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void flyView_Score(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FlyModel.Score();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void FlyModel_FlyChanging(object sender, FlyEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FlyView.Update(e.X, e.Y, e.Z);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public IFlyView FlyView
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public IFlyModel FlyModel
{ get; set; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
OK
[源码下载]