使用WPF编写游戏(一)

1.

<Window x:Class="WPFGameTutorial.Window6"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFGame">

    <Canvas x:Name="Carrier" Width="800" Height="600" Background="Silver"

    MouseLeftButtonDown="Carrier_MouseLeftButtonDown"/>

</Window>

 

2.

public partial class Window6 : Window {

        int count = 1;

        Image Spirit;

        Storyboard storyboard;

        public Window6() {

            InitializeComponent();

            InitMap();

            Spirit = new Image();

            Spirit.Width = 150;

            Spirit.Height = 150;

            Carrier.Children.Add(Spirit);

            Canvas.SetLeft(Spirit, 0);

            Canvas.SetTop(Spirit, 0);

//第一句申明一个界面计时器DispatcherTimer ,并且设置其线程优先级别为Normal,这是标准设置,

你可以根据你自己的需求进行更改,一共10个级别。

              //第二句注册Tick 事件,也就是计时器间隔触发的事件。

              //第三句设置Tick 事件的间隔,可以有很多方式,我使用的是TimeSpan.FromMilliseconds(),即间隔

单位为毫秒。

              //第四句启动线程。

            DispatcherTimer dispatcherTimer = new DispatcherTimer();

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(150);

            dispatcherTimer.Start();

        }

        

        //初始化地图

        Image Map = new Image();

        private void InitMap()

        {

            Map.Width = 1750;

            Map.Height = 1440;

            Map.Source = new BitmapImage((new Uri(@"Map/Map4.jpg", UriKind.Relative)));

            Carrier.Children.Add(Map);

            //将地图设置在中央

            Canvas.SetLeft(Map, -320);

            Canvas.SetTop(Map, -200);

            Map.SetValue(Canvas.ZIndexProperty, -1);

        }

        private void dispatcherTimer_Tick(object sender, EventArgs e) {

            // 主角始终为跑步状态,那么能否当主角移动到目的地后即变成站立状态呢?

                 // 方法比较多,比如在满足以下条件的情况下,将主角的图片源切换成站立系列帧图片即可。

            if (storyboard==null || storyboard != null && storyboard.GetCurrentTime() ==

TimeSpan.FromSeconds(1))

{

                Spirit.Source = new BitmapImage((new Uri(@"Player/1.png",UriKind.Relative)));

                return;

            }

            Spirit.Source = new BitmapImage((new Uri(@"Player/" + count + ".png", UriKind.Relative)));

            count = count == 7 ? 0 : count + 1;

        }

 

        private void Carrier_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {

            Point p = e.GetPosition(Carrier);//获取在Carrier上单击时的鼠标位置

            Move(p);

        }

 

        private void Move(Point p) {

            //创建移动动画

            storyboard = new Storyboard();

            //创建X轴方向动画

            //:如果没有X轴方向动画,则人物只能纵向(沿Y)移动

            DoubleAnimation doubleAnimation= new DoubleAnimation(

              Canvas.GetLeft(Spirit),

              p.X,

              new Duration(TimeSpan.FromSeconds(1))

            );

            Storyboard.SetTarget(doubleAnimation, Spirit);//向故事板加入动画和动画目标

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)"));

            storyboard.Children.Add(doubleAnimation);

            //创建Y轴方向动画

            doubleAnimation = new DoubleAnimation(

              Canvas.GetTop(Spirit),

              p.Y,

              new Duration(TimeSpan.FromSeconds(1))

            );

            Storyboard.SetTarget(doubleAnimation, Spirit);

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)"));

            storyboard.Children.Add(doubleAnimation);

            //动画播放

            storyboard.Begin();

        }

}

 

:如果人物是一张图片的话

public partial class Window5 : Window {

        Image Spirit;

        int count = 1;

        public Window5() {

            InitializeComponent();

            Spirit = new Image();

            Spirit.Width = 150;

            Spirit.Height = 150;

            Carrier.Children.Add(Spirit);

            DispatcherTimer dispatcherTimer = new DispatcherTimer();

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(150);

            dispatcherTimer.Start();

        }

        private void dispatcherTimer_Tick(object sender, EventArgs e) {

            Spirit.Source = cutImage(@"Player/PlayerMagic.png", count * 150, 0, 150, 150);

            count = count == 9 ? 0 : count + 1;

        }

        /// <summary>

        /// 截取图片

        /// </summary>

        /// <param name="imgaddress">文件名(包括地址+扩展名)</param>

        /// <param name="x">左上角点X</param>

        /// <param name="y">左上角点Y</param>

        /// <param name="width">截取的图片宽</param>

        /// <param name="height">截取的图片高</param>

        /// <returns>截取后图片数据源</returns>

        private BitmapSource cutImage(string imgaddress, int x, int y, int width, int height) {

            return new CroppedBitmap(

                BitmapFrame.Create(new Uri(imgaddress, UriKind.Relative)),

                new Int32Rect(x, y, width, height)

            );

        }

}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值