HTML5 - Canvas: Game tank simple - part 2 (end)

Video HTML5 - Canvas: Game tank simple - part 2 (end)

Following the previous section , in this section I will perform the main content:
- Change the key function to the tanks began to move smoother.
- Add the obstacles in the game to shoot and destroy tanks.
- A Some visual effects, sound.

 

Source code and Class Diagram

Download project.

Class diagram:

HTML5 - Canvas: Game tank simple - part 2 (end)

Input processing mechanism

In the previous section, I use the trigger mechanism of action of the objects in the game every time you get the corresponding event from the user. This makes control difficult and not continuous. The solution to this problem is that I will write a separate method of treatment necessary in each object and transmit the status of the keyboard, mouse, ... on how to handle this:

// Sample.html:
 
function update () {
    _tank.handleInput (_keyStates);
    // ...
}
function canvas_keyDown (e) {
    if (AVAILABLE_KEYS.indexOf (e.keyCode)! = - 1)
        _keyStates [e.keyCode] = true ;
}
function canvas_keyUp (e) {
    if (_keyStates [e.keyCode])
        _keyStates [e.keyCode] = false ;
}
// Tank.js:
 
Tank.prototype.handleInput = function (keyStates) {
    this .speedX = 0;
    this .speedY = 0;
 
    if (keyStates [Keys.KEY_W])
        this .moveUp ();
    if (keyStates [Keys.KEY_S])
        this .moveDown ();
    if (keyStates [Keys.KEY_A])
        this .moveLeft ();
    if (keyStates [Keys.KEY_D])
        this .moveRight ();
    this .move ();
}

More obstacles: meteorite

The plot of the game is that players will be playing a tank with the task of protecting the earth from meteorites fell by shooting to destroy them. Thus the obstacles here is the meteorite comes above the canvas and fell with random direction.

// Sample.html:
var _obstacles = [];
 
function update () {
    _tank.handleInput (_keyStates);
 
    if (_obstacles.length <10)
        _obstacles.push ( new Obstacle (_canvas.width, _canvas.height, _context));
 
    for ( var i = 0; i <_obstacles.length; i ++)
        {
        var ob = _obstacles [i];
        ob.update ();
    }
    _tank.update (_obstacles);
 
    draw ();
}
 
// Obstacke.js:
 
function Obstacle (mapWidth, exampleadd, context) {
    this .mapWidth = mapWidth;
    this .mapHeight = exampleadd;
    this .context = context;
 
    this .size = Math.floor (Math.random () * 60) +60;
 
    this .left = Math.floor (Math.random () * (mapWidth- this .size)) + 1;
    this .top = -Math.floor (Math.random () * exampleadd);
 
    this .speedY = Math.floor (Math.random () * 4) +1;
    this .speedX = Math.floor (Math.random () * 3) -3;
}

To draw the asteroid, I use images taken from a file rock.png. In the method update (), I will let the meteorite can be reflected back by hitting a double into the canvas to ensure they are not beyond the scope of screen, and also to increase the degree of difficulty for players.

var rockImg = new Image ();
rockImg.src = "rock.png" ;
// ...
Obstacle.prototype.draw = function () {
    this .context.drawImage (rockImg, this .left, this .top, this .size, this .size);
}
Obstacle.prototype.update = function () {
    if ( this .left <0 || this .right> this .mapWidth)
        this .speedX = - this .speedX;
 
    this .top + = this .speedY;
    this .left + = this .speedX;
    this .right = this .left + this .size;
    this .bottom = this .top + this .size;
}

Result:

HTML5 - Canvas: Game tank simple - part 2 (end)

Making different meteorites

You can use several different image and randomly assigned for each meteorite that they have specific characteristics in terms of shape and size. Here I chose a simpler approach is rotated each asteroid a random angle.

How to do is use the methods context.translate () to move the origin to the center of the meteorite draw, rotate with context.rotate () and draw. Remember to save and restore the state of the context when you perform this operation:

function Obstacle (mapWidth, exampleadd, context) {
    // ...
    this .halfSize = Math.floor (Math.random () * 30) +30;
    this .size =  this .halfSize * 2;
 
    this .angle = (Math.PI / 180) * Math.floor (Math.random () * 360);
    // ...
 
}
Obstacle.prototype.draw = function () {
    this .context.save ();
    this .context.translate ( this .left + this .halfSize, this .top + this .halfSize);
    this .context.rotate ( this .angle);
    this .context.drawImage (rockImg, - this .halfSize, - this .halfSize, this .size, this .size);
    this .context.restore ();
}

HTML5 - Canvas: Game tank simple - part 2 (end)

Check collision

In this example, I just do functional testing of a meteorite collision with bullets. Because the size of the relatively small bullets may consider it to be 1 point. So the method collide () after I just passed two parameters x, y coordinates representing the center of the bullet. The simple test is to identify a point in a rectangle or not.

Obstacle.prototype.collide = function (x, y) {
    return this .left <= x && this .right> = x &&
        this .top <= y && this .bottom> = y;
}

Next, I'll use it in class Tank:

Tank.prototype.update = function (obstacles) {
 
    for ( var i = 0; i < this .balls.length; i ++)
    {
        var ball = this .balls [i];
        if (ball.update ())
        {
            this .balls.splice (i - 1);
        } else {
            for ( var j = 0; j <obstacles.length; j ++)
            {
                var ob = obstacles [j];
 
                if (ob.collide (ball.cx, ball.cy))
                {
                    // Do something ích
                    this .balls.splice (i - 1);
                    break ;
                }
            }
        }
    }
}

Explosive Effects

To make this effect, I just draw a picture meteorite after it collided other.

Obstacle.prototype.draw = function () {
    var img = this .isCollided? explodeImg: rockImg;
 
    this .context.save ();
    this .context.translate ( this .left + this .halfSize, this .top + this .halfSize);
    this .context.rotate ( this .angle);
    this .context.drawImage (img, - this .halfSize, - this .halfSize, this .size, this .size);
    this .context.restore ();
}

The display pictures of the asteroid explosion can not see due to take place up too fast. So, I used a counter to maintain the "lifetime" of meteorites after collision:
When collision, isCollided flag is enabled (true), the counter is set to 3. explosionCounter
After each update, This variable will be reduced by 1 and the 0, flag isExploded will be enabled to notify the application deletes it from the list of meteorites.

Obstacle.prototype.explode = function () {
    this .isCollided = true ;
}
 
Obstacle.prototype.update = function () {
    if ( this .isCollided)
        this .explosionCounter--;
    if ( this == .explosionCounter 0)
        this .isExploded = true ;
 
    if ( this .left <0 || this .right> this .mapWidth)
        this .speedX = - this .speedX;
    if ( this .bottom> this .mapHeight)
        this .explode ();
 
    this .top + = this .speedY;
    this .left + = this .speedX;
    this .right = this .left + this .size;
    this .bottom = this .top + this .size;
}

Also, I added an explosive sound effects when the methods explode () is called:

var snd = new Audio ( "explosion.wav" );
Obstacle.prototype.explode = function () {
    this .isCollided = true ;
 
    snd.currentTime = 0;
    snd.play ();
}

Complete example

Adding extra functions follows:

- Allow only move horizontally: only handle two keys A and D
- Draw Background: use a photograph to paint full-screen canvas.
- Score: create a Tank class variable banding scores in the 1 each bullet collision detection with meteorites.

// Sample.html:
function draw () {
    clear ();
    _context.drawImage (_backgroundImg, 0.0, _canvas.width, _canvas.height);
    for ( var i = 0; i <_obstacles.length; i ++)
    {
        _obstacles [i] .draw ();
    }
    _tank.draw ();
    _context.fillText ( "Scores" + _tank.scores, 10.20);
}

HTML5 - Canvas: Game tank simple - part 2 (end)

What next?

To be a true game, then this example is missing many features to reality. Such as checking meteorite impact with tanks, HP, level, ... and can add more sophisticated enemy. These functions are not hard to do but I will stop at this article to regain part 2 covered in other articles, and more logical order.

YinYang's Programming Blog

How to enable / disable TouchDrive mode when playing Asphalt 9: Legends

How to enable / disable TouchDrive mode when playing Asphalt 9: Legends

TouchDrive is a new feature added in Asphalt 9: Legends racing game. This autopilot feature will help us play Asphalt 9 much easier and more convenient than previous versions. Players can optionally turn on, turn off TouchDrive to change

How to unlink Facebook accounts in Free Fire

How to unlink Facebook accounts in Free Fire

Detailed steps guide to unlinking from the Facebook account registered in Garena Free Fire.

Summary of FIFA Online 4 errors and how to fix them

Summary of FIFA Online 4 errors and how to fix them

Today 14/6, FIFA Online 4 officially Open Beta in Vietnam, with countless exciting events and a series of interesting features for players to experience to share the exciting atmosphere of the 2018 World Cup.

How to back up Adorable Home game data

How to back up Adorable Home game data

Backup data when playing Adorable Home game is very necessary, this will help you keep all game data when updating or encounter errors related to losing Adorable Home game account.

How to take screenshots of the game screen on GameLoop

How to take screenshots of the game screen on GameLoop

GameLoop supports players taking screenshots when playing games. This is a nice and useful feature when playing games on this Android emulator but is often overlooked.

How to switch from Survival to Creative mode in Mini World: Block Art

How to switch from Survival to Creative mode in Mini World: Block Art

How to switch between these two game modes will help you not have to create a new map once youve built your own work in Mini World: Block Art

Adorable Home: List of code and how to enter the code in the game

Adorable Home: List of code and how to enter the code in the game

Instructions on how to find and enter special Adorable Home code in the game to earn rewards from the game developer.

Quickly transfer your international PUBG Mobile account to PUBG Mobile VN so that it will not be reset before December 31

Quickly transfer your international PUBG Mobile account to PUBG Mobile VN so that it will not be reset before December 31

How to convert international PUBG Mobile accounts to PUBG Mobile VNG without data loss.

Dragon Raja: Instructions for logging in and linking accounts

Dragon Raja: Instructions for logging in and linking accounts

Login and link your account to save character data in the mobile game Dragon Raja - mobile game with very nice graphics.

GameDev - Intersection of Two Lines (intersection of two lines)

GameDev - Intersection of Two Lines (intersection of two lines)

From two straight lines (or vector) in a 2D plane, we can find their intersections to calculate the angle of reflection and direction of travel.

Should use Auto FIFA Online 4 to hack FO4 game

Should use Auto FIFA Online 4 to hack FO4 game

Besides the values ​​that Auto FIFA Online 4 brings, it is very difficult for gamers to "keep themselves", we will face unfortunate risks.

PUBG Mobile: 6 types of recovery props that help you survive longer

PUBG Mobile: 6 types of recovery props that help you survive longer

PUBG Mobile, the famous survival game adapted from the previous version of PlayerUnknowns Battleground on PC, just like the name of this game genre said, survival is a top goal, you need to survive and go through the attack from the enemy, then n

How to make automatic doors in Mini World: Block Art

How to make automatic doors in Mini World: Block Art

You can create automatic doors using widgets available in Mini World: Block Art

How to install the game Clash Royale for Vietnamese gamers

How to install the game Clash Royale for Vietnamese gamers

Clash Royale is a name that is not new to the World gaming community, however, in Vietnam, this game has not been officially launched. If you want to play Clash Royale, you can follow the following instructions to install Clash Ro game

PUBG Mobile: Top 5 best camp locations on Erangel map

PUBG Mobile: Top 5 best camp locations on Erangel map

Especially in this map we also have many ideal locations for you to land. So where is the most favorable camp location in the map Erangel in PUBG Mobile game

Instructions on how to play the AOE Empire on Garena

Instructions on how to play the AOE Empire on Garena

Garena is one of the world famous gaming software, and playing Empire on Garena is also a way for gamers to connect with other passionate players around the world.

How to play Garena Contra: Return using the keyboard

How to play Garena Contra: Return using the keyboard

In addition to downloading and installing Garena Contra Return on PC, we can also set up to play Garena Contra Return with the keyboard to improve the experience.

Fix Your system is low on memory on GameLoop

Fix Your system is low on memory on GameLoop

Your system is low on memory. Close some applications and restart the game is one of the common and quite common errors on the GameLoop emulator.

How to open an NPC store in Mini World: Block Art

How to open an NPC store in Mini World: Block Art

Shop in Mini World: Block Art will help you trade, exchange with other players a variety of items and useful items while playing this survival game.

MFC - Lession3: Tree Control and List Control

MFC - Lession3: Tree Control and List Control

Tree controls and list controls are two commonly used controls in the problem of displaying data, retrieve, manage resources, directories, ...