<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇</title>
</head>
<body>
<canvas id="can" width="400" height="400" style="background: black" ></canvas>
<script>
var sn =[42,41], dz=43, fx=1,n, ctx=document.getElementById("can").getContext("2d");
function draw(t,c){
ctx.fillStyle = c;
ctx.fillRect(t % 20*20+1, ~~(t/20)*20+1, 18, 18);
}
document.onkeydown= function(e){
fx=sn[1] - sn[0] == (n=[-1,-20,1,20][(e||event).keyCode -37] || fx) ? fx:n
};
!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("游戏结束,请开始下一局");
draw(n ,"Lime");
if(n ==dz){
while(sn.indexOf(dz=~~(Math.random()*400)) >= 0);
draw(dz ,"#A763E8");
}else
draw(sn.pop(),"black");
setTimeout(arguments.callee, 130);
}();
</script>
</body>
</html>
js 小游戏
最新推荐文章于 2023-11-06 15:43:44 发布