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 cnGameJSEffect preview:
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(); } }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);
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: this.view=new cnGame.View({map:this.map,x:0,y:0,width:cnGame.width,height:cnGame.height});
this.view.centerElem(this.player,true);
When drawing:
this.view.applyInView(function(){ map.draw(); });
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(); },
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!

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
