程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#推箱子-地圖編輯器

C#推箱子-地圖編輯器

編輯:C#入門知識

之前貼過推箱子用objective-c簡單實現,與之配合使用的還應該有一個地圖編輯器,這裡用C#做了一個,供大家參考!

思路很簡單,就是在窗體上點擊生成 Wall:牆, Worke:工人,Box:箱子,Passageway:通道,Destination:目的地,最後導出一個關卡的配置的plist,裡面是一個一維數組:010101234567... 通過這個數組游戲就可以進行初始化布局了!代碼如下:

[csharp] 
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文件

運行截圖:

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved