测试页面在这里,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)); 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)); 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, 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, 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, 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, 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, x22) //横上 竖右 || IslineCross(x11, y12, x21) //横下 竖左 || IslineCross(x11, x22) //横下 竖右 || IslineCross(x21, x22, x11) //横上 竖左 || IslineCross(x21, x12) //横上 竖右 || IslineCross(x21, x11) //横下 竖左 || IslineCross(x21, 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, 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(); } } } 总结
以上是内存溢出为你收集整理的SilverLight练习,移动方块的小游戏(源代码)全部内容,希望文章能够帮你解决SilverLight练习,移动方块的小游戏(源代码)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)