WPF游戏编程01--物体运动

 <Canvas x:Name="container" Width="800" Height="600" Background="Silver"  MouseMove="container_MouseMove" MouseLeftButtonDown="container_MouseLeftButtonDown">
            
        </Canvas>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WPFApplication
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DrawRectangle();
            DrawCircle();
        }

        // 绘制矩形
        private void DrawRectangle()
        {
            rect = new Rectangle();

            rect.Width = 50;
            rect.Height = 50;

            rect.RadiusX = 5;
            rect.RadiusY = 5;

            rect.Fill = new SolidColorBrush(Colors.Beige);

            Canvas.SetLeft(rect, 400);
            Canvas.SetTop(rect, 300);

            container.Children.Add(rect);
        }

        private void container_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point point = e.GetPosition(container);

            Storyboard storyboard = new Storyboard();

            // x
            DoubleAnimation doubleAnimation = new DoubleAnimation(Canvas.GetLeft(rect), point.X, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTarget(doubleAnimation, rect);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)"));
            storyboard.Children.Add(doubleAnimation);

            // y
            doubleAnimation = new DoubleAnimation(Canvas.GetTop(rect), point.Y, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTarget(doubleAnimation, rect);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)"));
            storyboard.Children.Add(doubleAnimation);

            if (!Resources.Contains("rectAnimation"))
            {
                Resources.Add("rectAnimation", doubleAnimation);
            }

            storyboard.Begin();
        }

        private void DrawCircle()
        {
            circle = new Ellipse();
            circle.Width = 50;
            circle.Height = 50;

            circle.Fill = new SolidColorBrush(Colors.Bisque);

            Canvas.SetLeft(circle,0);
            Canvas.SetTop(circle,0);
            container.Children.Add(circle);
        }

        private void container_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = e.GetPosition(container);

            Storyboard storyboard = new Storyboard();

            // x
            DoubleAnimation animation = new DoubleAnimation(Canvas.GetLeft(circle), p.X, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTarget(animation, circle);
            Storyboard.SetTargetProperty(animation,new PropertyPath("(Canvas.Left)"));
            storyboard.Children.Add(animation);

            // y
            animation = new DoubleAnimation(Canvas.GetTop(circle), p.Y, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTarget(animation,circle);
            Storyboard.SetTargetProperty(animation,new PropertyPath("(Canvas.Top)"));
            storyboard.Children.Add(animation);

            if(!Resources.Contains("circleAnimation"))
            {
                Resources.Add("circleAnimation",animation);
            }

            storyboard.Begin();


        }

        private Rectangle rect;
        private Ellipse circle;
    }
}


  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

shuangyan5230

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值