WPF游戏点击要移动在C#编写 [英] WPF game with click to move written in C#

查看:189
本文介绍了WPF游戏点击要移动在C#编写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图标签的网格冒险游戏移动到我的鼠标的位置,喜欢运动。理想情况下我会删除,并重新绘制他们的方式出现。但是,现在我只是想弄清楚如何INT到厚度或pointtoscreen转换



到目前为止,我有:

  player.XMove =(int)的Mouse.GetPosition(Application.Current.MainWindow).X; 
player.YMove =(int)的Mouse.GetPosition(Application.Current.MainWindow).Y;



我已经试过转换player.XMove和player.YMove与厚度和储存它作为保证金带标签的网格。我遇到了一些麻烦的转换。那么,有没有人能提供与转换的帮助?此外,有没有更好的标签/格属性点到



编辑转换:图像链接添加



http://i1118.photobucket.com/albums/k608/sealclubberr/clickToMove_zps51f2359f。 JPG



修改:增加了当前的代码和XAML。我尚未有机会与它摆弄



这是我当前的代码是这样的:

 公共类用户
{
私人诠释美联储;
公众诠释美联储
{喂
得到
{
的回报;
}

{
美联储= 5;
}
}

私人布尔可见;
公共BOOL可见
{
得到
{
返回可见;
}

{
可见=价值;
}
}

私人诠释XMOVE;
公众诠释XMOVE
{
得到
{
返回XMOVE;
}

{
XMOVE =价值;
}
}

私人诠释yMove偏移;
公众诠释yMove偏移
{
得到
{
返回yMove偏移;
}

{
yMove偏移=价值;
}
}

}



我会最终加入有一个使用用户类和位置类。(不贴。)之间的



我在主窗口代码:

 公共部分类主窗口:窗口
{
的用户球员=新用户();
ThicknessConverter周长=新ThicknessConverter();
公共主窗口()
{
的InitializeComponent();

}

私人无效Hansel_IsVisibleChanged(对象发件人,DependencyPropertyChangedEventArgs E)
{
Hansel.Visibility = Visibility.Visible;
}

私人无效Hansel_MouseLeftButtonDown(对象发件人,MouseButtonEventArgs E)
{
player.XMove =(int)的Mouse.GetPosition(Application.Current.MainWindow)。 X;
player.YMove =(int)的Mouse.GetPosition(Application.Current.MainWindow).Y;

}

私人无效southRectangle_IsHitTestVisibleChanged(对象发件人,DependencyPropertyChangedEventArgs E)
{

}


}

我也在努力的bool转换为可见,但遇到了一个误差。XAML中未能找到该转换器命名空间,这是有



在XAML是这样的:

 <窗口x:类=HanselAndGretalDisplay.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:地方=CLR的命名空间:HanselAndGretalDisplay
标题=主窗口的Horizo​​ntalAlignment =弹力 VerticalAlignment =拉伸>
<网格和GT;
< Grid.Resources>
<局部:BoolToVisibleOrHidden X:键=BoolToVisConverter/>
< /Grid.Resources>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=154,94,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=154,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,94,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=104,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=104,94,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=104,44,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,44,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=154,44,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=179,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=79,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,119,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,19,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=698,94,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=698,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,94,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,94,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,44,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,44,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=698,44,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=723,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=623,69,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,119,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,19,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015 BorderBrush =黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=154,425,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=154,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,425,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=104,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=104,425,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=104,375,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,375,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=154,375,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=179,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=79,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,450,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=129,350,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,425,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,425,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=623,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=623,425,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=623,375,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,375,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=673,375,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=698,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=598,400,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,450,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=648,350,0,0VerticalAlignment =评出的前景=#FF1B4015WIDTH =25背景=#FF1B4015BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<电网X:NAME =汉塞尔IsVisibleChanged在=Hansel_IsVisibleChanged的MouseLeftButtonDown =Hansel_MouseLeftButtonDown>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=398,218,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=398,243,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=398,268,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=373,243,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=423,243,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=413,293,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=383,293,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=439,268,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<标签内容=标签的Horizo​​ntalAlignment =左保证金=353,268,0,0VerticalAlignment =评出的前景=#FFFFE2C5WIDTH =25背景=#FFFFE2C5BorderBrush = 黑了borderThickness =3HEIGHT =25/>
<矩形X:NAME =southRectangle的Horizo​​ntalAlignment =左身高=79保证金=209,425,0,0VerticalAlignment =评出的WIDTH =383IsHitTestVisibleChanged在=southRectangle_IsHitTestVisibleChanged/> ;
<矩形X:NAME =northRectangle的Horizo​​ntalAlignment =左身高=79保证金=209,0,0,0VerticalAlignment =评出的WIDTH =412/>
<矩形X:NAME =westRectangle的Horizo​​ntalAlignment =左HEIGHT =202保证金=0,145,0,0VerticalAlignment =评出的WIDTH =133/>
<矩形X:NAME =eastRectangle的Horizo​​ntalAlignment =左HEIGHT =202保证金=627,145,0,0VerticalAlignment =评出的WIDTH =133/>
< /网格和GT;
< /网格和GT;
< /窗GT;

如果有编程方式生成XAML对象,并赋予它们到初始位置,这将是理想的一种简单的方法,对于这个项目从长远来看,但现在我不是太担心这一点。


解决方案

 <窗口x:类=MiscSamples.ClickToMove
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X = http://schemas.microsoft.com/winfx/2006/xaml
标题=ClickToMoveHEIGHT =300WIDTH =300>
<的ItemsControl的ItemsSource ={结合}PreviewMouseDown =ItemsControl_PreviewMouseDown
背景=#05FFFFFF>
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
<帆布IsItemsHost =真/>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>
< ItemsControl.ItemContainerStyle>
<风格的TargetType =ContentPresenter>
< setter属性=Canvas.LeftVALUE ={结合X}/>
< setter属性=Canvas.TopVALUE ={结合Y}」/>
< /样式和GT;
< /ItemsControl.ItemContainerStyle>
< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<矩形行程=黑StrokeThickness =2填写=蓝
高度=20WIDTH =20/>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>
< /窗GT;

代码背后:

 公共部分类ClickToMove:窗口
{
公开名单< MovableObject>对象{搞定;组; }

公共ClickToMove()
{
的InitializeComponent();

=物体新的List< MovableObject>
{
新MovableObject(){X = 100,Y = 100}
};

= DataContext的对象;
}

私人无效ItemsControl_PreviewMouseDown(对象发件人,MouseButtonEventArgs E)
{
变种位置= e.GetPosition(本);
Objects.First()MoveToPosition(position.X,position.Y)。
}
}



Item类:

 公共类MovableObject:INotifyPropertyChanged的
{
私人双_x;
公共双X
{
{返回_x; }

{
_x =价值;
OnPropertyChanged(X);
}
}

私人双_y;
公共双Y
{
{返回_y; }

{
_y =价值;
OnPropertyChanged(Y);
}
}

私人System.Threading.Timer MoveTimer;

私人双DestinationX;
私人双DestinationY;

公共无效MoveToPosition(双X,双Y)
{
DestinationX = X;
DestinationY = Y;

如果(MoveTimer!= NULL)
MoveTimer.Dispose();

MoveTimer =新的Timer(O => MoveStep(),NULL,0,10);
}

私人无效MoveStep()
{
如果(Math.Abs​​(X - DestinationX)→5)
{
如果(X< DestinationX)
X = X + 5;
否则如果(X> DestinationX)
X = X-5;
}

如果(Math.Abs​​(Y - DestinationY)→5)
{
如果(Y< DestinationY)
Y = Y + 5;
否则如果(Y> DestinationY)
Y = Y - 5;
}
}

公共事件PropertyChangedEventHandler的PropertyChanged;

受保护的虚拟无效OnPropertyChanged(字符串propertyName的)
{
Application.Current.Dispatcher.BeginInvoke((动作)(()=>
{
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(!=处理空值)处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}));

}
}


I'm attempting to move a grid of labels to the position of my mouse, like movement in an adventure game. Ideally I'd delete and redraw them on the way there. But, for now I just want to figure out how to convert int to thickness or pointtoscreen.

So far I have:

player.XMove =  (int)Mouse.GetPosition(Application.Current.MainWindow).X;
player.YMove = (int)Mouse.GetPosition(Application.Current.MainWindow).Y;

I've tried converting player.XMove and player.YMove to thickness and storing it as a margin for a grid with labels. I ran into some trouble with converting. So, is anyone able to provide help with the conversion? Also, is there a better label/grid attribute to convert the point to

Edit: Image link added.

http://i1118.photobucket.com/albums/k608/sealclubberr/clickToMove_zps51f2359f.jpg

Edit: Added Current code and XAML. I haven't yet had a chance to fiddle with it.

This is what my current code looks like:

public class User
    {
        private int fed;
        public int Fed
        {
            get
            {
                return fed;
            }
            set
            {
                fed = 5;
            }
        }

        private bool visible;
        public bool Visible
        {
            get
            {
                return visible;
            }
            set
            {
                visible = value;
            }
        }

        private int xMove;
        public int XMove
        {
            get
            {
                return xMove;
            }
            set
            {
                xMove = value;
            }
        }

        private int yMove;
        public int YMove
        {
            get
            {
                return yMove;
            }
            set
            {
                yMove = value;
            }
        }

    }

I'll eventually add Has A or Uses A between the user class and location class.(Not posted.)

My code in Main Window:

public partial class MainWindow : Window
    {
        User player = new User();
        ThicknessConverter perimeter = new ThicknessConverter();
        public MainWindow()
        {
            InitializeComponent();

        }

        private void Hansel_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
             Hansel.Visibility = Visibility.Visible;
        }

        private void Hansel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            player.XMove =  (int)Mouse.GetPosition(Application.Current.MainWindow).X;
            player.YMove = (int)Mouse.GetPosition(Application.Current.MainWindow).Y;

        }

        private void southRectangle_IsHitTestVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }


    }

I'm also working on converting bools to visibility, but ran into an error with the XAML not being able to find the converter namespace, which was there.

The XAML looks like:

<Window x:Class="HanselAndGretalDisplay.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:HanselAndGretalDisplay"
        Title="MainWindow" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid>
        <Grid.Resources>
            <local:BoolToVisibleOrHidden x:Key="BoolToVisConverter"/>
        </Grid.Resources>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="179,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="79,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,119,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,19,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,94,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,44,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="723,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,69,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,119,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,19,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="104,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="154,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="179,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="79,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,450,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="129,350,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,425,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="623,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="673,375,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="698,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="598,400,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,450,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="648,350,0,0" VerticalAlignment="Top" Foreground="#FF1B4015" Width="25" Background="#FF1B4015" BorderBrush="Black" BorderThickness="3" Height="25"/>
        <Grid x:Name="Hansel" IsVisibleChanged="Hansel_IsVisibleChanged" MouseLeftButtonDown="Hansel_MouseLeftButtonDown">
            <Label Content="Label" HorizontalAlignment="Left" Margin="398,218,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="398,243,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="398,268,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="373,243,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="423,243,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="413,293,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="383,293,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="439,268,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Label Content="Label" HorizontalAlignment="Left" Margin="353,268,0,0" VerticalAlignment="Top" Foreground="#FFFFE2C5" Width="25" Background="#FFFFE2C5" BorderBrush="Black" BorderThickness="3" Height="25"/>
            <Rectangle x:Name="southRectangle" HorizontalAlignment="Left" Height="79" Margin="209,425,0,0" VerticalAlignment="Top" Width="383" IsHitTestVisibleChanged="southRectangle_IsHitTestVisibleChanged"/>
            <Rectangle x:Name="northRectangle" HorizontalAlignment="Left" Height="79" Margin="209,0,0,0" VerticalAlignment="Top" Width="412"/>
            <Rectangle x:Name="westRectangle" HorizontalAlignment="Left" Height="202" Margin="0,145,0,0" VerticalAlignment="Top" Width="133"/>
            <Rectangle x:Name="eastRectangle" HorizontalAlignment="Left" Height="202" Margin="627,145,0,0" VerticalAlignment="Top" Width="133"/>
        </Grid>
    </Grid>
</Window>

If there's an easy way of programmatically generating XAML objects and assigning them to initial location that would be ideal, for the long run with this project, but for now I'm not too worried about that.

解决方案

<Window x:Class="MiscSamples.ClickToMove"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ClickToMove" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding}" PreviewMouseDown="ItemsControl_PreviewMouseDown"
                  Background="#05FFFFFF">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Stroke="Black" StrokeThickness="2" Fill="Blue"
                           Height="20" Width="20"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

Code Behind:

  public partial class ClickToMove : Window
    {
        public List<MovableObject> Objects { get; set; } 

        public ClickToMove()
        {
            InitializeComponent();

            Objects = new List<MovableObject>
                {
                    new MovableObject() {X = 100, Y = 100}
                };

            DataContext = Objects;
        }

        private void ItemsControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var position = e.GetPosition(this);
            Objects.First().MoveToPosition(position.X, position.Y);
        }
    }

Item class:

 public class MovableObject: INotifyPropertyChanged
    {
        private double _x;
        public double X
        {
            get { return _x; }
            set
            {
                _x = value;
                OnPropertyChanged("X");
            }
        }

        private double _y;
        public double Y
        {
            get { return _y; }
            set
            {
                _y = value;
                OnPropertyChanged("Y");
            }
        }

        private System.Threading.Timer MoveTimer;

        private double DestinationX;
        private double DestinationY;

        public void MoveToPosition(double x, double y)
        {
            DestinationX = x;
            DestinationY = y;

            if (MoveTimer != null)
                MoveTimer.Dispose();

            MoveTimer = new Timer(o => MoveStep(), null, 0, 10);
        }

        private void MoveStep()
        {
            if (Math.Abs(X - DestinationX) > 5)
            {
                if (X < DestinationX)
                    X = X+5;
                else if (X > DestinationX)
                    X = X-5;    
            }

            if (Math.Abs(Y - DestinationY) > 5)
            {
                if (Y < DestinationY)
                    Y = Y + 5;
                else if (Y > DestinationY)
                    Y = Y - 5;    
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    PropertyChangedEventHandler handler = PropertyChanged;
                    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));        
                }));

        }
    }

这篇关于WPF游戏点击要移动在C#编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆