<!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>
解决方案 »
- onClick 取值的问题
- Extjs 4.0 如何在下拉框中显示无限极分类级数据?
- c#后台生成数组数据传递给jquery前端问题
- 高分求助
- js的字符串如何转化为对象,在学json的时候遇到点问题
- onmouseover直接附加不起作用?
- 请教 fason,给onclick事件赋处理函数的问题
- javascript函数可以代jsp数组作为参数吗?
- 能否重载web页面的一些方法,如document.onclick()
- 一个小儿科的问题——强制转换成字符串的函数是?
- 同一个页面多个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)后面少个{