SilverLight练习,移动方块的小游戏(源代码)

测试页面在这里,http://silverlight.services.live.com/invoke/84388/MoveBlock/iframe.html

代码如下:

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using System.Windows.Shapes;
  7. using System.Windows.Threading;
  8. namespace Escapa
  9. {
  10.     public partial class Page : UserControl
  11.     {
  12.         private Rectangle blackRect = new Rectangle();
  13.         private Rectangle whiteRect = new Rectangle();
  14.         private Rectangle blueRect1 = new Rectangle();
  15.         private Rectangle blueRect2 = new Rectangle();
  16.         private Rectangle blueRect3 = new Rectangle();
  17.         private Rectangle blueRect4 = new Rectangle();
  18.         private Rectangle redRect = new Rectangle();
  19.         private TextBlock readme = new TextBlock();
  20.         private DispatcherTimer timer = new DispatcherTimer();
  21.         private DateTime startTime;
  22.         private void defaultValue()
  23.         {
  24.             this.blackRect.Height = 452; this.blackRect.Width = 452;
  25.             this.blackRect.SetValue(Canvas.LeftProperty, 0.0);
  26.             this.blackRect.SetValue(Canvas.TopProperty, 0.0);
  27.             this.blackRect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0, 0, 0));
  28.             this.whiteRect.Height = 349; this.whiteRect.Width = 349;
  29.             this.whiteRect.SetValue(Canvas.LeftProperty, 46.0);
  30.             this.whiteRect.SetValue(Canvas.TopProperty, 46.0);
  31.             this.whiteRect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF));
  32.             this.whiteRect.HorizontalAlignment = HorizontalAlignment.Center;
  33.             this.VerticalAlignment = VerticalAlignment.Center;
  34.             this.blueRect1.Height = 62;
  35.             this.blueRect1.Width = 62;
  36.             this.blueRect1.SetValue(Canvas.LeftProperty, 68.0);
  37.             this.blueRect1.SetValue(Canvas.TopProperty, 66.0);
  38.             this.blueRect1.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
  39.             this.blueRect2.Height = 52;
  40.             this.blueRect2.Width = 62;
  41.             this.blueRect2.SetValue(Canvas.LeftProperty, 268.0);
  42.             this.blueRect2.SetValue(Canvas.TopProperty, 56.0);
  43.             this.blueRect2.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
  44.             this.blueRect3.Height = 62;
  45.             this.blueRect3.Width = 32;
  46.             this.blueRect3.SetValue(Canvas.LeftProperty, 68.0);
  47.             this.blueRect3.SetValue(Canvas.TopProperty, 316.0);
  48.             this.blueRect3.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
  49.             this.blueRect4.Height = 22;
  50.             this.blueRect4.Width = 98;
  51.             this.blueRect4.SetValue(Canvas.LeftProperty, 298.0);
  52.             this.blueRect4.SetValue(Canvas.TopProperty, 326.0);
  53.             this.blueRect4.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
  54.             this.blueRect4.Height = 22;
  55.             this.blueRect4.Width = 98;
  56.             this.blueRect4.SetValue(Canvas.LeftProperty, 298.0);
  57.             this.blueRect4.SetValue(Canvas.TopProperty, 326.0);
  58.             this.blueRect4.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
  59.             this.redRect.Height = 41;
  60.             this.redRect.Width = 41;
  61.             this.redRect.SetValue(Canvas.LeftProperty, 204.0);
  62.             this.redRect.SetValue(Canvas.TopProperty, 202.0);
  63.             this.redRect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
  64.             this.xs = 3.0;  //默认横移动速度
  65.             this.ys = 2.0;  //默认竖移动速度
  66.         }
  67.         /// <summary>一横一竖线是否相交</summary>
  68.         /// <param name="hx1">横线左</param>
  69.         /// <param name="hx2">横线右</param>
  70.         /// <param name="hy">横线y</param>
  71.         /// <param name="vy1">竖线高</param>
  72.         /// <param name="vy2">竖线低</param>
  73.         /// <param name="vx">竖线x</param>
  74.         /// <returns></returns>
  75.         private bool IsLineCross(double hx1, double hx2, double hy, double vy1, double vy2, double vx)
  76.         {
  77.             return (vy1 < hy) && (vy2 > hy) && (hx1 < vx) && (hx2 > vx);
  78.         }
  79.         private bool IsRectCross(Rectangle r1, Rectangle r2)
  80.         {
  81.             double x11 = (double)r1.GetValue(Canvas.LeftProperty);
  82.             double y11 = (double)r1.GetValue(Canvas.TopProperty);
  83.             double x12 = x11 + r1.Width;
  84.             double y12 = y11 + r1.Height;
  85.             double x21 = (double)r2.GetValue(Canvas.LeftProperty);
  86.             double y21 = (double)r2.GetValue(Canvas.TopProperty);
  87.             double x22 = x21 + r2.Width;
  88.             double y22 = y21 + r2.Height;
  89.             return
  90.                 IsLineCross(x11, x12, y11, y21, y22, x21)       //横上 竖左
  91.                 || IsLineCross(x11, x12, y11, y21, y22, x22)    //横上 竖右
  92.                 || IsLineCross(x11, x12, y12, y21, y22, x21)    //横下 竖左
  93.                 || IsLineCross(x11, x12, y12, y21, y22, x22)    //横下 竖右
  94.                 || IsLineCross(x21, x22, y21, y11, y12, x11)    //横上 竖左
  95.                 || IsLineCross(x21, x22, y21, y11, y12, x12)    //横上 竖右
  96.                 || IsLineCross(x21, x22, y22, y11, y12, x11)    //横下 竖左
  97.                 || IsLineCross(x21, x22, y22, y11, y12, x12)    //横下 竖右
  98.                 ;
  99.         }
  100.         public Page()
  101.         {
  102.             InitializeComponent();
  103.             Canvas canvas = new Canvas();
  104.             this.LayoutRoot.Children.Add(canvas);
  105.             canvas.Children.Add(this.blackRect);
  106.             canvas.Children.Add(this.whiteRect);
  107.             canvas.Children.Add(this.blueRect1);
  108.             canvas.Children.Add(this.blueRect2);
  109.             canvas.Children.Add(this.blueRect3);
  110.             canvas.Children.Add(this.blueRect4);
  111.             canvas.Children.Add(this.redRect);
  112.             canvas.Children.Add(readme);
  113.             readme.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 100, 180));
  114.             readme.FontSize = 20;
  115.             readme.Text = "在白色范围内移动红色方块,避免碰到蓝色方块";
  116.             this.defaultValue();
  117.             this.timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
  118.             this.timer.Tick += new EventHandler(timer_Tick);
  119.             this.redRect.MouseLeftButtonDown += new MouseButtonEventHandler(redRect_MouseLeftButtonDown);
  120.             this.redRect.MouseMove += new MouseEventHandler(redRect_MouseMove);
  121.             this.redRect.MouseLeftButtonUp += new MouseButtonEventHandler(redRect_MouseLeftButtonUp);
  122.         }
  123.         private void CheckFinish()
  124.         {
  125.             //判断出白界
  126.             double redx = (double)this.redRect.GetValue(Canvas.LeftProperty);
  127.             double redy = (double)this.redRect.GetValue(Canvas.TopProperty);
  128.             double whitex = (double)this.whiteRect.GetValue(Canvas.LeftProperty);
  129.             double whitey = (double)this.whiteRect.GetValue(Canvas.TopProperty);
  130.             //判断红蓝碰撞
  131.             if (redx < whitex || redx + this.redRect.Width > whitex + this.whiteRect.Width) this.Finish();
  132.             if (redy < whitey || redy + this.redRect.Height > whitey + this.whiteRect.Height) this.Finish();
  133.             if (
  134.                 IsRectCross(this.redRect, this.blueRect1) ||
  135.                 IsRectCross(this.redRect, this.blueRect2) ||
  136.                 IsRectCross(this.redRect, this.blueRect3) ||
  137.                 IsRectCross(this.redRect, this.blueRect4)
  138.                 )
  139.             {
  140.                 this.Finish();
  141.             }
  142.         }
  143.         private void Finish()
  144.         {
  145.             this.isMouseDown = false;
  146.             this.redRect.ReleaseMouseCapture();
  147.             MessageBox.Show(string.Format("{0}秒", (DateTime.Now - this.startTime).TotalSeconds));
  148.             this.timer.Stop();
  149.             this.readme.Visibility = Visibility.Visible;
  150.             this.defaultValue();
  151.         }
  152.         //4个蓝色方块的移动方向
  153.         private int fx1 = 1; private int fy1 = 1;
  154.         private int fx2 = -1; private int fy2 = 1;
  155.         private int fx3 = 1; private int fy3 = -1;
  156.         private int fx4 = -1; private int fy4 = -1;
  157.         private double xs;
  158.         private double ys;
  159.         void timer_Tick(object sender, EventArgs e)
  160.         {
  161.             double v1x = (double)this.blueRect1.GetValue(Canvas.LeftProperty);
  162.             double v1y = (double)this.blueRect1.GetValue(Canvas.TopProperty);
  163.             double v2x = (double)this.blueRect2.GetValue(Canvas.LeftProperty);
  164.             double v2y = (double)this.blueRect2.GetValue(Canvas.TopProperty);
  165.             double v3x = (double)this.blueRect3.GetValue(Canvas.LeftProperty);
  166.             double v3y = (double)this.blueRect3.GetValue(Canvas.TopProperty);
  167.             double v4x = (double)this.blueRect4.GetValue(Canvas.LeftProperty);
  168.             double v4y = (double)this.blueRect4.GetValue(Canvas.TopProperty);
  169.             v1x += xs * fx1; v1y += ys * fy1;
  170.             v2x += xs * fx2; v2y += ys * fy2;
  171.             v3x += xs * fx3; v3y += ys * fy3;
  172.             v4x += xs * fx4; v4y += ys * fy4;
  173.             if (v1x < 0 || v1x > this.blackRect.Width - this.blueRect1.Width) fx1 *= -1;
  174.             if (v1y < 0 || v1y > this.blackRect.Height - this.blueRect1.Height) fy1 *= -1;
  175.             if (v2x < 0 || v2x > this.blackRect.Width - this.blueRect2.Width) fx2 *= -1;
  176.             if (v2y < 0 || v2y > this.blackRect.Height - this.blueRect2.Height) fy2 *= -1;
  177.             if (v3x < 0 || v3x > this.blackRect.Width - this.blueRect3.Width) fx3 *= -1;
  178.             if (v3y < 0 || v3y > this.blackRect.Height - this.blueRect3.Height) fy3 *= -1;
  179.             if (v4x < 0 || v4x > this.blackRect.Width - this.blueRect4.Width) fx4 *= -1;
  180.             if (v4y < 0 || v4y > this.blackRect.Height - this.blueRect4.Height) fy4 *= -1;
  181.             this.blueRect1.SetValue(Canvas.LeftProperty, v1x);
  182.             this.blueRect1.SetValue(Canvas.TopProperty, v1y);
  183.             this.blueRect2.SetValue(Canvas.LeftProperty, v2x);
  184.             this.blueRect2.SetValue(Canvas.TopProperty, v2y);
  185.             this.blueRect3.SetValue(Canvas.LeftProperty, v3x);
  186.             this.blueRect3.SetValue(Canvas.TopProperty, v3y);
  187.             this.blueRect4.SetValue(Canvas.LeftProperty, v4x);
  188.             this.blueRect4.SetValue(Canvas.TopProperty, v4y);
  189.             //每次速度提升1/1000
  190.             this.xs *= 1.001;
  191.             this.ys *= 1.001;
  192.             this.CheckFinish();
  193.         }
  194.         private double beginX;
  195.         private double beginY;
  196.         private bool isMouseDown;
  197.         void redRect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  198.         {
  199.             this.beginX = e.GetPosition(null).X;
  200.             this.beginY = e.GetPosition(null).Y;
  201.             this.isMouseDown = true;
  202.             ((UIElement)sender).CaptureMouse();
  203.             if (this.timer.IsEnabled == false)
  204.             {
  205.                 this.timer.Start();  //开始移动
  206.                 this.readme.Visibility = Visibility.Collapsed;
  207.                 this.startTime = DateTime.Now;
  208.             }
  209.         }
  210.         void redRect_MouseMove(object sender, MouseEventArgs e)
  211.         {
  212.             if (isMouseDown == true)
  213.             {
  214.                 double currX = e.GetPosition(null).X;
  215.                 double currY = e.GetPosition(null).Y;
  216.                 double currLeft = (double)((UIElement)sender).GetValue(Canvas.LeftProperty);
  217.                 double currTop = (double)((UIElement)sender).GetValue(Canvas.TopProperty);
  218.                 ((UIElement)sender).SetValue(Canvas.LeftProperty, currLeft + currX - beginX);
  219.                 ((UIElement)sender).SetValue(Canvas.TopProperty, currTop + currY - beginY);
  220.                 this.beginX = currX;
  221.                 this.beginY = currY;
  222.             }
  223.         }
  224.         void redRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  225.         {
  226.             this.isMouseDown = false;
  227.             ((UIElement)sender).ReleaseMouseCapture();
  228.         }
  229.     }
  230. }

猜你喜欢

转载自blog.csdn.net/KAMILLE/article/details/3370307