C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(十二)神奇的副本地图
2009-06-22 07:08:39 来源:WEB开发网那么在主角移动的时候,它的X,Y坐标属性是时时更新的,因此我们需要一个线程去捕获它,并且在此线程中时时判断主角是否采在了黑色点上(障碍物)。那么这里我采用了第二节中所讲到的CompositionTarget界面线程,注册了该线程dispatcherTimer1_Tick事件后,接下来就进入关键代码了:
private void Carrier_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
Point p = e.GetPosition(Carrier);
//假如点到的地方不是障碍物
if (pickColor(Deeper, (int)p.X, (int)p.Y) != Colors.Black) {
target = p;
NormalMove(p); //直线移动
//AStarMove(p); //A*寻路移动
}
}
BitmapSource Deeper = new BitmapImage((new Uri(@"Deeper.jpg", UriKind.Relative))); //设置地图副本
int X, Y; //主角当前的窗口真实坐标(非缩放)
Point target; //主角移动的最终目的
private void dispatcherTimer1_Tick(object sender, EventArgs e) {
X = Convert.ToInt32(Canvas.GetLeft(Spirit) + SpiritCenterX * GridSize);
Y = Convert.ToInt32(Canvas.GetTop(Spirit) + SpiritCenterY * GridSize);
//message.Text = "坐标:" + X + " " + Y;
message.Text = pickColor(Deeper, X, Y).ToString();
//假如碰到障碍物则采用A*寻路
if (pickColor(Deeper, X, Y) == Colors.Black) {
AStarMove(target);
} else if (pickColor(Deeper, X, Y) == Colors.Yellow) {
//假如是传送点则条到坐标(200,20)
storyboard.Stop();
Canvas.SetLeft(Spirit, 200 - SpiritCenterX * GridSize);
Canvas.SetTop(Spirit, 20 - SpiritCenterY * GridSize);
}
//用白色点记录移动轨迹
rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Snow);
rect.Width = 5;
rect.Height = 5;
Carrier.Children.Add(rect);
Canvas.SetLeft(rect, X);
Canvas.SetTop(rect, Y);
}
Tags:开发 WPF Silverlight
编辑录入:爽爽 [复制链接] [打 印]- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››WPF学习笔记12. Binding (5)
- ››WPF学习笔记11. Binding (4)
- ››WPF学习笔记10. Binding (3)
- ››WPF学习笔记9. Binding (2)
- ››WPF学习笔记8. Binding
更多精彩
赞助商链接