silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SilverLight练习,移动方块的小游戏(源代码)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

测试页面在这里,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.Wi

测试页面在这里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 @R_874_7538@me 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));
  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));
  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, 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, 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, 0xFF));
  54.             this.redRect.Height = 41;
  55.             this.redRect.Width = 41;
  56.             this.redRect.SETVALue(Canvas.LeftProperty, 204.0);
  57.             this.redRect.SETVALue(Canvas.TopProperty, 202.0);
  58.             this.redRect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0));
  59.             this.xs = 3.0;  //认横移动速度
  60.             this.ys = 2.0;  //认竖移动速度
  61.         }
  62.         /// <sumMary>一横一竖线是否相交</sumMary>
  63.         /// <param NAME="hx1">横线左</param>
  64.         /// <param NAME="hx2">横线右</param>
  65.         /// <param NAME="hy">横线y</param>
  66.         /// <param NAME="vy1">竖线高</param>
  67.         /// <param NAME="vy2">竖线低</param>
  68.         /// <param NAME="vx">竖线x</param>
  69.         /// <returns></returns>
  70.         private bool IsLineCross(double hx1, double hx2, double hy, double vy1, double vy2, double vX)
  71.         {
  72.             return (vy1 < hy) && (vy2 > hy) && (hx1 < vX) && (hx2 > vX);
  73.         }
  74.         private bool IsRectCross(Rectangle r1, rectangle r2)
  75.         {
  76.             double x11 = (double)r1.GetValue(Canvas.LeftProperty);
  77.             double y11 = (double)r1.GetValue(Canvas.TopProperty);
  78.             double x12 = x11 + r1.Width;
  79.             double y12 = y11 + r1.Height;
  80.             double x21 = (double)r2.GetValue(Canvas.LeftProperty);
  81.             double y21 = (double)r2.GetValue(Canvas.TopProperty);
  82.             double x22 = x21 + r2.Width;
  83.             double y22 = y21 + r2.Height;
  84.             return
  85.                 IsLineCross(x11, x12, y11, y21, y22, x21)       //横上 竖左
  86.                 || IsLineCross(x11, x22)    //横上 竖右
  87.                 || IsLineCross(x11, y12, x21)    //横下 竖左
  88.                 || IsLineCross(x11, x22)    //横下 竖右
  89.                 || IsLineCross(x21, x22, x11)    //横上 竖左
  90.                 || IsLineCross(x21, x12)    //横上 竖右
  91.                 || IsLineCross(x21, x11)    //横下 竖左
  92.                 || IsLineCross(x21, x12)    //横下 竖右
  93.                 ;
  94.         }
  95.         public Page()
  96.         {
  97.             initializeComponent();
  98.             Canvas canvas = new Canvas();
  99.             this.LayoutRoot.Children.Add(canvas);
  100.             canvas.Children.Add(this.blackRect);
  101.             canvas.Children.Add(this.whiteRect);
  102.             canvas.Children.Add(this.blueRect1);
  103.             canvas.Children.Add(this.blueRect2);
  104.             canvas.Children.Add(this.blueRect3);
  105.             canvas.Children.Add(this.blueRect4);
  106.             canvas.Children.Add(this.redRect);
  107.             canvas.Children.Add(readmE);
  108.             readme.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 100, 180));
  109.             readme.FontSize = 20;
  110.             readme.Text = "在白色范围内移动红色方块,避免碰到蓝色方块";
  111.             this.DefaultValue();
  112.             this.timer.Interval = new TimeSpan(0, 1);
  113.             this.timer.Tick += new EventHandler(timer_Tick);
  114.             this.redRect.MouSELEftButtonDown += new MouseButtonEventHandler(redRect_MouSELEftButtonDown);
  115.             this.redRect.MouseMove += new MouseEventHandler(redRect_MouseMovE);
  116.             this.redRect.MouSELEftButtonUp += new MouseButtonEventHandler(redRect_MouSELEftButtonUp);
  117.         }
  118.         private void checkFinish()
  119.         {
  120.             //判断出白界
  121.             double redx = (double)this.redRect.GetValue(Canvas.LeftProperty);
  122.             double redy = (double)this.redRect.GetValue(Canvas.TopProperty);
  123.             double whitex = (double)this.whiteRect.GetValue(Canvas.LeftProperty);
  124.             double whitey = (double)this.whiteRect.GetValue(Canvas.TopProperty);
  125.             //判断红蓝碰撞
  126.             if (redx < whitex || redx + this.redRect.Width > whitex + this.whiteRect.Width) this.Finish();
  127.             if (redy < whitey || redy + this.redRect.Height > whitey + this.whiteRect.Height) this.Finish();
  128.             if (
  129.                 IsRectCross(this.redRect, this.blueRect1) ||
  130.                 IsRectCross(this.redRect, this.blueRect2) ||
  131.                 IsRectCross(this.redRect, this.blueRect3) ||
  132.                 IsRectCross(this.redRect, this.blueRect4)
  133.                 )
  134.             {
  135.                 this.Finish();
  136.             }
  137.         }
  138.         private void Finish()
  139.         {
  140.             this.isMouseDown = false;
  141.             this.redRect.ReleaseMouseCapture();
  142.             messageBox.Show(String.Format("{0}秒", (@R_874_7538@me.Now - this.startTimE).@R_306_10586@lSeconds));
  143.             this.timer.Stop();
  144.             this.readme.Visibility = Visibility.Visible;
  145.             this.DefaultValue();
  146.         }
  147.         //4个蓝色方块的移动方向
  148.         private int fx1 = 1; private int fy1 = 1;
  149.         private int fx2 = -1; private int fy2 = 1;
  150.         private int fx3 = 1; private int fy3 = -1;
  151.         private int fx4 = -1; private int fy4 = -1;
  152.         private double xs;
  153.         private double ys;
  154.         void timer_Tick(object sender, EventArgs E)
  155.         {
  156.             double v1x = (double)this.blueRect1.GetValue(Canvas.LeftProperty);
  157.             double v1y = (double)this.blueRect1.GetValue(Canvas.TopProperty);
  158.             double v2x = (double)this.blueRect2.GetValue(Canvas.LeftProperty);
  159.             double v2y = (double)this.blueRect2.GetValue(Canvas.TopProperty);
  160.             double v3x = (double)this.blueRect3.GetValue(Canvas.LeftProperty);
  161.             double v3y = (double)this.blueRect3.GetValue(Canvas.TopProperty);
  162.             double v4x = (double)this.blueRect4.GetValue(Canvas.LeftProperty);
  163.             double v4y = (double)this.blueRect4.GetValue(Canvas.TopProperty);
  164.             v1x += xs * fx1; v1y += ys * fy1;
  165.             v2x += xs * fx2; v2y += ys * fy2;
  166.             v3x += xs * fx3; v3y += ys * fy3;
  167.             v4x += xs * fx4; v4y += ys * fy4;
  168.             if (v1x < 0 || v1x > this.blackRect.Width - this.blueRect1.Width) fx1 *= -1;
  169.             if (v1y < 0 || v1y > this.blackRect.Height - this.blueRect1.Height) fy1 *= -1;
  170.             if (v2x < 0 || v2x > this.blackRect.Width - this.blueRect2.Width) fx2 *= -1;
  171.             if (v2y < 0 || v2y > this.blackRect.Height - this.blueRect2.Height) fy2 *= -1;
  172.             if (v3x < 0 || v3x > this.blackRect.Width - this.blueRect3.Width) fx3 *= -1;
  173.             if (v3y < 0 || v3y > this.blackRect.Height - this.blueRect3.Height) fy3 *= -1;
  174.             if (v4x < 0 || v4x > this.blackRect.Width - this.blueRect4.Width) fx4 *= -1;
  175.             if (v4y < 0 || v4y > this.blackRect.Height - this.blueRect4.Height) fy4 *= -1;
  176.             this.blueRect1.SETVALue(Canvas.LeftProperty, v1X);
  177.             this.blueRect1.SETVALue(Canvas.TopProperty, v1y);
  178.             this.blueRect2.SETVALue(Canvas.LeftProperty, v2X);
  179.             this.blueRect2.SETVALue(Canvas.TopProperty, v2y);
  180.             this.blueRect3.SETVALue(Canvas.LeftProperty, v3X);
  181.             this.blueRect3.SETVALue(Canvas.TopProperty, v3y);
  182.             this.blueRect4.SETVALue(Canvas.LeftProperty, v4X);
  183.             this.blueRect4.SETVALue(Canvas.TopProperty, v4y);
  184.             //每次速度提升1/1000
  185.             this.xs *= 1.001;
  186.             this.ys *= 1.001;
  187.             this.checkFinish();
  188.         }
  189.         private double beginX;
  190.         private double beginY;
  191.         private bool isMouseDown;
  192.         void redRect_MouSELEftButtonDown(object sender, MouseButtonEventArgs E)
  193.         {
  194.             this.beginX = e.GetPosition(null).X;
  195.             this.beginY = e.GetPosition(null).Y;
  196.             this.isMouseDown = true;
  197.             ((UIElement)sender).CaptureMouse();
  198.             if (this.timer.IsEnabled == false)
  199.             {
  200.                 this.timer.Start();  //开始移动
  201.                 this.readme.Visibility = Visibility.Collapsed;
  202.                 this.startTime = @R_874_7538@me.Now;
  203.             }
  204.         }
  205.         void redRect_MouseMove(object sender, MouseEventArgs E)
  206.         {
  207.             if (isMouseDown == true)
  208.             {
  209.                 double currX = e.GetPosition(null).X;
  210.                 double currY = e.GetPosition(null).Y;
  211.                 double currLeft = (double)((UIElement)sender).GetValue(Canvas.LeftProperty);
  212.                 double currTop = (double)((UIElement)sender).GetValue(Canvas.TopProperty);
  213.                 ((UIElement)sender).SETVALue(Canvas.LeftProperty, currLeft + currX - beginX);
  214.                 ((UIElement)sender).SETVALue(Canvas.TopProperty, currTop + currY - beginY);
  215.                 this.beginX = currX;
  216.                 this.beginY = currY;
  217.             }
  218.         }
  219.         void redRect_MouSELEftButtonUp(object sender, MouseButtonEventArgs E)
  220.         {
  221.             this.isMouseDown = false;
  222.             ((UIElement)sender).ReleaseMouseCapture();
  223.         }
  224.     }
  225. }

大佬总结

以上是大佬教程为你收集整理的SilverLight练习,移动方块的小游戏(源代码)全部内容,希望文章能够帮你解决SilverLight练习,移动方块的小游戏(源代码)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。