<!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>

最新文章

  1. js中的DOM事件与对象
  2. Comparison of SQL Server Compact, SQLite, SQL Server Express and LocalDB
  3. AIDMA VS AISAS vs ISMAS 营销法则
  4. spi接口的ds1302时钟芯片控制在lcd1602上显示
  5. Java for LeetCode 034 Search for a Range
  6. Js的 &quot;继承&quot;
  7. python 基础知识(一)
  8. javascript之基本包装类型(Boolean,Number,String)基础篇
  9. 基于MFC简单图片裁剪工具
  10. 用VS2005编译生成Lua库文件和解释器
  11. Linux上leveldb的安装和使用
  12. 说一说js中__proto__和prototype以及原型继承的那些事
  13. CentOS7安装MySQL并配置账户等
  14. DHCP的IP地址租约、释放
  15. 全能系统监控工具dstat
  16. Javaweb学习笔记——(十八)——————事务、DBCP、C3P0、装饰者模式
  17. PAT 乙级 1092 最好吃的月饼 (20 分)
  18. mysql的聚簇索引与非聚簇索引的简短总结
  19. 一个简单的Oracle和 SQLSERVER 重建所有表索引的办法
  20. Python Scrapy 爬取煎蛋网妹子图实例(一)

热门文章

  1. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树
  2. 洛谷 P4001 [ICPC-Beijing 2006]狼抓兔子
  3. ionic4 添加自定义字体图标
  4. &lt;转&gt; django模板语言filter方法
  5. POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
  6. cmd命令行编译c/c++程序
  7. select 点击的时候获取 value值
  8. oracle 笔记---(四)__数据字典
  9. unity文件 PlayerPrefs.SetInt 保存 And PlayerPrefs.GetInt读取
  10. 实现类似tail -f file功能