Preface:
Recently I have been studying Silverlight and WPF, And I am immersed in the sea of programming and design every day. I feel that optics does not have a sense of accomplishment when I do not practice. So I decided to write something here. After all, I haven't written an article for a long time. Hahaha. Now, let's get started with Silverlight and AI in the game ~~~
The focus of this lesson is to chase and Dodge. I believe all those who have played games know whether you are playing space fighter shooting games, strategy simulation games, or role-playing games, non-player roles in the game, that is, if the NPC has a chance, will kill you or escape from you when you see that your life is not worth much.
Chase and Dodge are mainly composed of two parts:
1. Make a strategy to chase or escape.
2. Start to chase or escape.
In addition to this, if it is more complex. It also includes avoiding obstacles in the process of chasing or escaping. We will not consider this part today. We will talk about it later.
Now our goal has been clarified, and we believe that you can come up with a solution in a smart way:
In every game loop, update the coordinates of the chaser so that the coordinates of the chaser are getting closer and closer (^ This is also true for chasing girls... Although this method is simple, it does not take into account the direction and speed of the pursuit and the pursuit. Therefore, in practical applications, the game needs to integrate real-time Physical engines, and then consider the location and speed, so that the chaser can try to intercept what is being pursued, instead of simply following it as it is now, if the speed is the same, the system won't be able to catch it ~~ Today, we use the beauty of Silverlight (I feel that Silverlight is a female, don't you know ~?~) To design our first Dodge chase program.
Development Environment: silberlight 4, Visual Studio 2010, and windows7;
1. First create a silberlight application ---- named with a SLAI-lesson1;
2. Open the mainpage. XAML file and modify the code:
Code:
- <Usercontrol X: class = "slai_lesson1.mainpage"
- Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
- Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
- Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
- MC: ignorable = "D"
- D: designheight = "300" D: designwidth = "400">
- <Canvas X: Name = "layoutroot" background = "white">
- </Canvas>
- </Usercontrol>
Put the original <grid X: Name = "layoutroot" background = "white"> </GRID>
- Change to -- <canvas X: Name = "layoutroot" background = "white"> </canvas>
- The reason for this is that the canvas object is more convenient for absolute positioning of the objects in it. Wait, we will put monsters and humans in the canvas to simulate the pursuit and Dodge.
Next, we will go to the mainpage. Xmal. CS file. The code for the entire file is as follows:
Code:
- Namespace slai_lesson1
- {
- Public partial class mainpage: usercontrol
- {
- Rectangle _ human, _ monster;/* _ human _ Monster */
- Dispatchertimer _ dispatchertime;
- Double monsterx, monstery, humanx, humany;/* coordinates of monsters and humans */
- Public mainpage ()
- {
- Initializecomponent ();
- _ Human = new rectangle ();
- _ Human. width = 40; // you can specify the width of a rectangle.
- _ Human. Height = 40; // you can specify the height of a rectangle.
- _ Human. Fill = new solidcolorbrush (colors. Red); // fill color
- _ Human. setvalue (canvas. leftproperty, 100.0); // sets the X coordinate of a human.
- _ Human. setvalue (canvas. topproperty, 150.0); // sets the Y coordinate of a human.
- _ Monster = new rectangle ();
- _ Monster. width = 40;
- _ Monster. Height = 40;
- _ Monster. Fill = new solidcolorbrush (colors. Black );
- _ Monster. setvalue (canvas. leftproperty, 50.0 );
- _ Monster. setvalue (canvas. topproperty, 80.0 );
- Layoutroot. Children. Add (_ human );
- Layoutroot. Children. Add (_ monster );
- _ Dispatchertime = new dispatchertimer ();
- _ Dispatchertime. interval = timespan. frommilliseconds (200 );
- _ Dispatchertime. Tick + = new eventhandler (_ dispatchertime_tick );
- _ Dispatchertime. Start ();
- }
- Void _ dispatchertime_tick (Object sender, eventargs E)
- {
- Monsterx = (double) _ monster. getvalue (canvas. leftproperty); // obtain the X coordinate of the current monster
- Monstery = (double) _ monster. getvalue (canvas. topproperty );
- Humanx = (double) _ human. getvalue (canvas. leftproperty );
- Humany = (double) _ human. getvalue (canvas. topproperty );
- Catch (monsterx, monstery, humanx, humany );
- Runaway (monsterx, monstery, humanx, humany );
- _ Human. setvalue (canvas. leftproperty, humanx );
- _ Human. setvalue (canvas. topproperty, humany );
- _ Monster. setvalue (canvas. leftproperty, monsterx );
- _ Monster. setvalue (canvas. topproperty, monstery );
- }
- /// <Summary>
- /// Basic chase Function
- /// </Summary>
- /// <Param name = "mox"> </param>
- /// <Param name = "Moy"> </param>
- /// <Param name = "Hux"> </param>
- /// <Param name = "Huy"> </param>
- Public void catch (double Mox, double Moy, double hux, double Huy)
- {
- Monsterx = MOX;
- Monstery = Moy;
- Humanx = Hux;
- Humany = Huy;
- If (monsterx> humanx) // if the current coordinate of a monster is greater than that of a human, run back.
- {
- Monsterx-= 4;
- }
- Else if (monsterx
- {
- Monsterx + = 4;
- }
- If (monstery> humany) // similar to the X coordinate
- {
- Monstery-= 4;
- }
- Else if (monstery
- {
- Monstery + = 4;
- }
- }
- /// <Summary>
- /// Basic Dodge Algorithm
- /// </Summary>
- /// <Param name = "mox"> </param>
- /// <Param name = "Moy"> </param>
- /// <Param name = "Hux"> </param>
- /// <Param name = "Huy"> </param>
- Public void runaway (double Mox, double Moy, double hux, double Huy)
- {
- Monsterx = MOX;
- Monstery = Moy;
- Humanx = Hux;
- Humany = Huy;
- If (humanx> monsterx) // if the current human X coordinate is greater than the monster, the system will continue to escape.
- {
- Humanx + = 4;
- }
- Else if (humanx <monsterx) // if the current human X coordinate is less than the monster X coordinate, the system will escape.
- {
- Humanx-= 4;
- }
- If (monstery> humany)
- {
- Monstery-= 4;
- }
- Else if (humany <monstery)
- {
- Humany-= 4;
- }
- }
- }
- }
We use two rectangle rectangles to represent monsters and humans. Then we put a dispatchertimer in the program. Every ms, we trigger a chase function and a Dodge function, and reset the current coordinate point. The two rectangles start to chase and escape on the screen. Finally, send a running image: