上一段时间一直在阅读“深蓝色”的游戏制作教程,其中第十二讲提到了地图副本,即如下:(引自蓝色的博客)
代码下载地址,https://files.cnblogs.com/wangweixznu/WpfGameStudy.rar
具体详细内容讲解可以参考http://www.cnblogs.com/alamiye010/archive/2009/06/17/1505342.html
但是感觉有一个问题就是对于一旦用到A*算法就要涉及用二维矩阵来构建地图的障碍物,然而大家都知道,一款游戏的地图是很复杂的,障碍物也很复杂,如果纯粹靠编程的方式来精确定位障碍物难免有些麻烦,必须在在图像处理工具上标出每个障碍物的具体位置,然后在一一构建障碍物矩阵,利用深蓝色这篇文章中的道理,我想了一个方法,如果我们在加载地图的时候就能够自动创建障碍物矩阵,岂不是更好,那才是真正面向对象编程,地图由你怎么换,你只要给我提供一副黑白图片即可以了,加载地图的时候会根据黑白地图即地图副本自动创建障碍物矩阵
具体代码如下:页面代码
<Window x:Class="WpfGameStudy.AStarMapDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AStarMapDemo" Width="650" Height="650">
<Canvas Name="canvas" Width="600" Height="600" Background="Gray" MouseLeftButtonDown="canvas_MouseLeftButtonDown"></Canvas>
</Window>
对应后台代码:
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Windows;
6
using System.Windows.Controls;
7
using System.Windows.Data;
8
using System.Windows.Documents;
9
using System.Windows.Input;
10
using System.Windows.Media;
11
using System.Windows.Media.Imaging;
12
using System.Windows.Shapes;
13
using System.Windows.Media.Animation;
14
using System.IO;
15
16
using QX.Game.PathFinder;
17
namespace WpfGameStudy
18
}
只是简单写了一下,细节上的大家可以自己完善,根据这个原来创建地图真实太Easy了,哈哈
注:里面用的A*算法还是深蓝的,在此声明;感谢深蓝写了这么好的教程