Share a client game of fishing and a fishing Client

Source: Internet
Author: User
Tags android games

Share a client game of fishing and a fishing Client

Today, cocos2d is used as a demo of a fishing system. The resources are derived from the widely used android games for years! I previously wrote it on android. Today I wrote it again on cocos2d! You can use it as a demo to learn cocos2d or develop your own fishing system!

Let's take a few slides:

  

The gameplay is very simple. When you hold down the fish with your hands, the fish will be placed in the fish tank before the network breaks down! That's it!

The following code is parsed:

First, define the Fish class:

// Fish header file # pragma once # include "cocos2d. h "# include" cocos-ext.h "# include <vector> using namespace std; using namespace cocos2d; using namespace cocos2d: extension; class Fish: public CCSprite {public: Fish (void ); ~ Fish (void); bool init (); CCPoint getPoint (); void wander (); int get_score (); void setCoord (int x, int y); private: int image_index; double coord_x; double coord_y; CCSprite * _ image; vector <CCTexture2D *> _ textture_vec; int fish_num;}; // Fish implementation code: # include "Fish. h "# include" GameStruct. h "Fish: Fish (void) {this-> fish_num = rand () % 3; this-> _ image = NULL; this-> coord_x = rand () % 200; this-> coord_y = rand () % 200; this-> image _ Index = 0;} Fish ::~ Fish (void) {} bool Fish: init () {CCSprite: init (); this-> _ image = CCSprite :: create (fish_image [this-> fish_num] [0]. c_str (); this-> setContentSize (CCSize (60, 16); this-> _ image-> setPosition (CCPoint (30, 8 )); this-> addChild (this-> _ image); for (int I = 0; I <3; I ++) {CCSprite * temp_obj = CCSprite :: create (fish_image [this-> fish_num] [I]. c_str (); CCTexture2D * hero_hit = temp_obj-> getTexture (); this-> _ textture_vec.push_back (hero_hit);} return true;} void Fish: setCoord (int x, int y) {this-> coord_x = x; this-> coord_y = y;} int Fish: get_score () {return this-> image_index * 2 + 1 ;} void Fish: wander () {this-> image_index ++; if (this-> image_index >=3) {this-> image_index = 0 ;} this-> _ image-> setTexture (this-> _ textture_vec.at (this-> image_index); float degrees = rand ()/rand (); this-> setRotation (degrees); int len = 6; double len_x = len * sin (degrees); double len_y = len * cos (degrees ); this-> coord_x + = len_y; this-> coord_y + = len_x;} CCPoint Fish: getPoint () {return CCPoint (coord_x, coord_y );}

Fish is a Fish, inherited from CCSprite, and the final function is wander (). It is used to wander the Fish in the Fish pool! First, a random angle is obtained, and then the Fish is rotated, and then the line of defense is directed at the head! Update the x and y coordinates! While moving again, we need to constantly change the pictures of fish, because the tail is constantly swinging!

Then there is the FishNet class.

// Header file # pragma once # include "cocos2d. h "# include" cocos-ext.h "# include" Fish. h "# include <vector> using namespace std; using namespace cocos2d; using namespace cocos2d: extension; class HelloWorld; # define FISHNETSIZECCSize (90,90) class FishNet: public CCSprite {public: fishNet (HelloWorld * scene );~ FishNet (void); bool init (); void add_fish (Fish * fish); void reset (); void update (float tick); void enable (); void disable (); bool is_enable (); void scatter (); void recycle (); private: int _ image_index; CCSprite * _ image; vector <CCTexture2D *> _ textture_vector; vector <Fish *> fish_vect; bool _ enable; HelloWorld * _ scene;}; // implementation code # include "FishNet. h "# include" HelloWorldScene. h "# include" GameStruct. h "FishNet: FishNet (Hell OWorld * scene) {this-> _ image = NULL; this-> _ image_index = 0; this-> _ enable = false; this-> _ scene = scene;} FishNet: :~ FishNet (void) {} void FishNet: reset () {this-> _ image_index = 0;} bool FishNet: init () {CCSprite: init (); this-> setContentSize (FISHNETSIZE); this-> _ image = CCSprite: create ("fish_net0.png"); this-> _ image-> setPosition (CCSize (FISHNETSIZE. width/2, FISHNETSIZE. height/2); this-> addChild (this-> _ image); for (int I = 0; I <7; I ++) {CCSprite * image = CCSprite:: create (fish_net_image [I]. c_str (); CCTexture2D * textture = image-> GetTexture (); this-> _ textture_vector.push_back (textture);} return true;} void FishNet: add_fish (Fish * fish) {fish-> setPosition (CCSize (FISHNETSIZE. width/2, FISHNETSIZE. height/2); this-> fish_vect.push_back (fish); this-> addChild (fish);} void FishNet: scatter () {for (vector <Fish *> :: iterator iter = this-> fish_vect.begin (); iter! = This-> fish_vect.end (); iter ++) {this-> removeChild (* iter, true); // (* iter) -> setPosition (this-> getPosition (); (* iter)-> setCoord (this-> getPositionX (), this-> getPositionY ()); this-> _ scene-> generate_fish (* iter);} this-> fish_vect.clear ();} void FishNet: recycle () {if (this-> is_enable ()) {for (vector <Fish * >:: iterator iter = this-> fish_vect.begin (); iter! = This-> fish_vect.end (); iter ++) {this-> removeChild (* iter, true); this-> _ scene-> add_score (* iter) -> get_score ();} this-> fish_vect.clear ();} void FishNet: update (float tick) {this-> _ image_index ++; if (this-> _ image_index = 7) {this-> _ image_index = 0; this-> disable (); return ;} this-> _ image-> setTexture (this-> _ textture_vector.at (this-> _ image_index);} void FishNet: enable () {this-> _ enable = true;} void FishNet: disable () {this-> _ enable = false; this-> scatter (); this-> _ scene-> recycle_fish_net ();} bool FishNet: is_enable () {return this-> _ enable ;}

FIshNet also inherits the subccsprite. Several Notes: The update function keeps updating the status of the fishing network! Scatter function. When the fishing net breaks down, release all the fish in the fishing net! Recycle: When the fishing net reaches the fish tank and the fishing net has not broken down, the player should score!

The following is the core HelloWorld class, which is actually called the pond class. But I thought it was modified on the HelloWorld project, so there was no adjustment! HelloWorld inherits the child CCLayer!

  

# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "# include" cocos-ext.h "# include" Fish. h "# include" FishNet. h "# include <vector> # define FONT_NAME_DEFAULT" msyh "# define font_size_24using namespace std; using namespace cocos2d; class HelloWorld: public cocos2d: CCLayerColor, public CCTouchDelegate {public: virtual bool init (); static cocos2d: CCScene * scene (); void menuClose Callback (CCObject * pSender); void reset (); void add_score (int inc); void update (float tick); CREATE_FUNC (HelloWorld); void random_generate_fish (); void generate_fish (Fish * fish); void recycle_fish (Fish * fish); void recycle_fish_net (); virtual void ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent ); // process the virtual void ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent) of the user press the event; // process the virtual void ccTouc Of The Touch Move event HesEnded (CCSet * pTouches, CCEvent * pEvent); // handle virtual void ccTouchesCancelled (CCSet * pTouches, CCEvent * pEvent) events where the Touch is interrupted, for example, you have made a call. Private: int score; vector <Fish *> fish_vec; FishNet * fish_net; CCLabelTTF * score_hint; CCLabelTTF * score_label;}; # endif // _ success __# include "HelloWorldScene. h "# include <math. h> USING_NS_CC; CCScene * HelloWorld: scene () {// 'Scene 'is an autorelease object CCScene * scene = CCScene: create (); // 'player' is an autorelisted object HelloWorld * layer = HelloWorld: create (); // add layer as a child To scene-> addChild (layer); // return the scene return scene;} bool HelloWorld: init () {if (! CCLayerColor: initWithColor (ccc4 (255,255,255,255) {return false;} CCSize visibleSize = CCDirector: shareddire()-> getVisibleSize (); CCPoint origin = CCDirector: shareddire () -> getVisibleOrigin (); CCScale9Sprite * board = CCScale9Sprite: create ("board.png"); board-> setPosition (CCSize (visibleSize. width/2, 20); this-> addChild (board); this-> score_hint = CCLabelTTF: create ("score:", "Arial", 20.0); This-> score_hint-> setPosition (CCSize (visibleSize. width-100,20); this-> addChild (this-> score_hint); this-> score_label = CCLabelTTF: create ("0", "Arial", 20.0 ); this-> score_label-> setPosition (CCSize (this-> score_hint-> getPositionX () + 70, 20); this-> addChild (this-> score_label ); CCScale9Sprite * tank = CCScale9Sprite: create ("fish_tank.png"); tank-> setPosition (CCSize (30,30); this-> addChild (tank); for (int I = 0; I <10; I ++) {Fish * fish = new Fish (); fish-> init (); fish-> setPosition (fish-> getPoint ()); this-> addChild (fish); this-> fish_vec.push_back (fish);} this-> setTouchEnabled (true); CCDirector: sharedDirector ()-> getTouchDispatcher () -> addTargetedDelegate (this, 0, true); this-> fish_net = new FishNet (this); this-> fish_net-> init (); this-> schedule (schedule_selector (HelloWorld: update), 0.2); this-> score = 0; return true;} vo Id HelloWorld: reset () {this-> score = 0;} void HelloWorld: add_score (int inc) {this-> score + = inc; CCString * str = CCString:: createWithFormat ("% d", this-> score); this-> score_label-> setString (str-> getCString ();} void HelloWorld: update (float tick) {CCSize visibleSize = CCDirector: shareddire()-> getVisibleSize (); int generate_cnt = 0; for (vector <Fish * >:: iterator iter = this-> fish_vec.begin (); iter! = This-> fish_vec.end (); iter ++) {Fish * fish = * iter; fish-> wander (); fish-> setPosition (fish-> getPoint ()); if (fish-> getPositionX ()> visibleSize. width | fish-> getPositionY ()> visibleSize. height) {iter = this-> fish_vec.erase (iter); this-> removeChild (fish, true); generate_cnt ++ ;}}for (int I = 0; I <generate_cnt; I ++) {random_generate_fish ();} if (this-> fish_net-> is_enable () {this-> fish_net-> update (tick);} void HelloW Orld: random_generate_fish () {Fish * fish = new Fish (); fish-> init (); fish-> setPosition (fish-> getPoint ()); this-> addChild (fish); this-> fish_vec.push_back (fish);} void HelloWorld: generate_fish (Fish * fish) {this-> fish_vec.push_back (fish ); this-> addChild (fish);} void HelloWorld: recycle_fish (Fish * fish) {this-> removeChild (fish, true);} void HelloWorld: recycle_fish_net () {this-> removeChild (this-> fish_net, true);} v Oid HelloWorld: ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent) {CCSetIterator it = pTouches-> begin (); CCTouch * touch = (CCTouch *) (* it ); CCPoint point = convertTouchToNodeSpace (touch); this-> fish_net-> reset (); this-> fish_net-> enable (); this-> fish_net-> setPosition (point ); this-> addChild (this-> fish_net); for (vector <Fish *>: iterator iter = this-> fish_vec.begin (); iter! = This-> fish_vec.end ();) {Fish * fish = * iter; if (abs (fish-> getPositionX ()-point. x) <= 15 & abs (fish-> getPositionY ()-point. y) <= 15) {iter = this-> fish_vec.erase (iter); this-> removeChild (fish, true); this-> fish_net-> add_fish (fish ); continue;} iter ++;} void HelloWorld: ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent) {CCSetIterator it = pTouches-> begin (); CCTouch * touch = (CCTouch *) (* it); CCPoint point = convertTouchToNodeSpace (touch); this-> fish_net-> setPosition (point ); if (this-> fish_net-> is_enable () {if (this-> fish_net-> getPositionX () <90 & this-> fish_net-> getPositionY () <90) {this-> fish_net-> recycle (); this-> fish_net-> disable () ;}} void HelloWorld: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent) {this-> fish_net-> disable ();} void HelloWorld: ccTouchesCancelled (CCSet * pTouches, CCEvent * pEvent) {} void HelloWorld: Pushed (CCObject * pSender) {# if (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) | (CC_TARGET_PLATFORM = CC_PLATFORM_WP8) CCMessageBox ("You pressed the close button. windows Store Apps do not implement a close button. "," Alert "); # else CCDirector: shareddire()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0); # endif}

Several Functions in the Code are obvious! 1: random_generate_fish: generate new fish at random; update: Heartbeat function, which is executed five times per second. It constantly verifies whether a fish exists. If it exists, it will be reclaimed! Constantly judge whether the fishing network has reached the fish tank. If it has arrived, clear the fish in the fishing network and add extra points to the players! Verify whether the fishing network is damaged. If it is damaged, put the fish in the fishing network into the pond!

If you are interested, you can ask me for the entire project. The original android version is also available! In addition, I haven't written C ++ for too long. The name and stl are a little unfamiliar!

I hope to help you. You can also find me if you have more than one android project every year!

  

Related Article

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.