博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
贪吃蛇
阅读量:6956 次
发布时间:2019-06-27

本文共 894 字,大约阅读时间需要 2 分钟。

hot3.png

小疯今天写了个几行html的贪吃蛇小游戏,分享一下:

<!doctype html>  

<html>  
<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("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, 130);  
        }();  
    </script>  
</body>  
</html>

效果如下:

54f7f613da075589f58ecf4ad9b769aa665.jpg

 

转载于:https://my.oschina.net/yexiaofeng/blog/1829882

你可能感兴趣的文章
从Linux 2.6.8内核的一个TSO/NAT bug引出的网络问题排查观点(附一个skb的优化点)
查看>>
apache python 模块mod_wsgi的编译安装
查看>>
个人随想:对于一个.Neter来说,如果一直想走技术路线,该怎么走下去
查看>>
手动安装双机软件VCS5.1
查看>>
APK软件反编译 去广告
查看>>
负载均衡 haproxy 安装
查看>>
通过命令行方式批量设置保留IP地址
查看>>
Sql server Output 用法
查看>>
一步一步定制Linux系统
查看>>
域管理员新手必看:域环境下DNS客户端排错三板斧
查看>>
ORA-21561: OID generation failed
查看>>
递归,汉诺塔游戏
查看>>
Lucene:基于Java的全文检索引擎简介
查看>>
Linux挂载img镜像文件
查看>>
scp设置不要求验证拷贝
查看>>
我的友情链接
查看>>
ERROR 145 (HY000) at line 34: Table is marked as crashed and should be repaired
查看>>
java 在web项目启动时,执行某个方法
查看>>
域服务管理规划
查看>>
我的友情链接
查看>>