WPF game excerpt-map editor (1)

Source: Internet
Author: User
I. Preface

I have read so many blogs recently.ArticleI can't help but prepare to write some blog posts. I don't want to share any knowledge. I just need some ink. I know, I'm afraid that it's still wrong, and it's all about others, in this case, I will not take the opportunity to give it to others. Haha: first, this WPF game series focuses on 1. I want to consolidate and consolidate my knowledge for myself, then sort out what you have learned. 2. Make a study note, and search for relevant knowledge points in the future. Well, it's almost the same. Let's save it first. Here's the question :)

 

---------------------------------------------------- Gorgeous split line ---------------------------------------------------------

The topic of today's notes is:Map editorGive him a bold red color.

Ii. Requirements

As a real game, the map editor is very important. The main function of this map editor is to implement the logical array of maps in the game. How can this be done? Let me give you an example first. We all know that the map in the game shows a picture, but the machine cares about how beautiful you are, how gorgeous it is. He doesn't care, he only knows the data. So I don't need to talk to this rigid guy, so I told him directly, and I pointed to an array and said, this is a very gorgeous, very romantic map, then she believes it... Haha, just kidding me. That is, we can use an array to represent the data structure of the map. Then we set that 1 is accessible and 0 is unavailable. Of course, you can also use other numbers or letters, as long as you are not too troublesome, your machine is not too troublesome.

Simple,CodeI don't want to give it to you. It's quite a place. Just listen to it and you don't understand the source code. The principle is very simple, but there is a problem. If there is very little image data, it would be quite easy to edit the map data directly for wuziqi, but we can zoom in, maps of some online games are very big and many types, such as some competitive games, like LOL, just like one, very big. Imagine, edit valid map data on a large array of 0 and 1... Oh, it's big to think about it. I'm not talking about it. If you see such a huge number, you have all you want to die. Therefore, the map editor responds. Call: the role of this map editor is to edit any map, as long as you are an image, and then directly create obstacles on the image, then, set the start point and end point for automatic path searching (*AlgorithmOh, Mo heart) simulate the movement of characters, verify the correctness, export the Obstacle Data, then you can directly use the Obstacle Data to build a complete game. In addition, you can import the Obstacle Data built last time to add and modify the data, so that you can deal with large and complex maps. This is very human-oriented.

II. Principle

First lookProgramHome page:

 

Then there is the principle. Let's make a small question. I used two grids to deploy a small game. There are two scrollviewer on the left, because scrollviewer can have horizontal and vertical slide rods. Add an image to the content of the bottom-layer scrollviewer to display map images,

XAML:

<Scrollviewer grid. row = "0" grid. column = "0" horizontalalignment = "stretch" name = "mapscrollviewer" verticalignment = "stretch" verticalscrollbarvisibility = "Auto" horizontalscrollbarvisibility = "Auto">

<Scrollviewer. content>

<Image name = "map"> </image>

</Scrollviewer. content>

</Scrollviewer>

Background:

Knowledge Point 1 : Use Openfiledialog

Openfiledialog loadmap = new openfiledialog ()

{

Checkfileexists = true,

Checkpathexists = true,

Multiselect = false,

Filter = "image files (*. jpg, *. PNG) | *. jpg; *. PNG ",

 

};

Loadmap. fileok + = new system. componentmodel. canceleventhandler (loadmap_fileok );

Loadmap. showdialog ();

 

Void loadmap_fileok (Object sender, system. componentmodel. canceleventargs E)

{

Mapname = (sender as openfiledialog). filename;

Map. Source = new bitmapimage (New uri (sender as openfiledialog). filename, urikind. Absolute ));

}

The above layer is the logic layer, which dynamically adds (that is, code writing) a grid to its content. By adding columndefinition and rowdefinition to this grid, the grid is implemented, make the showgridlines of the grid true or display the grid.

Knowledge Point 2: Grid grid usage

Grid = new grid ()

{

Showgridlines = isshowgrid. ischecked. value,

Width = width,

Height = height,

};

Int gridwidth;

If (! Int. tryparse (gridwidth. Text, out gridwidth ))

{

MessageBox. Show ("please? Loss °? Input? ° Y words ?! Why? ");

Return;

}

For (INT x = 0; x <grid. width/gridwidth; X ++)

{

Columndefinition Col = new columndefinition ()

{

Width = new gridlength (gridwidth ),

};

Grid. columndefinitions. Add (COL );

}

Int gridheight = int. parse (gridheight. Text );

If (! Int. tryparse (gridheight. Text, out gridheight ))

{

MessageBox. Show ("please? Loss °? Input? ° Y words ?! Why? ");

Return;

}

For (INT I = 0; I <grid. Height/gridheight; I ++)

{

Rowdefinition ROW = new rowdefinition ()

{

Height = new gridlength (gridheight ),

};

Grid. rowdefinitions. Add (ROW );

}

Showgridlines is a good property. If it is set to true, the rowdefinitions and columndefinitions used for layout can be displayed.

Finally, the two scrollviewer are bound together to move forward and backward,

Knowledge Point 3: bind two scrollviewer

Private void obstructionviewer_scrollchanged (Object sender, scrollchangedeventargs E)

{

Scrollviewer = sender as scrollviewer;

Mapscrollviewer. scrolltoverticaloffset (scrollviewer. verticaloffset );

Mapscrollviewer. scrolltohorizontaloffset (scrollviewer. horizontaloffset );

}

In this way, you can register a click event in the above scrollviewer, that is, obstructionviewer, and initialize a map data array,

Matrix = new byte [2, 256,256];

For (INT I = 0; I <matrix. getupperbound (1); I ++)

{

For (INT x = 0; x <matrix. getupperbound (0); X ++)

{

Matrix [x, I] = 1;

}

}

 

Click to set the value of the array,

Point P = E. getposition (obstructionviewer );

Setobstructionmatrix (INT) p. x/gridwidth, (INT) p. Y/gridheight, (byte) 0 );

And draw a rectangle on it.

Rectangle rect = new rectangle ()

{

Width = gridwidth,

Height = gridheight,

Fill = new solidcolorbrush (colors. White ),

};

Grid. Children. Add (rect );

Grid. registername ("rect" + x + "_" + Y, rect );

Rect. setvalue (grid. columnproperty, X );

Rect. setvalue (grid. rowproperty, y );

Knowledge Point 4: Registration and Management of uielement

Grid. registername ("rect" + x + "_" + Y, rect );

By registering a name with grid, you can use this name to manage it,

Rectangle rect = grid. findname ("rect" + x + "_" + Y) as rectangle;

Grid. unregistername ("rect" + x + "_" + y );

Grid. Children. Remove (rect );

I used to cancel the square here, which is very useful, but this registration is not managed by grid, so you need to clear all the registration cannot be achieved by deregistering the grid. I implemented it manually,

For (INT y = 0; y <matrix. getupperbound (1); y ++)

{

For (INT x = 0; x <matrix. getupperbound (0); X ++)

{

If (Matrix [x, y] = 0)

{

Grid. unregistername ("rect" + x + "_" + y );

}

}

}

Knowledge Point 5: Use of A * Algorithm

I am here to use an algorithm encapsulated by a great god, so you only need to call it. Pilot import reference,

 

Initialization,

Ipathfinder Pathfinder;

String start, end;

I wrote a method to call it. I used the * algorithm to obtain the path point set and drew a square to display the one-stop service,

/// <Summary>

/// Profit margin? Use? A * test A for finding the optimal route? Fault? Does it affect everything? Layer? ? Can you tell me how to use it? Sex?

/// </Summary>

Private void findroadtest ()

{

// Is there no t-effect adequacy? Judge D disconnected?

If (grid = NULL | start = "" | End = "")

{

Return;

}

// Get response? To snapshot? Start evertex renewal? , Why? End? Click renew?

String [] STR = start. Split ('_');

Int start_x = convert. toint32 (STR [1]);

Int start_y = convert. toint32 (STR [2]);

STR = end. Split ('_');

Int end_x = convert. toint32 (STR [1]);

Int end_y = convert. toint32 (STR [2]);

// Enter? Input? Zheng ywen?

Pathfinder = new pathfinderfast (matrix );

List <pathfindernode> Path = Pathfinder. findpath (new system. Drawing. Point (start_x, start_y), new system. Drawing. Point (end_x, end_y ));

 

If (Path = NULL)

{

MessageBox. Show ("what is the path? No? Cunä? In 02! Why? ");

}

Else

{

// Router interface? Cunä? In

For (INT I = 0; I <path. Count; I ++)

{

Pathfindernode node = path. elementat (I );

 

Rectangle rect = new rectangle ();

Rect = new rectangle ()

{

Width = gridwidth,

Height = gridheight,

Radiusx = 5,

Radiusy = 5,

Fill = new solidcolorbrush (colors. Red ),

};

Grid. Children. Add (rect );

Rect. setvalue (grid. columnproperty, node. X );

Rect. setvalue (grid. rowproperty, node. y );

}

}

}

Knowledge Point 6. Usage of LINQ (some)

The following describes some of the usage of LINQ in my program, which is simple but easy to use.

Import XML files,

Xelement myele = xelement. Load ("output. xml ");

Obtain itself or any subnode,

Xelement myroot = myele. descendantsandself ("items"). Single ();

Create a new xelement with the property and value, and put it in the specified path of myroot and keep it,

Xelement newele = new xelement ("item ");

Newele. setattributevalue ("ID", mapname );

Myroot. Add (newele );

Myele. Save (savepath );

Summary

Others can understand the source code. Source code

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.