<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>贪吃蛇</title>
<style>
body
{ display: flex; justify-content: center; align-items: center;}
</style>
</head>
<body>
<canvas>id="can"width="400"height="400"style="background-color: red">不支持canver</canvas>
<script type="text/javascript">
var snake=[200,199],direction = 1,food=201,n;
var box=document.getElementById('can').getContext('2d');function draw(seat,color){
box.fillStyle=color;
box.fillRect(seat%20*20+1,~~(seat/20)*20+1,18,18);}for(i=0;i<=399;i++)
{ draw(i,"black");}
document.onkeydown = function(evt){ direction = snake[1]-snake[0]==(n=[-1,-20,1,20][(evt||event).keyCode-37]||direction)?direction :n; };
!function(){ snake.unshift(n=snake[0]+direction); if(snake.indexOf(n,1)>0||n<0||n>399||direction==1&& n%20==0||direction==-1&&n%20 ==19)
{return alert("GAME OVER");}
draw(n,"lime");
if(n ==food) while(snake.indexOf(food=~~(Math.random()*400))>0); draw(food,"yellow");}
else { draw(snake.pop(),"black");}setTimeout(arguments.callee,150); }();
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>贪吃蛇</title>
<style>
body
{ display: flex; justify-content: center; align-items: center;}
</style>
</head>
<body>
<canvas>id="can"width="400"height="400"style="background-color: red">不支持canver</canvas>
<script type="text/javascript">
var snake=[200,199],direction = 1,food=201,n;
var box=document.getElementById('can').getContext('2d');function draw(seat,color){
box.fillStyle=color;
box.fillRect(seat%20*20+1,~~(seat/20)*20+1,18,18);}for(i=0;i<=399;i++)
{ draw(i,"black");}
document.onkeydown = function(evt){ direction = snake[1]-snake[0]==(n=[-1,-20,1,20][(evt||event).keyCode-37]||direction)?direction :n; };
!function(){ snake.unshift(n=snake[0]+direction); if(snake.indexOf(n,1)>0||n<0||n>399||direction==1&& n%20==0||direction==-1&&n%20 ==19)
{return alert("GAME OVER");}
draw(n,"lime");
if(n ==food) while(snake.indexOf(food=~~(Math.random()*400))>0); draw(food,"yellow");}
else { draw(snake.pop(),"black");}setTimeout(arguments.callee,150); }();
</script>
</body>
</html>
解决方案 »
- JS 在火狐浏览器上不显示
- 一个关于JS表单验证的问题
- 这段代码放到table里面就死...苦闷!!,分不够可以加!
- 怎样将百分数转换成小数
- 明天要发给项目开发小组的js规范的个人建议,希望经验丰富者能够帮提出改善建议,非常感谢
- Javascript 调 用 com 组 件 的 问 题
- [分享]风声Javascript菜单类——预览版
- 跳转菜单咋就跳不起来呢??大家来看看!!
- 大家帮助我看一下这个正则问题,在线等,急!
- 刚刚用Behavior做了个东西。学HTC的人可以去看看
- 同一个页面多个select 多个onChange 请问怎么写?
- html5+javascript做的一个登陆滑动验证,pc端能滑动,为啥手机端不行?求大神指教啊!
<head> <meta charset="utf-8">
<title>贪吃蛇</title>
<style>
{ display: flex; justify-content: center; align-items: center;}
</style>
</head> <body>
<canvas>id="can"width="400"height="400"style="background-color: red">不支持canver</canvas>
<script type="text/javascript">
var snake=[200,199],direction = 1,food=201,n;
var box=document.getElementById('can').getContext('2d');function draw(seat,color){
box.fillStyle=color;
box.fillRect(seat%20*20+1,~~(seat/20)*20+1,18,18);}for(i=0;i<=399;i++)
{ draw(i,"black");}
document.onkeydown = function(evt){ direction = snake[1]-snake[0]==(n=[-1,-20,1,20][(evt||event).keyCode-37]||direction)?direction :n; };
!function(){ snake.unshift(n=snake[0]+direction); if(snake.indexOf(n,1)>0||n<0||n>399||direction==1&& n%20==0||direction==-1&&n%20 ==19)
{return alert("GAME OVER");}
draw(n,"lime");
if(n ==food) while(snake.indexOf(food=~~(Math.random()*400))>0); draw(food,"yellow");}
else { draw(snake.pop(),"black");}setTimeout(arguments.callee,150); }();
</script>
</body> </html>
上面这个可以
https://bbs.csdn.net/topics/390988009你这个错误有
<canvas后面多个>
有两个分号写成中文的了";"
if(n ==food)后面少个{