日期:2014-05-17  浏览次数:21087 次

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] ==