Home > Article > Web Front-end > JavaScript small masturbation game implementation principle explanation_javascript skills
How to play: Control movement up, down, left, and right, and use the space to fire. Every time an enemy plane is hit, 100 points are added. For every 5,000 points, the number of rounds fired by the player's plane is increased by one, up to four. If the player is hit by an enemy plane or the enemy plane flies to the bottom, he loses. . . .
Demo code:http://demo.jb51.net/js/FlyBeat/index.html
The current functions of the game are relatively simple. . . . It seems that it is not good to just post the source code, so I will write down my ideas this time. . .
The game is mainly divided into 4 js files, and each of the 4 js files contains 4 classes.
1: Aircraft type---Flyer
Aircraft In addition to some fixed attributes, it should also have blood volume, but this is a simple version, you can add it yourself.
There should also be ways to move, fire bullets, explode, etc.
Movement: In fact, it is to capture keyboard events. If you simply press the left key of the keyboard, and then the aircraft moves a few pixels to the left, you will find that the aircraft moves very stiffly, or the operation is delayed. Especially when you want to hold down the left side of the keyboard, when it moves, there is a serious delay and the operation is not smooth. So generally: when you press the keyboard, call a setInterval function to keep the aircraft moving. When you release the keyboard, the movement stops, so that the movement is very smooth.
Firing a bullet: In fact, the user presses the space button, and then triggers a keyboard event. This event generates an object of the Bullet class and then lets it fly out. This category will be discussed later.
Explosion: When the plane hits an enemy plane, the plane will trigger an explosion event and end the game. Of course, the detection of whether the aircraft has hit an enemy aircraft is done at the enemy aircraft.
These are some basic events. There are also extended events. . You can add it yourself
2: Bullet type--Bullet
Movement: The movement of the bullet is much simpler, just keep running up, and the top will keep decreasing.
Detect whether the enemy aircraft is hit: Pass the list of enemy aircraft into the method, traverse the enemy aircraft, detect whether the bullet collides with the enemy aircraft, if there is a collision with the enemy aircraft, the enemy aircraft will explode, if not, it will be skipped.
3: Enemy aircraft type--Enemy
이동 : 적기가 위에서 아래로 날아간 다음 왼쪽에서 오른쪽으로 날아가서 오른쪽 끝을 맞고 돌아서도록 설정했습니다.
플라이어 플레이어와 충돌 여부: 적 항공기가 계속 이동하는 동안 플라이어 항공기와 적 항공기가 교차하는지 지속적으로 감지하면 둘이 폭발하고 게임이 종료됩니다. 그렇지 않으면 건너뜁니다.
폭발: 적 항공기가 총알에 맞거나 플라이어 항공기와 충돌할 때 발생하는 이벤트입니다.
4: 게임 조작형--게임
확장 메소드가 포함되어 있습니다: 배열에서 지정된 요소를 삭제합니다