20行JS代码实现贪吃蛇
2017-06-14 20:42
330 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>20行JS代码实现贪吃蛇</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,~~(t/20)*20,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("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,500); }(); </script> </body> </html>
相关文章推荐
- 【05】20行js代码实现的贪吃蛇小游戏
- 20行js代码实现的贪吃蛇小游戏
- 20行JS代码实现贪吃蛇
- 20行code实现贪吃蛇功能
- 20行JS代码实现粘贴板功能
- 20行JS代码贪吃蛇
- 用20行JS代码实现粘贴板功能
- 20行JS代码实现网页刮刮乐效果
- 如何用20行JS代码实现粘贴板功能
- 20行JS代码实现粘贴板复制功能
- javascript 实现的多浏览器支持的贪吃蛇webgame
- javascript 实现的多浏览器支持的贪吃蛇webgame
- 一小段JS代码实现静态页面点击数的自动更新(使用AJAX)
- 一小段JS代码实现静态页面点击数的自动更新(使用AJAX)
- javascript 贪吃蛇实现代码
- 一小段JS代码实现静态页面点击数的自动更新(使用AJAX)
- 在iframe中使用js代码实现自动设置栽入的页面的高度自动化
- 实现图片等比例缩放的js代码
- 用JS代码实现页内搜索