Home > Web Front-end > H5 Tutorial > Detailed explanation of horizontal shooting game based on HTML5

Detailed explanation of horizontal shooting game based on HTML5

黄舟
Release: 2017-03-24 15:33:56
Original
2920 people have browsed it

Function Description:

Based onHTML5's horizontal version of the shooting game, refer to the flash game "Double Agent". The left and right arrow keys control movement, the down arrow key to squat, the up arrow key to jump, please turn off the input method before experiencing it.

## This game is based on the self-developed HTML5 game framework cnGameJS

Effect preview:

Detailed explanation of horizontal shooting game based on HTML5

Implementation analysis:

 

1. About multi-layer maps  

In the previous HTML5 game "Tank Support Team", the map used was only. A simple single-layer map means that there is only one layer of open space except for stones. However, this single-layer map has relatively large limitations. If you need to implement scene-based games (such as Super Mario and the above games). ), a map with only one layer is often not enough, because in addition to the obstacles where the game protagonist is standing, we also have game background and other elements (such as the wall behind, etc.), so we need to layer the map objects to achieve multiple The purpose of layer display.

New layer object:

Each layer object maintains the sprite of the layer, is responsible for updating and drawing them, and can obtain the specified coordinates on the matrix of the layer. The value of the layer object is as follows:

/**
        *层对象
        **/                               
        var layer = function(id,mapMatrix, options) {
    
            if (!(this instanceof arguments.callee)) {
                return new arguments.callee(id,mapMatrix, options);
            }
            this.init(id,mapMatrix, options);
        }
        layer.prototype={
            
            /**
            *初始化
            **/
            init: function(id,mapMatrix,options) {
                /**
                *默认对象
                **/    
                var defaultObj = {
                    cellSize: [32, 32],   //方格宽,高
                    x: 0,                      //layer起始x
                    y: 0                  //layer起始y
    
                };    
                options = options || {};
                options = cg.core.extend(defaultObj, options);
                this.id=options.id;
                this.mapMatrix = mapMatrix;
                this.cellSize = options.cellSize;
                this.x = options.x;
                this.y = options.y;
                this.row = mapMatrix.length; //有多少行
                this.width=this.cellSize[0]* mapMatrix[0].length;
                this.height=this.cellSize[1]* this.row;
                this.spriteList=new cg.SpriteList();//该层上的sprite列表
                this.imgsReference=options.imgsReference;//图片引用字典:{"1":{src:"xxx.png",x:0,y:0},"2":{src:"xxx.png",x:1,y:1}}
                this.zIindex=options.zIndex;
            },
            /**
            *添加sprite
            **/            
            addSprites:function(sprites){
                if (cg.core.isArray(sprites)) {
                    for (var i = 0, len = sprites.length; i < len; i++) {
                        arguments.callee.call(this, sprites[i]);
                    }
                }
                else{
                    this.spriteList.add(sprites);
                    sprites.layer=this;
                }                
                
            },
            /**
            *获取特定对象在layer中处于的方格的值
            **/
            getPosValue: function(x, y) {
                if (cg.core.isObject(x)) {
                    y = x.y;
                    x = x.x;
                }
                var isUndefined = cg.core.isUndefined;
                y = Math.floor(y / this.cellSize[1]);
                x = Math.floor(x / this.cellSize[0]);
                if (!isUndefined(this.mapMatrix[y]) && !isUndefined(this.mapMatrix[y][x])) {
                    return this.mapMatrix[y][x];
                }
                return undefined;
            },
            /**
            *获取特定对象在layer中处于的方格索引
            **/
            getCurrentIndex: function(x, y) {
                if (cg.core.isObject(x)) {
                    y = x.y;
                    x = x.x;
                }
                return [Math.floor(x / this.cellSize[0]), Math.floor(y / this.cellSize[1])];
            },
            /**
            *获取特定对象是否刚好与格子重合
            **/
            isMatchCell: function(x, y) {
                if (cg.core.isObject(x)) {
                    y = x.y;
                    x = x.x;
                }
                return (x % this.cellSize[0] == 0) && (y % this.cellSize[1] == 0);
            },
            /**
            *设置layer对应位置的值
            **/
            setPosValue: function(x, y, value) {
                this.mapMatrix[y][x] = value;
            },
            /**
            *更新层上的sprite列表
            **/            
            update:function(duration){
                this.spriteList.update(duration);
                
            },
            /**
            *根据layer的矩阵绘制layer和该layer上的所有sprite
            **/
            draw: function() {
                var mapMatrix = this.mapMatrix;
                var beginX = this.x;
                var beginY = this.y;
                var cellSize = this.cellSize;
                var currentRow;
                var currentCol
                var currentObj;
                var row = this.row;
                var img;
                var col;
                for (var i = beginY, ylen = beginY + row * cellSize[1]; i < ylen; i += cellSize[1]) {    //根据地图矩阵,绘制每个方格
                    currentRow = (i - beginY) / cellSize[1];
                    col=mapMatrix[currentRow].length;
                    for (var j = beginX, xlen = beginX + col * cellSize[0]; j < xlen; j += cellSize[0]) {
                        currentCol = (j - beginX) / cellSize[0];
                        currentObj = this.imgsReference[mapMatrix[currentRow][currentCol]];
                        if(currentObj){
                            currentObj.x = currentObj.x || 0;
                            currentObj.y = currentObj.y || 0;
                            img = cg.loader.loadedImgs[currentObj.src];
                            //绘制特定坐标的图像
                            cg.context.drawImage(img, currentObj.x, currentObj.y, cellSize[0], cellSize[1], j, i, cellSize[0], cellSize[1]); 
                        }
                    }
                }
                //更新该layer上所有sprite
                this.spriteList.draw();
    
            }
        }
Copy after login

After that, we can easily create different layers and add them to the map:

/*    背景矩阵    */
var bgMatrix = [
                    [1,1,1],
                    [1,1,1],
                    [1,1,1]
                ];

this.map = new cnGame.Map({width:3000,height:3000});
var newLayer=new cnGame.Layer("bg",bgMatrix, { cellSize: [1000, 1000], width: this.map.width, height: this.map.height });
newLayer.imgsReference={ "1": { src: srcObj.bg }};
this.map.addLayer(newLayer);
Copy after login

 

2. About the mobile scene. #  In the last HTML5 "Game Super Mario Game Demo", we achieved the effect of player fixation and scene movement by converting the movement of the game player into the movement of the game scene. However, this implementation method has A bigger problem, because it interferes with the changes in the xy values ​​of the map and the player, so it will cause a lot of inconvenience.

A better implementation is to keep the xy values ​​of the player and the map unchanged, and only change when drawing them. The coordinates of the origin.

The new method of the view object: applyInView:

The function of the applyIn

View method

is to not change the actual coordinates of the map and the player. Next, the view is fixed when drawing, and other game elements move relative to the view to achieve the effect of moving the background. For example, we need to make the player fixed relative to the midpoint of the view, and all other game elements on the map move relative to the view. Just need to initialize:

When drawing:

 this.view.applyInView(function(){
            map.draw();        
        });
Copy after login

In this way, all elements in the map will move relative to the view.

The implementation principle of applyInView is also very simple. It just keeps making the origin of drawing and the coordinates of the view equal and opposite:

/**
            *使坐标相对于view
            **/
            applyInView:function(func){    
                cg.context.save();
                cg.context.translate(-this.x, -this.y);
                func();
                cg.context.restore();
            },
Copy after login

In this way, no matter how the coordinates of the view change, the view will visually change Always fixed to

canvas

, the coordinates of other elements are always visually relative to the view.

The above is the detailed content of Detailed explanation of horizontal shooting game based on HTML5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template