<meta charset="utf-8">
<style>
The body
{display: flex; justify-content: center; align-items: center; }
</style>
<body>
Var box=document. GetElementById () 'can'. GetContext (" 2 d "); The function the draw (seat, color) {
Box. FillStyle=color;
Box. FillRect (seat % 20 * 20 + 1, ~ ~ (20) seat/* 20 + 1,18,18); } the for (I=0; i<=399; I++)
{the draw (I, "black"); }
Document. The onkeydown=function (evt) {direction=snake [1] - snake [0]==(n=20,1,20] [- 1 - [(evt | | event). The keyCode to 37] | | direction)? Direction: n. };
! The function () {snake. Unshift (n=snake [0] + direction); If (snake. IndexOf (n, 1) & gt; 0 | | n<0 | | n> 399 | | direction==1 & amp; & N % 20==0 | | direction==1 & amp; & N % 20==19)
{return alert (" GAME OVER "); }
The draw (n, "lime");
If (n==food) while (snake. IndexOf (food=~ ~ (Math) random () * 400)) & gt; 0); The draw (food, "yellow"); }
The else {the draw (snake. Pop (), "black"); } setTimeout (the arguments. The callee, 150); } ();
</script>