C # Push box-map editor

Source: Internet
Author: User

Previously, I posted a push box with objective-c for simple implementation. I should also use a map editor with objective-C. Here I made a map editor with c # for your reference!

The idea is simple. You can click the form to generate the Wall: Wall, Worke: worker, Box: Box, Passageway: Channel, Destination: Destination, and finally export the plist of a level configuration, there is a one-dimensional array: 010101234567... with this array game, you can initialize the layout! The Code is as follows:

[Csharp]
Public partial class frmMain: Form
{
Private enum Map_State
{
None =-1, Wall = 0, Worker,
Box, Passageway, Destination, WorkerInDest, RedBox
};
// Wall: Wall 0, Worke: WORKER 1, Box: Box 2, Passageway: Channel 3, Destination: Destination 4, WorkerInDest: worker in Destination 5, RedBox: Box in Destination 6
Private Map_State m_now_Select; // the currently selected icon Tool
Private Map_State [,] myArray = new Map_State [10, 10]; // map Array
Private int BlockSize = 50; // The length of the square.
 
Public frmMain ()
{
InitializeComponent ();
}
 
Private void frmMain_Load (object sender, EventArgs e)
{
InitMap (); // initialize the map
}
 
Private void toolStripBtn_Wall_Click (object sender, EventArgs e)
{
// Select the wall
M_now_Select = Map_State.Wall;
}
 
Private void toolStripBtn_Box_Click (object sender, EventArgs e)
{
// Select the box
M_now_Select = Map_State.Box;
}
 
Private void toolStripBtn_Destination_Click (object sender, EventArgs e)
{
// Select the destination
M_now_Select = Map_State.Destination;
}
 
Private void toolStripBtn_Passageway_Click (object sender, EventArgs e)
{
// Select the channel
M_now_Select = Map_State.Passageway;
}
 
Private void toolStripBtn_Worker_Click (object sender, EventArgs e)
{
// Selected person
M_now_Select = Map_State.Worker;
}
 
Private void toolStripBtn_Save_Click (object sender, EventArgs e)
{
SaveFileDialog1.Filter = "plist file | *. plist ";
SaveFileDialog1.Title = "save ";
SaveFileDialog1.FileName = string. Empty;
SaveFileDialog1.ShowDialog (); // The save dialog box is displayed.
}
 
Private void toolStripBtn_New_Click (object sender, EventArgs e)
{
InitMap (); // creates and initializes a map.
}
 
Private void ClearMap ()
{
M_now_Select = Map_State.None; // The currently unselected icon tool.
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 (); // draw
}
 
Private void initMap ()
{
M_now_Select = Map_State.None; // The currently unselected icon tool.
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 ();
}
 
// Draw the entire game Area
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) // Yes
{
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) // Yes
{
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) // It is a 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) // a channel
{
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) // is the 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 picturebox#mousedown (object sender, MouseEventArgs e)
{
Int x, y;
X = e. X/50;
Y = e. Y/50;
MyArray [x, y] = m_now_Select; // modify the map
Drawimage ();
// MessageBox. Show (x. ToString () + "-" + y. ToString ());
}
 
Private void saveFileDialog1_FileOk (object sender, CancelEventArgs e)
{
// Write
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 file | *. plist ";
OpenFileDialog1.Title = "open ";
OpenFileDialog1.Multiselect = false;
OpenFileDialog1.FileName = string. Empty;
OpenFileDialog1.ShowDialog (); // The Open dialog box is displayed.
}
 
Private void openFileDialog1_FileOk (object sender, CancelEventArgs e)
{
If (File. Exists (openFileDialog1.FileName ))
{
 
// Read
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 ();
}
}
}
The above class library used to read and write plist files has been introduced in my previous articles: C # using the iphone-plist-net library to read and write plist files

Run:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.