求注释,越精细越好,(贪吃蛇游戏)

<script> 
var sn = [ 42, 41 ], dz = 43, fx = 1, n, ctx = document.getElementById("can").getContext("2d");
 //申请变量,建立数组sn,sn中包含42,41两个元素;ctx代表
 
function draw(t, c) { 
ctx.fillStyle = c;  //fillstyle表是填充颜色;
ctx.fillRect(t % 20 * 20 + 1, ~~(t / 20) * 20 + 1, 18, 18);  //fillRect  是绘制“被填充”的矩形,Rect是绘制矩形
}
//建立函数,函数作用:变量c代表颜色,
 
 
document.onkeydown = function(e) { 
fx = sn[1] - sn[0] == (n = [ -1, -20, 1, 20 ][(e || event).keyCode - 37] || fx) ? fx : n 
};   //javascript中数组的应用,   控制蛇的长度

!function() { 
sn.unshift(n = sn[0] + fx); 
if (sn.indexOf(n, 1) > 0 || n<0||n>399 || fx == 1 && n % 20 == 0 || fx == -1 && n % 20 == 19) 
return alert("GAME OVER");  //判断游戏结束
 
draw(n, "Lime"); 
if (n == dz) { 
while (sn.indexOf(dz = ~~(Math.random() * 400)) >= 0); 
draw(dz, "Yellow"); 
}
else 
draw(sn.pop(), "Black"); 
setTimeout(arguments.callee, 130); 
}(); 
 
</script> 

猜你喜欢

转载自www.cnblogs.com/xsdcq/p/10467683.html