Silverlight-pursuit and dodge of AI in games

Source: Internet
Author: User
Tags visual studio 2010

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:
  1. <Usercontrol X: class = "slai_lesson1.mainpage"
  2. Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  4. Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
  5. Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. MC: ignorable = "D"
  7. D: designheight = "300" D: designwidth = "400">
  8. <Canvas X: Name = "layoutroot" background = "white">
  9. </Canvas>
  10. </Usercontrol>

Put the original <grid X: Name = "layoutroot" background = "white"> </GRID>

  1. 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:
    1. Namespace slai_lesson1
    2. {
    3. Public partial class mainpage: usercontrol
    4. {
    5. Rectangle _ human, _ monster;/* _ human _ Monster */
    6. Dispatchertimer _ dispatchertime;
    7. Double monsterx, monstery, humanx, humany;/* coordinates of monsters and humans */
    8. Public mainpage ()
    9. {
    10. Initializecomponent ();
    11. _ Human = new rectangle ();
    12. _ Human. width = 40; // you can specify the width of a rectangle.
    13. _ Human. Height = 40; // you can specify the height of a rectangle.
    14. _ Human. Fill = new solidcolorbrush (colors. Red); // fill color
    15. _ Human. setvalue (canvas. leftproperty, 100.0); // sets the X coordinate of a human.
    16. _ Human. setvalue (canvas. topproperty, 150.0); // sets the Y coordinate of a human.
    17. _ Monster = new rectangle ();
    18. _ Monster. width = 40;
    19. _ Monster. Height = 40;
    20. _ Monster. Fill = new solidcolorbrush (colors. Black );
    21. _ Monster. setvalue (canvas. leftproperty, 50.0 );
    22. _ Monster. setvalue (canvas. topproperty, 80.0 );
    23. Layoutroot. Children. Add (_ human );
    24. Layoutroot. Children. Add (_ monster );
    25. _ Dispatchertime = new dispatchertimer ();
    26. _ Dispatchertime. interval = timespan. frommilliseconds (200 );
    27. _ Dispatchertime. Tick + = new eventhandler (_ dispatchertime_tick );
    28. _ Dispatchertime. Start ();
    29. }
    30. Void _ dispatchertime_tick (Object sender, eventargs E)
    31. {
    32. Monsterx = (double) _ monster. getvalue (canvas. leftproperty); // obtain the X coordinate of the current monster
    33. Monstery = (double) _ monster. getvalue (canvas. topproperty );
    34. Humanx = (double) _ human. getvalue (canvas. leftproperty );
    35. Humany = (double) _ human. getvalue (canvas. topproperty );
    36. Catch (monsterx, monstery, humanx, humany );
    37. Runaway (monsterx, monstery, humanx, humany );
    38. _ Human. setvalue (canvas. leftproperty, humanx );
    39. _ Human. setvalue (canvas. topproperty, humany );
    40. _ Monster. setvalue (canvas. leftproperty, monsterx );
    41. _ Monster. setvalue (canvas. topproperty, monstery );
    42. }
    43. /// <Summary>
    44. /// Basic chase Function
    45. /// </Summary>
    46. /// <Param name = "mox"> </param>
    47. /// <Param name = "Moy"> </param>
    48. /// <Param name = "Hux"> </param>
    49. /// <Param name = "Huy"> </param>
    50. Public void catch (double Mox, double Moy, double hux, double Huy)
    51. {
    52. Monsterx = MOX;
    53. Monstery = Moy;
    54. Humanx = Hux;
    55. Humany = Huy;
    56. If (monsterx> humanx) // if the current coordinate of a monster is greater than that of a human, run back.
    57. {
    58. Monsterx-= 4;
    59. }
    60. Else if (monsterx
    61. {
    62. Monsterx + = 4;
    63. }
    64. If (monstery> humany) // similar to the X coordinate
    65. {
    66. Monstery-= 4;
    67. }
    68. Else if (monstery
    69. {
    70. Monstery + = 4;
    71. }
    72. }
    73. /// <Summary>
    74. /// Basic Dodge Algorithm
    75. /// </Summary>
    76. /// <Param name = "mox"> </param>
    77. /// <Param name = "Moy"> </param>
    78. /// <Param name = "Hux"> </param>
    79. /// <Param name = "Huy"> </param>
    80. Public void runaway (double Mox, double Moy, double hux, double Huy)
    81. {
    82. Monsterx = MOX;
    83. Monstery = Moy;
    84. Humanx = Hux;
    85. Humany = Huy;
    86. If (humanx> monsterx) // if the current human X coordinate is greater than the monster, the system will continue to escape.
    87. {
    88. Humanx + = 4;
    89. }
    90. Else if (humanx <monsterx) // if the current human X coordinate is less than the monster X coordinate, the system will escape.
    91. {
    92. Humanx-= 4;
    93. }
    94. If (monstery> humany)
    95. {
    96. Monstery-= 4;
    97. }
    98. Else if (humany <monstery)
    99. {
    100. Humany-= 4;
    101. }
    102. }
    103. }
    104. }

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:

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.