Implementation Principles of JavaScript small-sized aircraft-hitting games _ javascript skills

Source: Internet
Author: User
Tags rounds
This is a small game that we will bring to you: plane. Er... I can't write any amazing games myself. I can only write small games, with a small amount of code, and I can learn from them. I mainly think about the ideas and the code is large, it is estimated that no one will study it... Instructions: Move Up, down, left, right, and left with spaces. Each time an attacker hits an enemy plane, an increase of 100 points means that the number of rounds of a player's plane increases by one to four points, if it is hit by an enemy plane or the enemy plane is thrown to the bottom, it will be lost ....

DEMO code: http://demo.jb51.net/js/FlyBeat/index.html

The current functions of the game are still relatively simple .... It seems that it is not good to post a source code, so this time I will write the idea...

The game is mainly divided into 4 js files, and 4 js files contain 4 categories.
1: aircraft --- Flyer

The Code is as follows:


// Dom elements corresponding to aircraft
This. dom = null;
// Whether it is alive
This. isLive = true;
// Whether it is moving
This. isMove = false;
// Mobile ID
This. moveId = null;
// Whether or not it is being played
This. isSend = false;
// How many rounds have been sent (displayed on the screen)
This. nowBullet = 0;
// Game background Dom
GamePanel: null,
// Game background width
GameWidth: 0,
// Game background height
GameHeight: 0,
// Aircraft movement speed
Movepx: 10,
// Aircraft movement frequency
Movesp: 30,
// Aircraft bullet level
BulletLevel: 1,
// Maximum number of rounds (displayed on the screen)
MaxBullet: 12,
// Corresponding direction key value
KeyCodeAndDirection :{
37: "left ",
38: "up ",
39: "right ",
40: "down"
},


The above are the attributes that an airplane should have ....

AircraftIn addition to some fixed attributes, there should also be a blood volume, but this is a simple version, you can add it yourself.

More importantlyMobile, bullet launch, explosion, and other methods.

Moving: it is actually capturing Keyboard Events. If you simply press the left of the keyboard and then the plane moves a few pixels to the left, you will find that the plane is very hard to move, or operation latency, especially when you want to press and hold the left of the keyboard, when it moves, the latency is very serious and the operation is not smooth. Generally, when you press the keyboard, call a setInterval function to keep the plane moving. When the keyboard is released, the Movement stops, so that the movement is smooth.

Trigger a Bullet: in fact, the user presses a space and then triggers a keyboard event. This event generates a Bullet class object and then let it fly out. This class will be mentioned later.

Explosion: When an airplane hits an enemy plane, it will trigger an explosion and end the game. Of course, whether the plane hits the enemy's plane is detected by the enemy's plane.

These are some basic events. There are also extended events .. You can add

2: Bullet class-Bullet

The Code is as follows:


// Bullet Dom Element
This. dom = null;
// Bullet movement speed
Movepx: 8,
// Bullet movement frequency
Movesp: 10,


There are two basic bullet methods: MobileAnd Checks whether an attacker has been hit by an enemy.

Movement: the movement of bullets is much simpler, so it keeps running up and top is always reduced and OK.

Check whether an attacker hits an enemy's machine: This method transmits the list of the enemy's machines to traverse the enemy's machine, and checks whether the bullet is in conflict with the enemy's machine. If the bullet is in conflict with the enemy's machine, the attacker skips the attack.
3: enemies-Enemy

The Code is as follows:


// Element of the enemy dom
This. dom = null;
// Whether
This. isLive = true;
// Horizontal movement speed of enemy planes
Movepx: 6,
// Vertical movement speed of enemy planes
Movepy: 4,
// Enemy's mobile frequency
Movesp: 75,


The basic methods for enemy planes are: Move, whether to hit the plane Flyer players, or not

Move: It is how the enemy plane moves. I set it to fly from top to bottom, let it fly from left to back, hit the end of the right, and turn it to fly.

Whether to hit the plane Flyer: During the constant movement of the enemy plane, the system constantly checks whether there is an intersection between the plane Flyer and the enemy plane. If there is a conflict between the two, the game ends. Otherwise, the game is skipped.

Explosion: An event triggered when an enemy plane is hit by a bullet or hits a plane Flyer.

4: Game control-Game

It contains an extension method: to delete a specified element from an array.

The Code is as follows:


// Extend the array method to delete a specific value
Array. prototype. remove = function (obj ){

For (var I = 0, l = this. length; I <l; I ++ ){
If (this [I] = obj ){
This. splice (I, 1 );
Return this;
}
}
Throw "The Array has no this Obj ";
}


The rest are those that initialize enemy planes, planes, and control the game process, as well as some methods such as modifying scores and ending the game. There is nothing to talk about.

In this case, the game itself is relatively simple, and the source code below is annotated. This time it is written in more detail... If you are interested, you can continue to improve yourself... If you have any questions, please click it... For more information, see.
Packaging http://www.jb51.net/jiaoben/32660.html
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.