测试页面在这里,http://silverlight.services.live.com/invoke/84388/MoveBlock/iframe.html
代码如下:
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace Escapa
- {
- public partial class Page : UserControl
- {
- private Rectangle blackRect = new Rectangle();
- private Rectangle whiteRect = new Rectangle();
- private Rectangle blueRect1 = new Rectangle();
- private Rectangle blueRect2 = new Rectangle();
- private Rectangle blueRect3 = new Rectangle();
- private Rectangle blueRect4 = new Rectangle();
- private Rectangle redRect = new Rectangle();
- private TextBlock readme = new TextBlock();
- private DispatcherTimer timer = new DispatcherTimer();
- private DateTime startTime;
- private void defaultValue()
- {
- this.blackRect.Height = 452; this.blackRect.Width = 452;
- this.blackRect.SetValue(Canvas.LeftProperty, 0.0);
- this.blackRect.SetValue(Canvas.TopProperty, 0.0);
- this.blackRect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0, 0, 0));
- this.whiteRect.Height = 349; this.whiteRect.Width = 349;
- this.whiteRect.SetValue(Canvas.LeftProperty, 46.0);
- this.whiteRect.SetValue(Canvas.TopProperty, 46.0);
- this.whiteRect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF));
- this.whiteRect.HorizontalAlignment = HorizontalAlignment.Center;
- this.VerticalAlignment = VerticalAlignment.Center;
- this.blueRect1.Height = 62;
- this.blueRect1.Width = 62;
- this.blueRect1.SetValue(Canvas.LeftProperty, 68.0);
- this.blueRect1.SetValue(Canvas.TopProperty, 66.0);
- this.blueRect1.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
- this.blueRect2.Height = 52;
- this.blueRect2.Width = 62;
- this.blueRect2.SetValue(Canvas.LeftProperty, 268.0);
- this.blueRect2.SetValue(Canvas.TopProperty, 56.0);
- this.blueRect2.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
- this.blueRect3.Height = 62;
- this.blueRect3.Width = 32;
- this.blueRect3.SetValue(Canvas.LeftProperty, 68.0);
- this.blueRect3.SetValue(Canvas.TopProperty, 316.0);
- this.blueRect3.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
- this.blueRect4.Height = 22;
- this.blueRect4.Width = 98;
- this.blueRect4.SetValue(Canvas.LeftProperty, 298.0);
- this.blueRect4.SetValue(Canvas.TopProperty, 326.0);
- this.blueRect4.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
- this.blueRect4.Height = 22;
- this.blueRect4.Width = 98;
- this.blueRect4.SetValue(Canvas.LeftProperty, 298.0);
- this.blueRect4.SetValue(Canvas.TopProperty, 326.0);
- this.blueRect4.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0F, 0x6F, 0xFF));
- this.redRect.Height = 41;
- this.redRect.Width = 41;
- this.redRect.SetValue(Canvas.LeftProperty, 204.0);
- this.redRect.SetValue(Canvas.TopProperty, 202.0);
- this.redRect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
- this.xs = 3.0; //默认横移动速度
- this.ys = 2.0; //默认竖移动速度
- }
- /// <summary>一横一竖线是否相交</summary>
- /// <param name="hx1">横线左</param>
- /// <param name="hx2">横线右</param>
- /// <param name="hy">横线y</param>
- /// <param name="vy1">竖线高</param>
- /// <param name="vy2">竖线低</param>
- /// <param name="vx">竖线x</param>
- /// <returns></returns>
- private bool IsLineCross(double hx1, double hx2, double hy, double vy1, double vy2, double vx)
- {
- return (vy1 < hy) && (vy2 > hy) && (hx1 < vx) && (hx2 > vx);
- }
- private bool IsRectCross(Rectangle r1, Rectangle r2)
- {
- double x11 = (double)r1.GetValue(Canvas.LeftProperty);
- double y11 = (double)r1.GetValue(Canvas.TopProperty);
- double x12 = x11 + r1.Width;
- double y12 = y11 + r1.Height;
- double x21 = (double)r2.GetValue(Canvas.LeftProperty);
- double y21 = (double)r2.GetValue(Canvas.TopProperty);
- double x22 = x21 + r2.Width;
- double y22 = y21 + r2.Height;
- return
- IsLineCross(x11, x12, y11, y21, y22, x21) //横上 竖左
- || IsLineCross(x11, x12, y11, y21, y22, x22) //横上 竖右
- || IsLineCross(x11, x12, y12, y21, y22, x21) //横下 竖左
- || IsLineCross(x11, x12, y12, y21, y22, x22) //横下 竖右
- || IsLineCross(x21, x22, y21, y11, y12, x11) //横上 竖左
- || IsLineCross(x21, x22, y21, y11, y12, x12) //横上 竖右
- || IsLineCross(x21, x22, y22, y11, y12, x11) //横下 竖左
- || IsLineCross(x21, x22, y22, y11, y12, x12) //横下 竖右
- ;
- }
- public Page()
- {
- InitializeComponent();
- Canvas canvas = new Canvas();
- this.LayoutRoot.Children.Add(canvas);
- canvas.Children.Add(this.blackRect);
- canvas.Children.Add(this.whiteRect);
- canvas.Children.Add(this.blueRect1);
- canvas.Children.Add(this.blueRect2);
- canvas.Children.Add(this.blueRect3);
- canvas.Children.Add(this.blueRect4);
- canvas.Children.Add(this.redRect);
- canvas.Children.Add(readme);
- readme.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 100, 180));
- readme.FontSize = 20;
- readme.Text = "在白色范围内移动红色方块,避免碰到蓝色方块";
- this.defaultValue();
- this.timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
- this.timer.Tick += new EventHandler(timer_Tick);
- this.redRect.MouseLeftButtonDown += new MouseButtonEventHandler(redRect_MouseLeftButtonDown);
- this.redRect.MouseMove += new MouseEventHandler(redRect_MouseMove);
- this.redRect.MouseLeftButtonUp += new MouseButtonEventHandler(redRect_MouseLeftButtonUp);
- }
- private void CheckFinish()
- {
- //判断出白界
- double redx = (double)this.redRect.GetValue(Canvas.LeftProperty);
- double redy = (double)this.redRect.GetValue(Canvas.TopProperty);
- double whitex = (double)this.whiteRect.GetValue(Canvas.LeftProperty);
- double whitey = (double)this.whiteRect.GetValue(Canvas.TopProperty);
- //判断红蓝碰撞
- if (redx < whitex || redx + this.redRect.Width > whitex + this.whiteRect.Width) this.Finish();
- if (redy < whitey || redy + this.redRect.Height > whitey + this.whiteRect.Height) this.Finish();
- if (
- IsRectCross(this.redRect, this.blueRect1) ||
- IsRectCross(this.redRect, this.blueRect2) ||
- IsRectCross(this.redRect, this.blueRect3) ||
- IsRectCross(this.redRect, this.blueRect4)
- )
- {
- this.Finish();
- }
- }
- private void Finish()
- {
- this.isMouseDown = false;
- this.redRect.ReleaseMouseCapture();
- MessageBox.Show(string.Format("{0}秒", (DateTime.Now - this.startTime).TotalSeconds));
- this.timer.Stop();
- this.readme.Visibility = Visibility.Visible;
- this.defaultValue();
- }
- //4个蓝色方块的移动方向
- private int fx1 = 1; private int fy1 = 1;
- private int fx2 = -1; private int fy2 = 1;
- private int fx3 = 1; private int fy3 = -1;
- private int fx4 = -1; private int fy4 = -1;
- private double xs;
- private double ys;
- void timer_Tick(object sender, EventArgs e)
- {
- double v1x = (double)this.blueRect1.GetValue(Canvas.LeftProperty);
- double v1y = (double)this.blueRect1.GetValue(Canvas.TopProperty);
- double v2x = (double)this.blueRect2.GetValue(Canvas.LeftProperty);
- double v2y = (double)this.blueRect2.GetValue(Canvas.TopProperty);
- double v3x = (double)this.blueRect3.GetValue(Canvas.LeftProperty);
- double v3y = (double)this.blueRect3.GetValue(Canvas.TopProperty);
- double v4x = (double)this.blueRect4.GetValue(Canvas.LeftProperty);
- double v4y = (double)this.blueRect4.GetValue(Canvas.TopProperty);
- v1x += xs * fx1; v1y += ys * fy1;
- v2x += xs * fx2; v2y += ys * fy2;
- v3x += xs * fx3; v3y += ys * fy3;
- v4x += xs * fx4; v4y += ys * fy4;
- if (v1x < 0 || v1x > this.blackRect.Width - this.blueRect1.Width) fx1 *= -1;
- if (v1y < 0 || v1y > this.blackRect.Height - this.blueRect1.Height) fy1 *= -1;
- if (v2x < 0 || v2x > this.blackRect.Width - this.blueRect2.Width) fx2 *= -1;
- if (v2y < 0 || v2y > this.blackRect.Height - this.blueRect2.Height) fy2 *= -1;
- if (v3x < 0 || v3x > this.blackRect.Width - this.blueRect3.Width) fx3 *= -1;
- if (v3y < 0 || v3y > this.blackRect.Height - this.blueRect3.Height) fy3 *= -1;
- if (v4x < 0 || v4x > this.blackRect.Width - this.blueRect4.Width) fx4 *= -1;
- if (v4y < 0 || v4y > this.blackRect.Height - this.blueRect4.Height) fy4 *= -1;
- this.blueRect1.SetValue(Canvas.LeftProperty, v1x);
- this.blueRect1.SetValue(Canvas.TopProperty, v1y);
- this.blueRect2.SetValue(Canvas.LeftProperty, v2x);
- this.blueRect2.SetValue(Canvas.TopProperty, v2y);
- this.blueRect3.SetValue(Canvas.LeftProperty, v3x);
- this.blueRect3.SetValue(Canvas.TopProperty, v3y);
- this.blueRect4.SetValue(Canvas.LeftProperty, v4x);
- this.blueRect4.SetValue(Canvas.TopProperty, v4y);
- //每次速度提升1/1000
- this.xs *= 1.001;
- this.ys *= 1.001;
- this.CheckFinish();
- }
- private double beginX;
- private double beginY;
- private bool isMouseDown;
- void redRect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- this.beginX = e.GetPosition(null).X;
- this.beginY = e.GetPosition(null).Y;
- this.isMouseDown = true;
- ((UIElement)sender).CaptureMouse();
- if (this.timer.IsEnabled == false)
- {
- this.timer.Start(); //开始移动
- this.readme.Visibility = Visibility.Collapsed;
- this.startTime = DateTime.Now;
- }
- }
- void redRect_MouseMove(object sender, MouseEventArgs e)
- {
- if (isMouseDown == true)
- {
- double currX = e.GetPosition(null).X;
- double currY = e.GetPosition(null).Y;
- double currLeft = (double)((UIElement)sender).GetValue(Canvas.LeftProperty);
- double currTop = (double)((UIElement)sender).GetValue(Canvas.TopProperty);
- ((UIElement)sender).SetValue(Canvas.LeftProperty, currLeft + currX - beginX);
- ((UIElement)sender).SetValue(Canvas.TopProperty, currTop + currY - beginY);
- this.beginX = currX;
- this.beginY = currY;
- }
- }
- void redRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- this.isMouseDown = false;
- ((UIElement)sender).ReleaseMouseCapture();
- }
- }
- }