Starting from this section, I will explain how to create a game similar to flappy bird.
I. Sharpen the knife without cutting into the firewood. First, let's introduce the game development environment and the choice of the game engine:◆ Development language: C ++ ◆ Development Environment: VS2012: ◆ game engine: cocos2dx-3.0beta2: License =
2, back up, we extract the downloaded cocos2dx-3.0 to somewhere, Here suppose is to extract to C: \ Program Files below.
3. Go to the C: \ Program Files \ cocos2d-x-3.0beta2 \ tools \ project-creator \ directory from cmd
4. Under this directory, you will see a script tool created by the project, that is, create_project.py.
5. Run the create_project.py script (ensure that you have successfully installed the python runtime environment ),
● Two methods are provided here: 1) if the script is run without any parameters, a Project Creation dialog box will pop up, such
2) Of course, you can also create parameters directly in the command line. For detailed parameters, you can use create_project.py-h to query and learn-n: Name-k: package name-l: programming Language-p: Project Storage path here is a simple example: create_project.py-n Earlybird-k com. earlybird. oiteboys-l cpp-p e: \ git \ cocos2dx ### begin copy enginewaitting copy cocos2d... cocos2d: Done! Proj. ios_mac: Done! Proj. android: Done! Proj. win32: Done! Proj. linux: Done! ### New project has been created in this path: e:/git/cocos2dx/EarlybirdHave Fun! --------------------------------------
2. Be familiar with the game elements:1. Protagonist: birdie (this game has three birds of different colors, each time the game starts to randomly select a bird)
Note that the three pictures above represent only three different states of a bird. Yes, we can see that a bird is continuously waving its wings and passing through the pipe as a frame animation. Then, the pictures of two birds are similar:
2. obstacle: Water Pipe (the Water Pipe consists of up and down two, and the brown water pipe represents the pipe that has passed)
3. Game background (including day and night backgrounds and ground ):
4. scores in the game (scores in the game are divided by numbers and each digit is displayed ):
5. Game start and end
You may have discovered that each image is shown in a large image, rather than a single image. Yes, in the process of making a game, we usually pack all the images used in a large image, so that at the beginning of the game, we only need to load an image at a time.
Iii. Design of the material Reading Class: So the question is: how can we obtain a specific resource from during the game production process? The file records the names, sizes, and coordinates of each resource in the image. The format is as follows: name width and height start coordinate end coordinate bird0_1/48/48/0.0546875/0.9472656/0.046875 with this file, we can easily retrieve resources from the large image.
For the sake of standardization, we have designed an AtlasLoader class to specifically process the acquisition of image resources. We design this class as global sharing, that is, there is only one class object in the global. The Atlas struct stores the information of each row read from the file.
typedef struct _atlas{ char name[255]; int width; int height; Point start; Point end;} Atlas;
Load image resources:
void AtlasLoader::loadAtlas(string filename){ auto textureAtlas = Director::getInstance()->getTextureCache()->addImage("atlas.png"); this->loadAtlas(filename, textureAtlas);}
After the resource file is loaded, we split the resource from the big image one by one and put it in a Map <std: string, SpriteFrame *> _ spriteFrames, in this way, when we need to use a certain resource, we can directly obtain it from this Map.
void AtlasLoader::loadAtlas(string filename, Texture2D *texture) { string data = FileUtils::getInstance()->getStringFromFile(filename); unsigned pos; Atlas atlas; pos = data.find_first_of("\n"); string line = data.substr(0, pos); data = data.substr(pos + 1); while(line != ""){ sscanf(line.c_str(), "%s %d %d %f %f %f %f %f", atlas.name, &atlas.width, &atlas.height, &atlas.start.x, &atlas.start.y, &atlas.end.x, &atlas.end.y); atlas.start.x = 1024*atlas.start.x; atlas.start.y = 1024*atlas.start.y; atlas.end.x = 1024*atlas.end.x; atlas.end.y = 1024*atlas.end.y; pos = data.find_first_of("\n"); line = data.substr(0, pos); data = data.substr(pos + 1); Rect rect = Rect(atlas.start.x, atlas.start.y, atlas.width, atlas.height); auto frame = SpriteFrame::createWithTexture(texture, rect); this->_spriteFrames.insert(string(atlas.name), frame); }}
Iv. Summary: Well, In this section, we will introduce some materials used in the game and explain how to obtain these resources during the game production process. In subsequent articles, we will introduce how to use these materials to show the rich and colorful game world.
5. Related Knowledge points: 1. design mode (Singleton mode) 2. Texture detailed code, please move to github https://github.com/OiteBoys/Earlybird