Silverlight and AI in the game-basic pursuit and dodge (2)

Source: Internet
Author: User
In the last article, we made a simple and basic Chase and dodge function, which is to modify the coordinates of the tracing Based on the prey coordinates, so that the distance between the two is gradually shortened. In the last article, we used to generate a chase animation based on the changes in the screen coordinates of objects. The changed coordinates and seats were continuous, that is, the changes in coordinate points and coordinate points, this is called a continuous environment, but it is more widely used in the game in another way, the brick environment. Today, we are going to pursue and dodge in a brick environment in Silverlight.

What is a brick environment?

In a game composed of bricks, game screens are cut into discontinuous bricks: Square, triangle, and hexagonal. A square is usually selected because it is easier to process the square than other shapes.

With the cut into blocks, the entire game screen looks like this:

 

 

The position of the game player is fixed on a brick. When moving, the unit is brick, and the forward direction is also limited. In the continuous environment in the last article, it uses coordinate points as the position of the player's role, so that it can be moved in any direction. In the brick environment, if the square is the basic unit, the game player can only have eight directions of movement as follows:

 

 

In fact, whether in a continuous environment or in a brick environment, the basic pursuit of Dodge code is common, except that the last time we used point as the basic unit for moving, today, we use bricks as the basic unit. For example, the last moving step is to move from coordinates (0, 0) to coordinates (1, 1 ). The next step is to move from the first brick to the next one. Well, the basic principle is like this. How should we implement it in Silverlight?

I implemented it in this way, but it is not the best to provide a reference. You may have a better solution :)

Now that we want to move the brick environment, we know that it is essentially the same as moving in a continuous environment. In order to reuse the core pursuit and dodge code in the previous article, we only need to reflect the result of the last moving coordinate to the result of moving bricks. You can use a method to enlarge the coordinate movement effect. If you move a coordinate point, such as () ); this operation is zoomed in to move from the first brick to the next corresponding brick. In this way, the effects produced on the screen will be zoomed in. Previously, only a little bit is moved, and after the enlargement, the distance of a square is moved, therefore, we need a logical coordinate set and a screen coordinate set and use some coordinate conversion methods to make communication between them.

The conversion function is as follows:

Code:
  1. Public point changelogictoviewp (Double X, Double Y) // convert the logic coordinates to screen coordinates.
  2. {
  3. X = 350 + x * 60;
  4. Y = 10 + y * 60;
  5. Return new point (x, y );
  6. }

The numbers, 60, and 10 must be determined based on the size of the brick you set and the screen margin. You can test the number yourself. Here we set the length and width of the brick to 60, and the left margin of the canvas is 350, so the coordinates (350) are converted to the position of the first brick on the screen, which is + x * 60.

The Chase code is similar to the previous one:

Code:
  1. Public void catch (double Monx, double Mony, double humx, double humy)
  2. {
  3. Monsterx = getrow (Monx );
  4. Monstery = getcolunm (mony );
  5. Humanx = getrow (humx );
  6. Humany = getcolunm (humy );
  7. If (monsterx
  8. {
  9. Monsterx ++;
  10. Monsterx = setrow (monsterx );
  11. Humanx = setrow (humanx );
  12. }
  13. Else if (monsterx> humanx)
  14. {
  15. Monsterx --;
  16. Monsterx = setrow (monsterx );
  17. Humanx = setrow (humanx );
  18. }
  19. Else if (monsterx = humanx)
  20. {
  21. Monsterx = setrow (monsterx );
  22. Humanx = setrow (humanx );
  23. }
  24. If (monstery> humany)
  25. {
  26. Monstery --;
  27. Monstery = setcolnm (monstery );
  28. Humany = setcolnm (humany );
  29. }
  30. Else if (monstery
  31. {
  32. Monstery ++;
  33. Monstery = setcolnm (monstery );
  34. Humany = setcolnm (humany );
  35. }
  36. Else if (monstery = humany)
  37. {
  38. Monstery = setcolnm (monstery );
  39. Humany = setcolnm (humany );
  40. }
  41. }

The setrow () and setcolnm () methods are also coordinate conversion methods, as follows:

Code:
  1. Public double setrow (Double X)
  2. {
  3. Return x * 60 + 350;
  4. }
  5. Public double setcolnm (Double Y)
  6. {
  7. Return y * 60 + 10;
  8. }

 

Well, I will write it here today, and the specific source code will not be posted. Here I just provide my own ideas. I believe everyone will have a better way, and my code is a bit messy, if you have any friends who need it, you can leave a message. I will sort it out and send it to your mailbox at that time :)

Test: http://www.ophoneba.com/SL/SLAI-2.html

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.