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

Detailed explanation of horizontal shooting game based on HTML5

Mar 24, 2017 pm 03:33 PM

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

/**

        *层对象

        **/                              

        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:

1

2

3

4

5

6

7

8

9

10

11

/*    背景矩阵    */

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:

1

2

<a href="https://www.php.cn/php/php-view.html" target="_blank">   this.view=new cnGame.View({map:this.map,x:0,y:0,width:cnGame.width,height:cnGame.height});

        this.view.centerElem(this.player,true);</a>

When drawing:

1

2

3

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:

1

2

3

4

5

6

7

8

9

/**

            *使坐标相对于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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

Nested Table in HTML

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Table Border in HTML

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

HTML margin-left

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Moving Text in HTML

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

HTML Ordered List

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

HTML onclick Button

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

HTML Input Placeholder

See all articles