用html5+js實現掌機游戲賽車demo


最近無聊,用html5+js做了一個以前玩過的掌機賽車游戲,順便學習一下畫布的api以及鞏固一下js基礎。

游戲界面,沒做什么美化。

游戲規則:游戲界面分為三列,黑色方塊隨機落下,紅色方塊可以在三列自由移動(用方向鍵,長按下方向鍵黑色方塊加速下滑)。紅色方塊碰到黑色方塊即為輸。
得分:每正常通過一次黑色方塊加12分,加速通過加30分。

下面直接上代碼:

html:

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <style>
 8  body{
 9  text-align: center;
10     }
11  #mycar{
12  border: 1px solid black;
13     }
14 </style>
15 <body>
16     <canvas id="mycar" width="300px" height="500px"></canvas>
17     <div id="scored">得分:0</div>
18     <script src="js/mycar.js"></script>
19 </body>
20 </html>

 js:

 

 1 /**  2  * Created by zqc on 2014/12/3.  3  */
 4 
 5 function createCar(speed,cxt,dom) {  6     var o = new Object();  7     o.speed = speed; // 落下速度
 8     o.cxt = cxt; // 畫布
 9     o.cell = 100; // 賽車寬度
 10     o.curdir = {'x':100,'y':400}; // 紅色賽車初始位置
 11     o.hinder = [[],[],[]]; // 障礙物位置
 12     o.scroll = 0; // 下滑距離
 13     o.scored = 0; // 分數
 14     o.init = function () {  15         o.cxt.fillStyle = 'red';  16  o.cxt.fillRect(o.curdir.x, o.curdir.y, o.cell, o.cell);  17         document.onkeydown = function (e) { // 按鍵事件
 18             if(e.keyCode === 37 && o.curdir.x > 0){  19                 o.moveCar('left');  20  }  21             else if(e.keyCode === 39 && o.curdir.x < 200){  22                 o.moveCar('right');  23  }  24             else if(e.keyCode === 40) {// 長按加速
 25                 o.speed = speed / 3;  26  }  27  };  28         document.onkeyup = function () {  29             o.speed = speed;  30  };  31  o.setHinder();  32  o.startCar();  33  };  34     o.setHinder = function () { // 生成障礙物
 35         var rand1 = Math.ceil(Math.random() * 1000) % 2,  36             rand2 = Math.ceil(Math.random() * 1000) % 2,  37             rand3 = Math.ceil(Math.random() * 1000) % 2;  38         o.hinder[0].push({'x':0,'y':0,'hinder':rand1}); // hinder表示是否有障礙物
 39         o.hinder[1].push({'x':100,'y':0,'hinder':rand2});  40         o.hinder[2].push({'x':200,'y':0,'hinder':rand1 + rand2 == 2?0:rand3});  41         for(var i = 0; i < o.hinder.length; i ++){  42             var last =o.hinder[i][o.hinder[i].length - 1];  43             if(last.hinder === 1) { // 有障礙物
 44                 o.cxt.fillStyle = 'black';  45  o.cxt.fillRect(last.x,last.y, o.cell, o.cell);  46  }  47  }  48  };  49     o.downHinder = function () { // 控制障礙物下降
 50         var i = 0,  51             j = 0,  52             cur = null,  53             old = null;  54         for(; i < o.hinder[0].length; i ++) {  55             for(j = 0; j < 3; j ++) {  56                 cur = o.hinder[j][i];  57                 if (cur.hinder === 1) {  58                     old = o.hinder[j][i];  59                     o.cxt.clearRect(old.x,old.y, o.cell, o.cell); // 清除上一幀
 60                     o.hinder[j][i].y = o.hinder[j][i].y + 5;  61                     cur = o.hinder[j][i];  62                     o.cxt.fillStyle = 'black';  63  o.cxt.fillRect(cur.x,cur.y, o.cell, o.cell);  64  }  65                 else
 66                     o.hinder[j][i].y = o.hinder[j][i].y + 5;  67  }  68  }  69  };  70     o.moveCar = function (dir) {  71         o.cxt.fillStyle = 'red';  72  o.cxt.clearRect(o.curdir.x, o.curdir.y, o.cell, o.cell);  73         o.curdir.x = (dir === 'left'?o.curdir.x - o.cell:o.curdir.x + o.cell);  74  o.cxt.fillRect(o.curdir.x,o.curdir.y, o.cell, o.cell);  75  };  76     o.clearHander = function () {  77         for(var i = 0; i < o.hinder.length; i ++) {  78             if (o.hinder[i][0].y >= 500) { // 超過畫布最低位置,清除障礙物
 79                 o.counterScorde(); // 計分
 80                 var over = o.hinder[i].shift();  81                 if(over.hinder === 1)  82  o.cxt.clearRect(over.x,over.y, o.cell, o.cell);  83  }  84  }  85  };  86     o.counterScorde = function () {  87         o.scored  = o.scored  +  Math.ceil(100/o.speed);
 88         dom.innerHTML = '得分:' + o.scored;  89  };  90     o.startCar = function () {  91         setTimeout(function () {  92             o.downHinder(); // 落下障礙物
 93             o.clearHander(); // 清除已通過的障礙物
 94             if(o.hinder[o.curdir.x/100][0].hinder === 1 && o.hinder[o.curdir.x/100][0].y + 100 >= o.curdir.y){  95                 alert('你掛了');  96                 return;  97  }  98             o.scroll = o.scroll + 5; // 單位下滑速度
 99             if(o.scroll % 300 === 0) 100                 o.setHinder(); // 設置障礙物
101  o.startCar(); 102  }, o.speed); 103  }; 104     return o; 105 } 106 
107 var c = document.getElementById('mycar'); 108 var scored = document.getElementById('scored'); 109 var cxt = c.getContext('2d'); 110 var car = createCar(30,cxt,scored); 111 car.init();

 

 

 

詳情見github: 掌機游戲賽車demo

 

算法寫的有點不好,有大神路過望給一寫指導。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM