js贪食蛇
<!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中的DOM事件与对象
- Comparison of SQL Server Compact, SQLite, SQL Server Express and LocalDB
- AIDMA VS AISAS vs ISMAS 营销法则
- spi接口的ds1302时钟芯片控制在lcd1602上显示
- Java for LeetCode 034 Search for a Range
- Js的 ";继承";
- python 基础知识(一)
- javascript之基本包装类型(Boolean,Number,String)基础篇
- 基于MFC简单图片裁剪工具
- 用VS2005编译生成Lua库文件和解释器
- Linux上leveldb的安装和使用
- 说一说js中__proto__和prototype以及原型继承的那些事
- CentOS7安装MySQL并配置账户等
- DHCP的IP地址租约、释放
- 全能系统监控工具dstat
- Javaweb学习笔记——(十八)——————事务、DBCP、C3P0、装饰者模式
- PAT 乙级 1092 最好吃的月饼 (20 分)
- mysql的聚簇索引与非聚簇索引的简短总结
- 一个简单的Oracle和 SQLSERVER 重建所有表索引的办法
- Python Scrapy 爬取煎蛋网妹子图实例(一)
热门文章
- SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树
- 洛谷 P4001 [ICPC-Beijing 2006]狼抓兔子
- ionic4 添加自定义字体图标
- <;转>; django模板语言filter方法
- POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
- cmd命令行编译c/c++程序
- select 点击的时候获取 value值
- oracle 笔记---(四)__数据字典
- unity文件 PlayerPrefs.SetInt 保存 And PlayerPrefs.GetInt读取
- 实现类似tail -f file功能