日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
基于HTML5的橫版射擊游戲發(fā)布附源碼

 功能說明:

成都創(chuàng)新互聯(lián)公司主營高密網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,高密h5微信小程序開發(fā)搭建,高密網(wǎng)站營銷推廣歡迎高密等地區(qū)企業(yè)咨詢

基于HTML5的橫版射擊游戲,參考自flash游戲《雙面特工》。左右方向鍵控制移動,下方向鍵蹲下,上方向鍵跳躍,空格鍵射擊。體驗前請先關(guān)閉輸入法。

該游戲基于自己開發(fā)的HTML5游戲框架cnGameJS。

效果預(yù)覽:

實現(xiàn)分析:

1.關(guān)于多層地圖。

在上一個HTML5游戲《坦克后援隊》中,所用的地圖只為簡單的單層地圖,意思是地圖中除了石頭就是空地,僅僅只有一層的地圖。但是這種單層地圖具有比較大的局限性,如果需要實現(xiàn)場景類的游戲(例如超級瑪麗和上面的游戲),只有一層的地圖往往是不夠的,因為我們除了游戲主角所站的障礙物外,還有游戲背景等元素(例如后面的墻壁等),因此我們需要為地圖對象分層,從而達到多層展示的目的。

新增的layer對象:

每個layer對象維護該層的sprite,負責(zé)更新和繪制它們,并且可以獲取指定坐標(biāo)在該層的矩陣上的值。layer對象源碼如下:

 
 
 
 
  1. /** 
  2.         *層對象 
  3.         **/                                
  4.         var layer = function(id,mapMatrix, options) { 
  5.      
  6.             if (!(this instanceof arguments.callee)) { 
  7.                 return new arguments.callee(id,mapMatrix, options); 
  8.             } 
  9.             this.init(id,mapMatrix, options); 
  10.         } 
  11.         layer.prototype={ 
  12.              
  13.             /** 
  14.             *初始化 
  15.             **/ 
  16.             init: function(id,mapMatrix,options) { 
  17.                 /** 
  18.                 *默認對象 
  19.                 **/     
  20.                 var defaultObj = { 
  21.                     cellSize: [32, 32],   //方格寬,高 
  22.                     x: 0,                      //layer起始x 
  23.                     y: 0                  //layer起始y 
  24.      
  25.                 };     
  26.                 optionsoptions = options || {}; 
  27.                 options = cg.core.extend(defaultObj, options); 
  28.                 this.id=options.id; 
  29.                 this.mapMatrix = mapMatrix; 
  30.                 this.cellSize = options.cellSize; 
  31.                 this.x = options.x; 
  32.                 this.y = options.y; 
  33.                 this.row = mapMatrix.length; //有多少行 
  34.                 thisthis.width=this.cellSize[0]* mapMatrix[0].length; 
  35.                 thisthis.height=this.cellSize[1]* this.row; 
  36.                 this.spriteList=new cg.SpriteList();//該層上的sprite列表 
  37.                 this.imgsReference=options.imgsReference;//圖片引用字典:{"1":{src:"xxx.png",x:0,y:0},"2":{src:"xxx.png",x:1,y:1}} 
  38.                 this.zIindex=options.zIndex; 
  39.             }, 
  40.             /** 
  41.             *添加sprite 
  42.             **/             
  43.             addSprites:function(sprites){ 
  44.                 if (cg.core.isArray(sprites)) { 
  45.                     for (var i = 0, len = sprites.length; i < len; i++) { 
  46.                         arguments.callee.call(this, sprites[i]); 
  47.                     } 
  48.                 } 
  49.                 else{ 
  50.                     this.spriteList.add(sprites); 
  51.                     sprites.layer=this; 
  52.                 }                 
  53.                  
  54.             }, 
  55.             /** 
  56.             *獲取特定對象在layer中處于的方格的值 
  57.             **/ 
  58.             getPosValue: function(x, y) { 
  59.                 if (cg.core.isObject(x)) { 
  60.                     y = x.y; 
  61.                     xx = x.x; 
  62.                 } 
  63.                 var isUndefined = cg.core.isUndefined; 
  64.                 y = Math.floor(y / this.cellSize[1]); 
  65.                 x = Math.floor(x / this.cellSize[0]); 
  66.                 if (!isUndefined(this.mapMatrix[y]) && !isUndefined(this.mapMatrix[y][x])) { 
  67.                     return this.mapMatrix[y][x]; 
  68.                 } 
  69.                 return undefined; 
  70.             }, 
  71.             /** 
  72.             *獲取特定對象在layer中處于的方格索引 
  73.             **/ 
  74.             getCurrentIndex: function(x, y) { 
  75.                 if (cg.core.isObject(x)) { 
  76.                     y = x.y; 
  77.                     xx = x.x; 
  78.                 } 
  79.                 return [Math.floor(x / this.cellSize[0]), Math.floor(y / this.cellSize[1])]; 
  80.             }, 
  81.             /** 
  82.             *獲取特定對象是否剛好與格子重合 
  83.             **/ 
  84.             isMatchCell: function(x, y) { 
  85.                 if (cg.core.isObject(x)) { 
  86.                     y = x.y; 
  87.                     xx = x.x; 
  88.                 } 
  89.                 return (x % this.cellSize[0] == 0) && (y % this.cellSize[1] == 0); 
  90.             }, 
  91.             /** 
  92.             *設(shè)置layer對應(yīng)位置的值 
  93.             **/ 
  94.             setPosValue: function(x, y, value) { 
  95.                 this.mapMatrix[y][x] = value; 
  96.             }, 
  97.             /** 
  98.             *更新層上的sprite列表 
  99.             **/             
  100.             update:function(duration){ 
  101.                 this.spriteList.update(duration); 
  102.                  
  103.             }, 
  104.             /** 
  105.             *根據(jù)layer的矩陣繪制layer和該layer上的所有sprite 
  106.             **/ 
  107.             draw: function() { 
  108.                 var mapMatrix = this.mapMatrix; 
  109.                 var beginX = this.x; 
  110.                 var beginY = this.y; 
  111.                 var cellSize = this.cellSize; 
  112.                 var currentRow; 
  113.                 var currentCol 
  114.                 var currentObj; 
  115.                 var row = this.row; 
  116.                 var img; 
  117.                 var col; 
  118.                 for (var i = beginY, ylen = beginY + row * cellSize[1]; i < ylen; i += cellSize[1]) {    //根據(jù)地圖矩陣,繪制每個方格 
  119.                     currentRow = (i - beginY) / cellSize[1]; 
  120.                     col=mapMatrix[currentRow].length; 
  121.                     for (var j = beginX, xlen = beginX + col * cellSize[0]; j < xlen; j += cellSize[0]) { 
  122.                         currentCol = (j - beginX) / cellSize[0]; 
  123.                         currentObj = this.imgsReference[mapMatrix[currentRow][currentCol]]; 
  124.                         if(currentObj){ 
  125.                             currentObjcurrentObj.x = currentObj.x || 0; 
  126.                             currentObjcurrentObj.y = currentObj.y || 0; 
  127.                             img = cg.loader.loadedImgs[currentObj.src]; 
  128.                             //繪制特定坐標(biāo)的圖像 
  129.                             cg.context.drawImage(img, currentObj.x, currentObj.y, cellSize[0], cellSize[1], j, i, cellSize[0], cellSize[1]);  
  130.                         } 
  131.                     } 
  132.                 } 
  133.                 //更新該layer上所有sprite 
  134.                 this.spriteList.draw(); 
  135.      
  136.             } 
  137.         } 

之后我們可以很方便地創(chuàng)建不同的層,并添加到地圖中:

 
 
 
 
  1. /*    背景矩陣    */ 
  2. var bgMatrix = [ 
  3.                     [1,1,1], 
  4.                     [1,1,1], 
  5.                     [1,1,1] 
  6.                 ]; 
  7.  
  8. this.map = new cnGame.Map({width:3000,height:3000}); 
  9. var newnewLayer=new cnGame.Layer("bg",bgMatrix, { cellSize: [1000, 1000], width: this.map.width, height: this.map.height }); 
  10. newLayer.imgsReference={ "1": { src: srcObj.bg }}; 
  11. this.map.addLayer(newLayer); 

2.關(guān)于移動場景。

在上一次的HTML5《游戲超級瑪麗游戲demo》中,我們通過使游戲玩家的移動轉(zhuǎn)換為游戲場景的移動來實現(xiàn)玩家固定,場景移動的效果,但是這種實現(xiàn)方法有比較大的問題,因為它干涉了地圖和玩家的xy值的變化,因此會帶來很多不便。更好的實現(xiàn)方法是,保持玩家和地圖的xy值不變,只改變繪制它們時原點的坐標(biāo)。

view對象新增的方法:applyInView:

applyInView方法的作用是在不改變地圖和玩家實際坐標(biāo)的前提下,在繪制時使view固定,其他游戲元素相對于view移動,實現(xiàn)移動背景的效果。例如,我們需要使玩家相對于view中點固定,該map上的其他所有游戲元素相對于view移動,我們只需要在初始化時:

 
 
 
 
  1. this.view=new cnGame.View({map:this.map,x:0,y:0,width:cnGame.width,height:cnGame.height}); 
  2.         this.view.centerElem(this.player,true); 

在繪制時:

 
 
 
 
  1. this.view.applyInView(function(){ 
  2.             map.draw();         
  3.         }); 

這樣map內(nèi)所有元素都會相對于view而移動。

而applyInView的實現(xiàn)原理也非常簡單,它只是不斷使繪制的原點和view的坐標(biāo)等長且相反:

 
 
 
 
  1. /** 
  2.             *使坐標(biāo)相對于view 
  3.             **/ 
  4.             applyInView:function(func){     
  5.                 cg.context.save(); 
  6.                 cg.context.translate(-this.x, -this.y); 
  7.                 func(); 
  8.                 cg.context.restore(); 
  9.             }, 

這樣無論view的坐標(biāo)如何變化,view在視覺上始終固定在canvas,其他元素的坐標(biāo)在視覺上始終相對于view。

該游戲所有源碼下載地址:點擊下載

原文:http://www.cnblogs.com/Cson/archive/2012/03/15/2398129.html


標(biāo)題名稱:基于HTML5的橫版射擊游戲發(fā)布附源碼
文章URL:http://www.dlmjj.cn/article/cocpgpi.html