Deluxe Edition aircraft war series (6)-source code

Source: Internet
Author: User
Tags random seed

The last article describes the main logic judgment in the game. After the above work is fully prepared, the main logic of the game will become very clear, next, you will see that all the logic is so clear and natural, because the preparations have been fully prepared. Here we only integrate the previous work and add some game judgment elements.

At the same time, the source code will provide the link address at the end of the article. The source code is hosted on GitHub. Everything is open-source and free of charge. In the current environment, open-source is the king, so sharing can win-win, I always think this is right. You have an idea. After communication and sharing, we both have two ideas. Why not.

Well, back to the question, the main judgment logic of the game is in the gamescene scenario, including the gamelayer layer, where the game logic is judged.

Let's take a look at the content of gamescene. h:

# Include "cocos2d. H "# include" planelayer. H "# include" bulletsprite. H "# include" enemylayer. H "using_ns_cc; Class gamelayer: Public cocos2d: layer {public: // create the scenario where the gamelayer layer belongs static cocos2d: Scene * createscene (); Virtual bool Init (); // call this function virtual void onentertransitiondidfinish (); create_func (gamelayer); Public: // update Game Void gameupdate (float DT) based on each frame ); // bullet collision detection bool bulletcollisionenemy (sprite * pbullet); // aircraft collision detection bool enemycollisionplane (); // menu callback function void menuclosecallback (cocos2d: ref * psender ); public: planelayer * planelayer; // aircraft layer bulletsprite * bulletsprite; // bullet layer enemylayer * enemylayer; // enemy layer int getrand (INT start, int end ); // obtain the random number from start to end };

Without too much explanation, let's look at the specific implementation of each function, gamescene. cpp
# Include "gamescene. H "/*** create a scenario and add the gamelayer Layer */cocos2d: Scene * gamelayer: createscene () {auto scene = scene: Create (); auto layer = gamelayer: Create (); scene-> addchild (layer); Return scene;} bool gamelayer: Init () {If (! Layer: Init () {return false;} This-> settouchenabled (true); // you can click size winsize = Director ctor: getinstance () at the set layer () -> getwinsize ();/*** loads the background image immediately, */Char buff [15]; int id = getrand (1, 5); // returns 1 ~ The random number sprintf (buff, "img_bg_0000d.jpg", ID) between 5; Auto Over = sprite: Create (buff); over-> setposition (point (winsize. width/2, winsize. height/2); this-> addchild (over); Return true;}/*** returns a random integer from start to end */INT gamelayer :: getrand (INT start, int end) {struct timeval TV; gettimeofday (& TV, null); unsigned long int rand_seed = TV. TV _sec * 1000 + TV. TV _usec/1000; // Random Seed srand (rand_seed); float I = ccrandom_0_1 () * (end-start + 1) + start; Return (INT) I ;} /*** call the onenter function * to create an airplane, bullet, or enemy plane and add it to the layer */void gamelayer: onentertransitiondidfinish () {planelayer = planelayer :: create (); this-> addchild (planelayer); bulletsprite = bulletsprite: Create (); this-> addchild (bulletsprite); enemylayer = enemylayer: Create (); this-> addchild (enemylayer); // call the gameupdate function this-> schedule (schedule_selector (gamelayer: gameupdate) when setting each frame; // Add a menu, and set the callback function size visiblesize = Director: getinstance ()-> getvisiblesize (); point origin = Director: getinstance ()-> getvisibleorigin (); Auto closeitem = menuitemimage :: create ("closenormal.png", "closeselected.png", cc_callback_1 (gamelayer: menuclosecallback, this); closeitem-> setposition (point (origin. X + visiblesize. width-closeitem-> getcontentsize (). width/2, origin. Y + closeitem-> getcontentsize (). height/2); Auto menu = menu: Create (closeitem, null); menu-> setposition (point: zero); this-> addchild (menu, 1);}/*** menu callback function */void gamelayer: menuclosecallback (ref * psender) {# If (cc_target_platform = cc_platform_wp8) | (cc_target_platform = cc_platform_winrt) MessageBox ("you pressed the close button. windows Store apps do not implement a close button. "," alert "); return; # endifdirector: getinstance ()-> end (); # If (cc_target_platform = cc_platform_ios) Exit (0 ); # endif}/*** collision detection function between bullets and enemy planes */bool gamelayer: bulletcollisionenemy (sprite * pbullet) {// traverse all enemy planes in the scenario, check whether the bullets overlap with the packaging rectangle of the enemy. For (Auto & eenemy: enemylayer-> vecenemy) {enemysprite * penemysprite = (enemysprite *) eenemy; // determine whether the rectangle overlaps if (pbullet-> boundingbox (). intersectsrect (penemysprite-> getboundingbox () {if (1 = penemysprite-> getlife () {penemysprite-> loselife (); enemylayer-> submit (penemysprite );} else {penemysprite-> loselife () ;}// remove the bullet bulletsprite-> removebullet (pbullet); Return true ;}} return false ;} /*** the game logic is detected at each frame. * The bullet and the enemy plane are collided. * The main aircraft and the enemy plane are collided. */void gamelayer :: gameupdate (float DT) {bool bmovebutt = false; For (Auto & ebuttle: bulletsprite-> vecbullet) {sprite * pbullet = (sprite *) ebuttle; bmovebutt = aggregate (pbullet ); if (bmovebutt) {return ;}} enemycollisionplane ();}/*** collision between enemy planes and the main aircraft * traverse all enemy planes for detection */bool gamelayer: enemycollisionplane () {sprite * pplane = (sprite *) planelayer-> getchildbytag (airplane); For (Auto & eenemy: enemylayer-> vecenemy) {worker * penemysprite = (enemysprite *) eenemy; if (pplane-> boundingbox (). intersectsrect (penemysprite-> getboundingbox () & penemysprite-> getlife ()> 0) {// todo, do what you want/This-> unscheduleallselectors (); // This-> bulletlayer-> stopbulletshoot (); // This-> planelayer-> blowup (); Return true ;}} return false ;}
There are detailed comments in each key area. You can understand the engine and leave a message if you have any questions.

The download link of the source code is provided. It is hosted on GitHub. the most annoying thing for me is to download something and pay TM credits. For example, share the resources on csdn, I still have to give you some points to download, or you don't need to upload it. Since it is open-source, you don't want to do anything for yourself, and you may need to be sprayed again, but I always believe that open-source is the king, click OK.

Click Open Link


Deluxe Edition aircraft war series (6)-source code

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.