首页 > 技术文章 > 梦幻之旅--地图编辑器开发

jack-Star 2013-07-29 22:24 原文

首先,我的地图编辑器是十分简单的,但是网上一个像样的例子也没有,很是无奈,或许是我没有找到吧,不喜欢的勿拍。接下来进入正题。我的地图就比较简单,就是简单的1和0。1表示可走,0表示不可走。接下来先看看软件的截图吧

首先需要一个panel ,也就是一个画板,画板的背景是一张地图图片,接下来就是往画板上话虚线 那么 贴上代码(里面代码注释已经很详细了)

 1     private void panel1_Paint(object sender, PaintEventArgs e)
 2         {
 3            
 4             Rectangle rect = this.Bounds; // 获取当前窗体的坐标
 5             int col = 320 / 32;    // 网格的列数
 6             int row = 240 / 24;  // 网格的行数
 7             int drawRow = 0;
 8             int drawCol = 0;
 9             Pen p = new Pen(Brushes.Blue);      // 实例化画笔。
10             p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;  // 设置画笔的样式(虚线)。
11             for (int i = 0; i <= row; i++)  // 画水平线
12             {
13                 e.Graphics.DrawLine(p, 0, drawCol, 320, drawCol);
14                 drawCol += 24;
15             }
16             for (int j = 0; j <= col; j++)// 画垂直线
17             {
18                 e.Graphics.DrawLine(p, drawRow, 0, drawRow, 240);
19                 drawRow += 32;
20             }
21         }
panel Paint 事件上画虚线

其次是 当鼠标在panel 上移动的时候 也要触发一个事件 ,用来显示当前移动到哪个单元格上了 ,那么 贴上代码

1     private void panel1_MouseMove(object sender, MouseEventArgs e)
2         {
3             int x = e.X / 32;//当前坐标的x坐标,除以32 是因为那些 小方格是以32*24 大小的
4             int y = e.Y / 24;//当前坐标的x坐标
5             label1.Text = "" + x + "×" + y + "个矩形 " + "坐标为:(" + e.X + "," + e.Y + ")";
6 
7         }
鼠标移动事件

还有 鼠标移动到某个小方格上的鼠标点击事件

 1    public Color[] mycolor = new Color[] { Color.Black, Color.Brown };
 2         public Point mypoint;
 3         public Pen mypen = new Pen(Brushes.Black);
 4         private void panel1_MouseClick(object sender, MouseEventArgs e)
 5         {
 6             if (e.Button == MouseButtons.Left)
 7             {
 8                 Graphics g = this.panel1.CreateGraphics();
 9                 GraphicsPath gp = new GraphicsPath();
10                 int x = (e.X / 32) * 32;
11                 int y = (e.Y / 24) * 24;
12                 a[x / 32, y / 24] = 0;
13                 Rectangle rec = new Rectangle(new Point(x, y), new Size(32, 24));
14                 gp.AddRectangle(rec);
15                 SolidBrush pb = new SolidBrush(Color.Red);
16                 pb.Color = Color.Red;
17                 g.DrawRectangle(mypen, rec);
18                 g.FillPath(pb, gp);
19             }
20             else
21             {
22                 Graphics g = this.panel1.CreateGraphics();
23                 GraphicsPath gp = new GraphicsPath();
24                 int x = (e.X / 32) * 32;
25                 int y = (e.Y / 24) * 24;
26                 a[x / 32, y / 24] = 1;
27                 Rectangle rec = new Rectangle(new Point(x, y), new Size(32, 24));
28                 gp.AddRectangle(rec);
29                 SolidBrush pb = new SolidBrush(Color.Green);
30                 pb.Color = Color.Green;
31                 g.DrawRectangle(mypen, rec);
32                 g.FillPath(pb, gp);
33             }
34         }
鼠标点击事件

还有 打开一个新的 背景, 只需要单机打开新的地图按钮

1        private void button1_Click(object sender, EventArgs e)
2         {
3             openFileDialog1.ShowDialog();
4             panel1.BackgroundImage = Image.FromFile(openFileDialog1.FileName);
5         }
打开新的地图

点击保存按钮,保存对应地图的地图状态信息。

 1      private void button2_Click(object sender, EventArgs e)
 2         {
 3 
 4             SaveFileDialog sfd = new SaveFileDialog();
 5             sfd.Filter = "(*.txt)|*.txt|(*.*)|*.*";
 6             sfd.AddExtension = true;
 7             sfd.RestoreDirectory = true;
 8             if (sfd.ShowDialog() == DialogResult.OK)
 9             {
10                 FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
11                 StreamWriter sw = new StreamWriter(fs);
12                 try
13                 {
14                     StringBuilder sb = new StringBuilder();
15                     for (int i = 0; i <= 9; i++)
16                     {
17                         for (int j = 0; j <=9; j++)
18                         {
19                             sb.Append(i+"  ");
20                             sb.Append(j + "  ");
21                             sb.AppendLine(a[i,j] + "  ");
22                         }
23                     }
24                     sw.Write(sb.ToString());
25                     sw.Flush();
26                 }
27                 catch (Exception ex)
28                 {
29                     MessageBox.Show(ex.Message.ToString());
30                 }
31                 finally
32                 {
33                     sw.Close();
34                     fs.Close();
35                 }
36             }
37 
38 
39         }
导出地图数据

总结起来 这个地图编辑器是非常简单的,他只是实现了地图基础数据的整合,但是就是这样一个简单的小工具,却能省去大量的时间。一般一张地图五分钟,总共设计这样一个游戏的地图时间在半个小时就能搞定。  那个下面就上传,地图包和地图编辑器工具。仅供大家参考。其实我的成长,也有你们的帮助。我会努力把这个项目讲完的。

 http://pan.baidu.com/share/link?shareid=3075606904&uk=1345361952

http://pan.baidu.com/share/link?shareid=3107346913&uk=1345361952

 

推荐阅读