首页 > Web开发 > 详细

js贪食蛇

时间:2017-02-11 00:54:33      阅读:238      评论:0      收藏:0      [点我收藏+]

技术分享

 

<!DOCTYPE html>
<head>
<title>canvas贪食蛇</title>
<style>
.canvas{
    background:black;
}
</style>
</head>
<body>
<canvas class="canvas" id="canvas" width=‘400‘ height=‘400‘></canvas>
<script>
var sn=[42,41], dz=43, fx=1, n, ctx=document.getElementById(‘canvas‘).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]-[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 ;
      draw(n,‘red‘);
      if(n==dz){
          while(sn.indexOf(dz=~~(Math.random()*400))>=0);
          draw(dz,‘red‘);
      }else
          draw(sn.pop(),‘Black‘);
      setTimeout(arguments.callee,130);
}();
</script>

</body>
</html>

js贪食蛇

原文:http://www.cnblogs.com/aliwa/p/6388382.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!