C#推箱子-地图编辑器

之前贴过推箱子用objective-c简单实现,与之配合使用的还应该有一个地图编辑器,这里用C#做了一个,供大家参考!

思路很简单,就是在窗体上点击生成 Wall:墙, Worke:工人,Box:箱子,Passageway:通道,Destination:目的地,最后导出一个关卡的配置的plist,里面是一个一维数组:010101234567... 通过这个数组游戏就可以进行初始化布局了!代码如下:

 public partial class frmMain : Form
    {
        private enum Map_State
        {
            None = -1, Wall = 0, Worker,
            Box, Passageway, Destination, WorkerInDest, RedBox
        };
        //Wall:墙0, Worke:工人1,Box:箱子2,Passageway:通道3,Destination:目的地4,WorkerInDest:工人在目的地5,RedBox:箱子在目的地6
        private Map_State m_now_Select;//当前选中的图标工具
        private Map_State[,] myArray = new Map_State[10, 10];//地图数组
        private int BlockSize = 50;//方块边长

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            initMap();//初始化地图
        }

        private void toolStripBtn_Wall_Click(object sender, EventArgs e)
        {
            //选中墙
            m_now_Select = Map_State.Wall;
        }

        private void toolStripBtn_Box_Click(object sender, EventArgs e)
        {
            //选中箱子
            m_now_Select = Map_State.Box;
        }

        private void toolStripBtn_Destination_Click(object sender, EventArgs e)
        {
            //选中目的地
            m_now_Select = Map_State.Destination;
        }

        private void toolStripBtn_Passageway_Click(object sender, EventArgs e)
        {
            //选中通道
            m_now_Select = Map_State.Passageway;
        }

        private void toolStripBtn_Worker_Click(object sender, EventArgs e)
        {
            //选中人
            m_now_Select = Map_State.Worker;
        }

        private void toolStripBtn_Save_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "plist文件|*.plist";
            saveFileDialog1.Title = "保存";
            saveFileDialog1.FileName = string.Empty;
            saveFileDialog1.ShowDialog(); //显示保存对话框
        }

        private void toolStripBtn_New_Click(object sender, EventArgs e)
        {
            initMap();//新建,初始化地图
        }

        private void ClearMap()
        {
            m_now_Select = Map_State.None; //当前未选中图标工具
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 10; j++)
                    myArray[i, j] = Map_State.None;
            pictureBox1.Width = 10 * BlockSize + 2;
            pictureBox1.Height = 10 * BlockSize + 2;
            drawimage();//画图
        }

        private void initMap()
        {
            m_now_Select = Map_State.None; //当前未选中图标工具
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 10; j++)
                    myArray[i, j] = Map_State.Passageway;
            pictureBox1.Width = 10 * BlockSize + 2;
            pictureBox1.Height = 10 * BlockSize + 2;
            drawimage();
        }

        //绘制整个游戏区域图形
        private void drawimage()
        {
            Bitmap bit = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            Graphics g = Graphics.FromImage(bit);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            System.Drawing.Image image;
            for (int i = 0; i < 10; i++)
            {

                for (int j = 0; j < 10; j++)
                {
                    if (myArray[i, j] == Map_State.Wall)//是墙
                    {
                        image = new Bitmap(System.Windows.Forms.Application.StartupPath+"\\wall.gif");
                        g.DrawImage(image, i * 50, j * 50, 50, 50);
                    }
                    if (myArray[i, j] == Map_State.Worker)//是工人
                    {
                        image = new Bitmap(System.Windows.Forms.Application.StartupPath + "\\worker.gif");
                        g.DrawImage(image, i * 50, j * 50, 50, 50);
                    }
                    if (myArray[i, j] == Map_State.Box)//是箱子
                    {
                        image = new Bitmap(System.Windows.Forms.Application.StartupPath + "\\box.gif");
                        g.DrawImage(image, i * 50, j * 50, 50, 50);
                    }
                    if (myArray[i, j] == Map_State.Passageway)//是通道
                    {
                        image = new Bitmap(System.Windows.Forms.Application.StartupPath + "\\passageway.gif");
                        g.DrawImage(image, i * 50, j * 50, 50, 50);
                    }
                    if (myArray[i, j] == Map_State.Destination)//是目的地
                    {
                        image = new Bitmap(System.Windows.Forms.Application.StartupPath + "\\destination.gif");
                        g.DrawImage(image, i * 50, j * 50, 50, 50);
                    }
                }
            }
            this.pictureBox1.Image = bit;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int x, y;
            x = e.X / 50;
            y = e.Y / 50;
            myArray[x, y] = m_now_Select;//修改地图
            drawimage();
            //MessageBox.Show(x.ToString()+"-"+y.ToString());
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            //写入  
            var arr = new PListArray();
            for (int j = 9; j >= 0; j--)
            {
                for (int i = 0; i < 10; i++)
                {
                    arr.Add(new PListInteger((long)myArray[i, j]));
                }
            }

            var myRoot = new PListRoot();
            myRoot.Root = arr;
            myRoot.Save(saveFileDialog1.FileName, PListFormat.Xml);
        }

        private void toolStripBtnOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "plist文件|*.plist";
            openFileDialog1.Title = "打开";
            openFileDialog1.Multiselect = false;
            openFileDialog1.FileName = string.Empty;
            openFileDialog1.ShowDialog();//显示打开对话框
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            if (File.Exists(openFileDialog1.FileName))
            {

                //读取    
                PListRoot root = PListRoot.Load(openFileDialog1.FileName);
                PListArray arr = (PListArray)root.Root;

                for (int j = 9; j >= 0; j--)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        myArray[i, j] = (Map_State)((PListInteger)arr[(9 - j) * 10 + i]).Value;
                    }
                }

                drawimage();
            }
        }
    }

上面使用到的读写plist文件的类库,在我之前的文章有介绍:C#使用iphone-plist-net库读写plist文件 

运行截图:

C#推箱子-地图编辑器_第1张图片

源码下载:http://download.csdn.net/detail/wangqiuyun/4586661

你可能感兴趣的:(游戏,object,image,C#,Class,工具)