我一直很想用js写一个简易的网页小游戏,但是作为一个只写过几份js代码,连入门都算不上的蒟蒻,要我自己写的话简直天方夜谭。而网上的代码很难找到适合像我这样的小白。所幸在github上找到了一位前辈做过的前端小demo,这些完整且简单的代码着实令我受益匪浅。
点击此处进入
在这些的demo中,我选择了贪吃蛇游戏,一来比较简单,二来不用其他图片素材,比较方便。
游戏点此
和我之前html、css、javascript炖在一起作大杂烩不同,这份代码html、css、javascript分别放在三个文档中,清晰明了。
HTML
我们先从其骨架——html文档着手。html文档中的代码并不是很多,主要做了两件事:
- 在
<head>中调用js和css的文档
- 用
<div>标签将网页分割成几个独立区域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>贪吃蛇</title> <script src="snake.js"></script> <link rel="stylesheet" href="snake.css"/> </head> <body> <div id="map"></div> <div id="Button"> <button type="button" onclick="start()">开始游戏</button> <button type="button" onclick="stop()">结束游戏</button> </div> </body> </html>
|
CSS
分好了区域,那么就开始用css修饰网页中需要的元素。css代码没什么需要解释的,详情看代码及注释,毕竟我也是新手。
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
| @charset "utf-8"; *{ margin:0; padding:0;} html,body{ width:100%; height:100%;} body{ position:relative;}
#map{ position:absolute; left:0;right:0; top:0; bottom:50px; margin:auto; border:2px solid #000; font-size: 0; }
#Button{ position:absolute; left:450px;right:0; bottom:10px; margin:auto; font-size: 25px; }
span{ display: inline-block; box-sizing: border-box; }
span.snake{background: blue;} span.food{background: red;}
|
JAVASCRIPT
js的代码应该算是该游戏的重中之重了。
代码本身并不是很难,只是新的语言还不知道怎么用。
代码流程如下:
- 先定义所需要的变量。
- 游戏开始前初始化地图、蛇和食物。
- 开始游戏后按周期移动蛇,并判断下一步是否为自己的身体或者食物。
- 吃到食物,随机显示下一个食物。
- 吃到自己的身体则GameOver。
- 当遇到按键事件,根据所按键改变方向。
具体还是看代码吧……
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
| var map={ width:900, height:500 }; var box={ width:25, height:25 };
var boxNums={ wNums:map.width/box.width, hNums:map.height/box.height, nums:0 }; boxNums.nums=boxNums.wNums*boxNums.hNums;
var snake=[];
var other=[];
var period=200;
var dir=2;
window.onload=function(){ mapInit(); createFood(); }
function start(){ setInterval(snakeMove,period); document.onkeyup=function(e){ for(var i=0;i<4;i++){ if(e.keyCode==i+37&&dir!=(i+2)%4){ dir=i;break; } } } } function stop(){ window.location.href=window.location.href; }
function mapInit(){ var map_target=document.getElementById("map"); map_target.style.width=map.width+"px"; map_target.style.height=map.height+"px"; var newSpan=null; for(var i=1;i<=boxNums.nums;i++){ newSpan=document.createElement("span"); newSpan.style.width=box.width+"px"; newSpan.style.height=box.height+"px"; newSpan.id=i; map_target.appendChild(newSpan); if(i<=5){ newSpan.className="snake"; snake.push(newSpan); } else { other.push(newSpan); } } }
function createFood(){ var x=Math.floor(Math.random()*other.length); other[x].className="food"; }
function snakeMove(){ var headId=parseInt(snake[snake.length-1].id); switch(dir){ case 0: headId--; if(headId%boxNums.wNums==0)headId+=boxNums.wNums; break; case 1: headId-=boxNums.wNums; if(headId<=0)headId+=boxNums.nums; break; case 2: headId++; if(headId%boxNums.wNums==1)headId-=boxNums.wNums; break; case 3: headId+=boxNums.wNums; if(headId>=boxNums.nums)headId-=boxNums.nums; break; default:break; } var newHead=document.getElementById(headId); for(var i=1;i<snake.length;i++) if(headId==snake[i].id){ alert("Game Over!"); window.location.href=window.location.href; } var index; for(var i=1;i<other.length;i++) if(other[i].id==headId){ index=i; break; } other.splice(index,1); snake.push(newHead); if(newHead.className=="food"){ createFood(); } else { snake[0].className=""; other.push(snake.shift()); } newHead.className="snake"; }
|