canvas游戏篇 - 贪吃蛇

xiaoxiao2025-04-08  19

截图如下:


HTML 代码如下:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <p>博主地址: https://blog.csdn.net/hj7jay </p> <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>

canvas游戏篇 游戏名称 - 贪吃蛇 游戏控制方式: 键盘方向键


以上就是关于“ canvas游戏篇 - 贪吃蛇 ” 的全部内容。

转载请注明原文地址: https://www.6miu.com/read-5027787.html

最新回复(0)