Cocos2d-x monster intelligent AI monster also has IQ ---- game development "zhao Yun to fight" (6), monster bullet ball Zhao Yun

Source: Internet
Author: User

Cocos2d-x monster intelligent AI monster also has IQ ---- game development "zhao Yun to fight" (6), monster bullet ball Zhao Yun

This is the blog of Evankaka. You are welcome to discuss and exchange the above ~~~~~~

Reprinted please indicate the source http://write.blog.csdn.net/postedit/42611031

I have been studying the AI of monsters for the past few days, and my blog has not been updated. This article will focus on the monster intelligence in game development. A good game generally has to be divided into different levels of monsters, so that our game is interesting. If the IQ of monsters is too high, the game is difficult. If the IQ of a monster is too low, it is meaningless to play the game. Generally, low-level monsters and intermediate monsters occupy the majority of good games, while high-level monsters are generally BOSS-level monsters. Let me talk about some of my views on Monster AI. This article connected to the Cocos2d-x blood strip follow the monster movement ---- game development "zhao Yun to fight" (5)

Cocos2d-x version: 2.2.5

Project Environment: Windows 7 + VS2010

Open Method: place the project in the project folder under the cocos2d-x installation directory open with


Directory

I. Introduction to the concept of intelligent AI for monsters

Ii. added functions for the Monster class

Iii. Summary and Analysis


First look at the effect:




I. Introduction to the concept of intelligent AI for monsters

Low-level monsters are generally inmovable monsters. They are fixed in the original place and constantly launch attacks. Then, based on the hero's position, the bullets are constantly changed to the left or to the right, such as the game's fort.

Intermediate monster-initially has some IQ, mainly manifested in the fact that when a hero is within its attack range and can know the hero's position, it will determine whether to launch an attack in proportion. Otherwise, you can follow a certain route or walk randomly. Then the attack is triggered randomly. Only when a hero is within the attack range can the hero stop moving. In the same place, the hero determines whether to launch an attack based on probability.

Senior monster ----- BOSS-level monster, which has its own visible range area. When a hero falls into its visible range area, it will chase the hero to run. In the visible range area, there is also an attack range area. When a hero falls into the attack range area, the monster will trigger an attack based on a certain probability, which is generally relatively high. Then, if a hero is not within the visible range of the monster, the monster will have its own patrol route, and it will continue to follow this route, and will always find the hero, how do you run the hero, it will always follow you.

Of course, all of the above is relatively simple. In actual games, monsters will be further subdivided. For example, some monsters can automatically add blood, and some monsters can summon blood, I will not discuss it in detail here. If you are interested, you can go to Baidu and wait for it again. Monster intelligence involves AI, and further exploration is an algorithm problem.

(I used PS to do this myself. Haha, you can do it. programmer PS should have learned a little bit too !)

This article mainly sets up an algorithm for advanced monster AI. The following is its flowchart:

(Alas. The most annoying flowchart. I drew it for a while)

The following is an effect of this article:



Ii. added functions for the Monster class

In the previous lecture, how does one add a function to the Monster class to implement 3 seconds of computing? In fact, it's easy. Isn't it enough to open a three-second counter event?

This-> schedule (schedule_selector (Monster: updateMonster), 3.0f); // calculate the distance every 3 seconds
The distance between monsters and heroes is calculated, which involves many judgments. For details, refer to the above algorithm flowchart and refer to the following code. Note,
StartListen(CCNode* m_hero,CCNode* m_map)

This is a function to start the monster listening hero.



First, add the following in Monster. h:

Public:

// Within the visible range, the monster follows the heroic movement void FollowRun (CCNode * m_hero, CCNode * m_map); // determines whether to attack void JudegeAttack (); // monster patrol route void MonsterSeeRun (); // The monster starts listening to the hero void StartListen (CCNode * m_hero, CCNode * m_map); // The listening function, which can be detected every 3 seconds, calculate the distance between the hero and the monster void updateMonster (float delta); // update the function. If the hero is within the visible range, the void update (float delta) is continuously triggered );

Private:

CCNode * my_hero; // The current hero CCNode * my_map; // The current map float dis; // The distance between the current monster and the hero


Then here is its implementation of Monster. cpp:

Void Monster: FollowRun (CCNode * m_hero, CCNode * m_map) {// get the distance between two points x. Remember to add the map float x = m_hero-> getPositionX () to the Monster coordinates () -(this-> getPositionX () + m_map-> getPositionX (); // obtain the distance between two points y, remember to add float y = m_hero-> getPositionY ()-(this-> getPositionY () + m_map-> getPositionY () to the coordinates of the monster ()); // calculate the distance between the monster and the hero. dis = sqrt (pow (x, 2) + pow (y, 2); if (dis> = 300) // when the distance between the monster and the hero exceeds 300 return; if (dis <= 100) // within the scope of the monster attack, the monster stops moving {this-> StopAnimation (); // stop running Judeg EAttack (); // determine with a certain probability whether the return attack is triggered;} if (x <-100) // determine the distance between the x coordinate of the monster and the hero {MonsterDirecton = true; m_MonsterSprite-> setFlipX (MonsterDirecton); // set the direction if (IsAttack) return; this-> setPosition (this-> getPositionX ()-1, this-> getPositionY ()); // The Monster moves this-> SetAnimation ("monster_run", 6, MonsterDirecton); // play the animation} else if (x> 100) {MonsterDirecton = false; m_MonsterSprite-> setFlipX (MonsterDirecton); // set the direction if (IsAttack) return; this-> setPos Ition (this-> getPositionX () + 1, this-> getPositionY (); this-> SetAnimation ("monster_run", 6, MonsterDirecton ); // play the animation} else if (x <= 100) // when the difference between the monster seek title and the hero is less than 100, start to move the monster ordinate {if (m_hero-> getPositionY ()> this-> getPositionY () {m_MonsterSprite-> setFlipX (MonsterDirecton); // set the direction if (IsAttack) return; this-> setPosition (this-> getPositionX (), this-> getPositionY () + 1); this-> SetAnimation ("monster_run", 6, MonsterDirecton ); // play the animation} else If (m_hero-> getPositionY () <this-> getPositionY () {m_MonsterSprite-> setFlipX (MonsterDirecton); // set the direction if (IsAttack) return; this-> setPosition (this-> getPositionX (), this-> getPositionY ()-1); this-> SetAnimation ("monster_run", 6, MonsterDirecton ); // play the animation }}void Monster: JudegeAttack () {srand (UINT) GetCurrentTime (); int x = rand () % 100; if (x> 98) {this-> AttackAnimation ("monster_attack", 5, MonsterDirecton) ;}} void Monster: MonsterSeeRun () {if (dis <300) return; this-> SetAnimation ("monster_run", 6, MonsterDirecton); // play the animation CCMoveBy * moveby1; if (MonsterDirecton = true) moveby1 = CCMoveBy: create (2, ccp (-100,0); else moveby1 = CCMoveBy: create (2, ccp (100,0 )); // create a callback action. After the patrol route is completed, CCCallFunc * callFunc = CCCallFunc: create (this, callfunc_selector (Monster: StopAnimation )); // create a continuous action CCActionInterval * xunluo = CCSequence: create (moveby1, callFunc, NU LL); this-> runAction (xunluo);} // start listening void Monster: StartListen (CCNode * m_hero, CCNode * m_map) {my_hero = m_hero; my_map = m_map; this-> schedule (schedule_selector (Monster: updateMonster), 3.0f); // calculate the distance from this-> scheduleUpdate () every 3 seconds; // once a hero enters the visible range, monster chasing Hero} // listens to the function, detects void Monster: updateMonster (float delta) every 3 seconds {// obtains the distance between two points x, remember to add float x = my_hero-> getPositionX ()-(this-> getPositionX () + my_map-> getPositionX () to the coordinates of monsters; // To the distance between two points y, remember to add the map float y = my_hero-> getPositionY ()-(this-> getPositionY () + my_map-> getPositionY () to the coordinates of the monster ()); // calculate the distance between the monster and the hero. dis = sqrt (pow (x, 2) + pow (y, 2); if (dis> = 300) {if (! IsRunning) MonsterSeeRun () ;}} void Monster: update (float delta) {if (dis <300) // when a hero is within its visible range, keep chasing the hero FollowRun (my_hero, my_map );}

Note the following:

// Add monster1 = Monster: create (); // monster1-> InitMonsterSprite ("monster.png"); monster1-> InitMonsterSprite ("monster.png", "xue_back.png ", "xue_fore.png"); monster1-> setPosition (ccp (visibleSize. width-50, visibleSize. height/2); this-> addChild (monster1, 1 );
Changed:

// Add monster1 = Monster: create (); // monster1-> InitMonsterSprite ("monster.png"); monster1-> InitMonsterSprite ("monster.png", "xue_back.png ", "xue_fore.png"); monster1-> setPosition (ccp (visibleSize. width-50, visibleSize. height/2); mymap-> addChild (monster1); // Add the monster to the map so that the monster can move monster1-> StartListen (hero, mymap) with the map ); // very important. This is used in this lecture.

Finally, let's take a look at the effect:

1. The initial distance between the monster and the hero is 300 different. Take the patrol route


2. The hero is visible to the monster and within the attack range

3. Monsters follow heroes


4. Monsters follow heroes




Iii. Summary and Analysis

Here, the monster can only be a simple high-level monster, because the patrol route is not very well done, I just let it exercise around 100, a better patrol route is not like this, here to save trouble, it's easy. In addition, there is no collision detection yet, so you can see a Hero passing through the monster. Don't be surprised. I plan to do this later. In addition, when a monster follows the hero's movements, a pause should be set. In this way, the hero may be out of the visual range of the monster. If this is not done, once a hero falls into the visible range of the monster, the monster will always chase the hero, so that the game is more difficult to play. There are two shortcomings. You can try again later. This is today ~~



Postscript:

I thought for a long time to get this monster intelligence. I just wanted to get a simple low-level monster, and then I thought it was boring. I just got an advanced one (although not a senior one). Fortunately, I still had a PS, and I couldn't get a lot of pictures! In addition, Baidu monster AI on the Internet will have a lot of resources. If you are interested, go and find them. If necessary, leave a mailbox. I will send the project to you ~




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.