Flappy Pig Small Game Source Analysis (2)--anatomy option

Source: Internet
Author: User

Today continue to analyze the source code of Flappy Bird. Revisit the directory structure of the source code.

In the first chapter of this series we analyzed the Game.js file, which is the architecture of the entire program. This article we look at the Option.js file, the content of this file is very simple, mainly to configure some of the game elements of the parameters.

We should follow the usual practice of the source code:

varFlappy = (function(self) {' Use strict '; //SetSelf.option = {        //gravitational acceleration, screen pixels are different from actual physical meters, so there are conversionsg:400,        //jumping speed, controlling the bounce power of a pigv0:400,        //Column Movement Speedvp:2.5,        //frequency, control the number of animation frames, default 20msFrequency:20,        //number of Levelslevels:100,        //blank distance at the beginningsafelift:500,        //floor height (related to picture)Floorheight:64,        //width of pigPigwidth:33,        //the height of the pigPigheight:30,        //current height of pigpigy:300,        //distance from the left of the pig,Pigleft:80,        //Pillar HTMLpillarhtml: ' <div class= ' top ' ></div><div class= ' bottom ' ></div> ',        //Column WidthPillarwidth:45,        //height of column up and downpillargapy:108,        //left and right interval width of columnpillargapx:250,        //The base position value of the upper pillar (which is the top value, related to the CSS notation)Pillartop:-550,        //base position value of the lower pillarPillarbottom:-500    }; returnSelf//when this is done, Flappy is an object with the option attribute.}) (Flappy|| {});

About which var flappy = (function (self) {

Other details

}) (Flappy | | {}) If you still don't understand it, you can look at the first article in this series.

After reading this source code I believe that most of the students do not have a little pressure, the only possible question is, why do you want to do so? Why should we use a separate file to store these configuration parameters?

We often hear some of the jargon of programming: low-coupling, high cohesion, maintainability, modularity, and so on, leaving out these vague notions, and we're talking about a concept that is ubiquitous in real life-the single principle of responsibility.

When cleaning, the students who sweep the floor only responsible for cleaning, cleaning the students only clean the glass, cleaning the blackboard is only responsible for cleaning the blackboard, we do not involve each other. Check when found not sweep clean, it must be the responsibility of the sweeping classmate, the corresponding glass is not clean to find the students to clean glass to be responsible. Once the students sweeping the floor is not only responsible for sweeping, but also to help wipe the glass, check if the glass did not wipe clean, this time want to know who is responsible for is very troublesome, at least to the two people called to ask a clear, perhaps they two again to evade each other. In the program design we call each other a single responsibility principle, once there is any problem, we can quickly find which module is responsible for the problem.

Understand what a single responsibility principle is, Let's take a look at this option.js file, and it's easy to know that the responsibility of this option.js file is to configure the game parameters for use by other modules, and once you need to adjust the parameters, for example, I want to make the pig bigger, the distance between the pillars is a little bit higher, and I just need to change option. The. js file is ready, and he will immediately react to each of the files that were introduced to him. If you write these configuration items separately in the respective modules, you can imagine how painful it is if you need to adjust them.

The content of this section is simple, but the reader needs to deepen his understanding, form the idea of programming and apply it.

Flappy Pig Small Game Source Analysis (2)--anatomy option

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.