silverlight
发布时间:2022-05-04 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SilverLight练习,移动方块的小游戏(源代码),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
测试页面在这里,
http://silverlight.
service
s.live.com/invoke/84388/MoveBlock/iframe.html 代码如下: using System; using Syste
m.Windows; using Syste
m.Window
s.Controls; using Syste
m.Window
s.Input; using Syste
m.Wi
测试页面在这里,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 @R_874_7538@me 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;
- }
-
-
-
-
-
-
-
-
-
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}秒", (@R_874_7538@me.Now - this.startTimE).@R_306_10586@lSeconds));
-
this.timer.Stop();
-
this.readme.Visibility = Visibility.Visible;
-
this.DefaultValue();
- }
-
-
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);
-
-
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 = @R_874_7538@me.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练习,移动方块的小游戏(源代码)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。