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

[索引页]
[源码下载]


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



作者:webabcd


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


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


在线DEMO
Get Microsoft Silverlight


思路
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(000033);
        }


        
void InitData()
        
{
            random 
= new Random();

            sineWave 
= new SineWave
            
{
                A 
= random.Next(4060),
                OffsetY 
= random.Next((int)minY, (int)maxY),
                Omega 
= 1d / random.Next(2050),
                Phi 
= random.Next((int)minX * 2, (int)minX),
            }
;

            evt 
= new FlyEventArgs()
            
{
                X 
= minX,
                Z 
= (minZ + maxZ) / 2,
                Z_Out 
= Convert.ToBoolean(random.Next(02))
            }
;
        }


        
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(03/ 200d;
                
else
                    evt.Z_Out 
= false;
            }

            
else
            
{
                
if (evt.Z > minZ)
                    evt.Z 
-= random.Next(03/ 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 getset; }
        
public IFlyModel FlyModel getset; }
    }

}



OK
[源码下载]

posted @ 2009-03-19 08:57  webabcd  阅读(9191)  评论(53编辑  收藏  举报