贪吃蛇html
1. 基于html5的贪吃蛇的设计与实现
第一个问题,是缺少引用文件,检查一下是否本地有个js文件挪了位置
第二个问题,是语法格式问题,如果解决了第一个问题,第二个问题很可能也解决了。
2. 贪吃蛇碰到自己就会死的html5代码
snake.js
代码如下:::
复制代码
代码如下:
var canvas;
var ctx;
var timer;
//measures
var x_cnt = 15;
var y_cnt = 15;
var unit = 48;
var box_x = 0;
var box_y = 0;
var box_width = 15 * unit;
var
box_height = 15 * unit;
var bound_left = box_x;
var bound_right = box_x
+ box_width;
var bound_up = box_y;
var bound_down = box_y + box_height;
//images
var image_sprite;
//objects
var snake;
var food;
var food_x;
var food_y;
//functions
function Role(sx, sy, sw,
sh, direction, status, speed, image, flag)
{
this.x = sx;
this.y =
sy;
this.w = sw;
this.h = sh;
this.direction = direction;
this.status = status;
this.speed = speed;
this.image = image;
this.flag = flag;
}
function transfer(keyCode)
{
switch
(keyCode)
{
case 37:
return 1;
case 38:
return 3;
case
39:
return 2;
case 40:
return 0;
}
}
function addFood()
{
//food_x=box_x+Math.floor(Math.random()*(box_width-unit));
//food_y=box_y+Math.floor(Math.random()*(box_height-unit));
food_x =
unit * Math.floor(Math.random() * x_cnt);
food_y = unit *
Math.floor(Math.random() * y_cnt);
food = new Role(food_x, food_y, unit,
unit, 0, 0, 0, image_sprite, true);
}
function play(event)
{
var
keyCode;
if (event == null)
{
keyCode = window.event.keyCode;
window.event.preventDefault();
}
else
{
keyCode =
event.keyCode;
event.preventDefault();
}
var cur_direction =
transfer(keyCode);
snake[0].direction = cur_direction;
}
function
update()
{
//add a new part to the snake before move the snake
if
(snake[0].x == food.x && snake[0].y == food.y)
{
var length =
snake.length;
var tail_x = snake[length - 1].x;
var tail_y =
snake[length - 1].y;
var tail = new Role(tail_x, tail_y, unit, unit,
snake[length - 1].direction, 0, 0, image_sprite, true);
snake.push(tail);
addFood();
}
//modify attributes
//move the head
switch
(snake[0].direction)
{
case 0: //down
snake[0].y += unit;
if
(snake[0].y > bound_down - unit)
snake[0].y = bound_down - unit;
break;
case 1: //left
snake[0].x -= unit;
if (snake[0].x <
bound_left)
snake[0].x = bound_left;
break;
case 2: //right
snake[0].x += unit;
if (snake[0].x > bound_right - unit)
snake[0].x = bound_right - unit;
break;
case 3: //up
snake[0].y
-= unit;
if (snake[0].y < bound_up)
snake[0].y = bound_up;
break;
}
//move other part of the snake
for (var i = snake.length - 1; i
>= 0; i--)
{
if (i > 0)
//snake[i].direction=snake[i-1].direction;
{
snake[i].x = snake[i -
1].x;
snake[i].y = snake[i - 1].y;
}
}
}
function
drawScene()
{
ctx.clearRect(box_x, box_y, box_width, box_height);
ctx.strokeStyle = "rgb(0,0,0";
ctx.strokeRect(box_x, box_y, box_width,
box_height);
//detection collisions
//draw images
for (var i = 0; i
< snake.length; i++)
{
ctx.drawImage(image_sprite, snake[i].x,
snake[i].y);
}
ctx.drawImage(image_sprite, food.x, food.y);
}
function init()
{
canvas = document.getElementById("scene");
ctx
= canvas.getContext('2d');
//images
image_sprite = new Image();
image_sprite.src = "images/sprite.png";
image_sprite.onLoad = function
()
{}
//ojects
snake = new Array();
var head = new Role(0 *
unit, 0 * unit, unit, unit, 5, 0, 1, image_sprite, true);
snake.push(head);
window.addEventListener('keydown', play, false);
addFood();
setInterval(update, 300); //note
setInterval(drawScene, 30);
}
望点赞!!!!
3. js贪吃蛇如何增加蛇和食物数量;设置食物的毒性;设置游戏开始、结束、暂停、继续等开关按钮,
布局就不说了哈,只说功能游戏开始,直接增加个点击事件就行,点击之后,所有代码再开始执行
$("#start").on("click",function(){
// 你的所有代码
})
结束也同理,点击之后直接把所有布局都删了就行
$("#end").on("click",function(){
document.body.innerHTML=""// 清除你的所有布局,可以你布局一个结束页面
})
游戏暂停也简单,就点击之后,停掉所有运动,也就是清除定时器
$("#pause").on("click",function(){
clearInterval(timer)// 清除你的所有定时器
})
继续也同理,点击之后直接定时器重启就行
$("#continue").on("click",function(){
setInterval()//把你的定时器来一遍
})
先说蛇的长度增加,是在蛇吃到食物之后
document.body.appendChild(createElement("div"));//增加一个单位的长度
食物同理,把最后的div换成你的食物 比如 span.
有毒的食物就简单啦,可以把某个标签 比如li设成食物的样式,但是吃到之后,事件函数为
document.querySelectorAll("div")[document.querySelectorAll("div").length-1].remove();//删掉一个单位的蛇的长度
手敲觉得有用点个赞哟!
4. 贪吃蛇游戏的源代码
||||?
贪吃蛇源码:
<!doctype html>
<html>
<body style='overflow:hidden'>
<canvas id="can" width="400" height="400" style="background:Black;display: block;margin:20px auto;"></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>
5. 贪吃蛇怎么用html5增加暂停键
以前也很少关注html5,感觉选择html已经慢慢成为趋势,想了解下。就找了个游戏学习了,写完这个游戏感觉html5和js结合很紧密,如果js不是特别好。估计需要先补习下js,这个只是个人的建议,不一定准确。还有一个就是,思维和逻辑要特别清楚,不然写游戏可能很痛苦。
贪吃蛇,最主要的功能点: 1,蛇的移动 2,改变蛇的方向 3,放置食物 4,增加舍身 5,怎么挂的。
第一次写游戏,第一次写html5
感觉还是很吃力的。写完了,给大家分享下。互相交流.............不懂的或者有建议的,可以留言给我。。。代码很短,就60行。
不过这个是个半成品,等写完成了。再更新下
复制代码
代码如下:
<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="400" height="400"
style="border:1px solid #c3c3c3;"></canvas>
<script
type="text/javascript">
var c=document.getElementById("myCanvas");
var time = 160 ; //蛇的速度
var cxt=c.getContext("2d");
var x = y = 8;
var a = 0; //食物坐标
var t = 20; //舍身长
var map = []; //记录蛇运行路径
var
size = 8; //蛇身单元大小
var direction = 2; // 1 向上 2 向右 0 左 3下
interval =
window.setInterval(set_game_speed, time); // 移动蛇
function set_game_speed(){
// 移动蛇
switch(direction){
case 1:y = y-size;break;
case 2:x =
x+size;break;
case 0:x = x-size;break;
case 3:y = y+size;break;
}
if(x>400 || y>400 || x<0 || y<0){
alert("你挂了,继续努力吧!失败原因:碰壁了.....");window.location.reload();
}
for(var
i=0;i<map.length;i++){
if( parseInt(map[i].x)==x &&
parseInt(map[i].y)==y){
alert("你挂了,继续努力吧!失败原因:撞到自己了.....");window.location.reload();
}
}
if (map.length>t) { //保持舍身长度
var cl = map.shift(); //删除数组第一项,并且返回原元素
cxt.clearRect(cl['x'], cl['y'], size, size);
};
map.push({'x':x,'y':y}); //将数据添加到原数组尾部
cxt.fillStyle =
"#006699";//内部填充颜色
cxt.strokeStyle = "#006699";//边框颜色
cxt.fillRect(x, y,
size, size);//绘制矩形
if((a*8)==x && (a*8)==y){ //吃食物
rand_frog();t++;
}
}
document.onkeydown = function(e) { //改变蛇方向
var code = e.keyCode - 37;
switch(code){
case 1 : direction =
1;break;//上
case 2 : direction = 2;break;//右
case 3 : direction =
3;break;//下
case 0 : direction = 0;break;//左
}
}
// 随机放置食物
function rand_frog(){
a = Math.ceil(Math.random()*50);
cxt.fillStyle
= "#000000";//内部填充颜色
cxt.strokeStyle = "#000000";//边框颜色
cxt.fillRect(a*8, a*8, 8, 8);//绘制矩形
}
// 随机放置食物
rand_frog();
</script>
</body>
</html>
请点赞!!!
6. 课程设计:使用JavaScript制作一个网页上的贪吃蛇游戏
<html>
<head>
<title>贪吃蛇 Snake v2.4</title>
<style>
body{
font-size:9pt;
}
table{
border-collapse: collapse;
border:solid #333 1px;
}
td{
height: 10px;
width: 10px;
font-size: 0px;
}
.filled{
background-color:blue;
}
</style>
</head>
<script>
function $(id){return document.getElementById(id);}
/**************************************************************
* javascript贪吃蛇 v2.4 <br />
* author: sunxing007 05/14/2009<br />
* 转载请注明来自http://blog.csdn.net/sunxing007 谢谢!<br />
* v2.4修正了蛇身颜色可以随着蛇前进而移动
**************************************************************/
//贪吃蛇类
var Snake = {
tbl: null,
/**
* body: 蛇身,数组放蛇的每一节,
* 数据结构{x:x0, y:y0, color:color0},
* x,y表示坐标,color表示颜色
**/
body: [],
//当前移动的方向,取值0,1,2,3, 分别表示向上,右,下,左, 按键盘方向键可以改变它
direction: 0,
//定时器
timer: null,
//速度
speed: 250,
//是否已经暂停
paused: true,
//行数
rowCount: 30,
//列数
colCount: 30,
//初始化
init: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
this.tbl = $("main");
var x = 0;
var y = 0;
var colorIndex = 0;
//产生初始移动方向
this.direction = Math.floor(Math.random()*4);
//构造table
for(var row=0;row<this.rowCount;row++){
var tr=this.tbl.insertRow(-1);
for(var col=0;col<this.colCount;col++) {
var td=tr.insertCell(-1);
}
}
//产生20个松散节点
for(var i=0; i<10; i++){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
//产生蛇头
while(true){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = "black";
this.body.push({x:x,y:y,color:'black'});
break;
}
}
this.paused = true;
//添加键盘事件
document.onkeydown= function(e){
if (!e)e=window.event;
switch(e.keyCode | e.which | e.charCode){
case 13: {
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
//如果没有暂停,则停止移动
Snake.pause();
Snake.paused = true;
}
break;
}
case 37:{//left
//阻止蛇倒退走
if(Snake.direction==1){
break;
}
Snake.direction = 3;
break;
}
case 38:{//up
//快捷键在这里起作用
if(event.ctrlKey){
Snake.speedUp(-20);
break;
}
if(Snake.direction==2){//阻止蛇倒退走
break;
}
Snake.direction = 0;
break;
}
case 39:{//right
if(Snake.direction==3){//阻止蛇倒退走
break;
}
Snake.direction = 1;
break;
}
case 40:{//down
if(event.ctrlKey){
Snake.speedUp(20);
break;
}
if(Snake.direction==0){//阻止蛇倒退走
break;
}
Snake.direction = 2;
break;
}
}
}
},
//移动
move: function(){
this.timer = setInterval(function(){
Snake.erase();
Snake.moveOneStep();
Snake.paint();
}, this.speed);
},
//移动一节身体
moveOneStep: function(){
if(this.checkNextStep()==-1){
clearInterval(this.timer);
alert("Game over!\nPress Restart to continue.");
return;
}
if(this.checkNextStep()==1){
var _point = this.getNextPos();
var _x = _point.x;
var _y = _point.y;
var _color = this.getColor(_x,_y);
this.body.unshift({x:_x,y:_y,color:_color});
//因为吃了一个食物,所以再产生一个食物
this.generateDood();
return;
}
//window.status = this.toString();
var point = this.getNextPos();
//保留第一节的颜色
var color = this.body[0].color;
//颜色向前移动
for(var i=0; i<this.body.length-1; i++){
this.body[i].color = this.body[i+1].color;
}
//蛇尾减一节, 蛇尾加一节,呈现蛇前进的效果
this.body.pop();
this.body.unshift({x:point.x,y:point.y,color:color});
//window.status = this.toString();
},
//探寻下一步将走到什么地方
pause: function(){
clearInterval(Snake.timer);
this.paint();
},
getNextPos: function(){
var x = this.body[0].x;
var y = this.body[0].y;
var color = this.body[0].color;
//向上
if(this.direction==0){
y--;
}
//向右
else if(this.direction==1){
x++;
}
//向下
else if(this.direction==2){
y++;
}
//向左
else{
x--;
}
//返回一个坐标
return {x:x,y:y};
},
//检查将要移动到的下一步是什么
checkNextStep: function(){
var point = this.getNextPos();
var x = point.x;
var y = point.y;
if(x<0||x>=this.colCount||y<0||y>=this.rowCount){
return -1;//触边界,游戏结束
}
for(var i=0; i<this.body.length; i++){
if(this.body[i].x==x&&this.body[i].y==y){
return -1;//碰到自己的身体,游戏结束
}
}
if(this.isCellFilled(x,y)){
return 1;//有东西
}
return 0;//空地
},
//擦除蛇身
erase: function(){
for(var i=0; i<this.body.length; i++){
this.eraseDot(this.body[i].x, this.body[i].y);
}
},
//绘制蛇身
paint: function(){
for(var i=0; i<this.body.length; i++){
this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color);
}
},
//擦除一节
eraseDot: function(x,y){
this.tbl.rows[y].cells[x].style.backgroundColor = "";
},
paintDot: function(x,y,color){
this.tbl.rows[y].cells[x].style.backgroundColor = color;
},
//得到一个坐标上的颜色
getColor: function(x,y){
return this.tbl.rows[y].cells[x].style.backgroundColor;
},
//用于调试
toString: function(){
var str = "";
for(var i=0; i<this.body.length; i++){
str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - ";
}
return str;
},
//检查一个坐标点有没有被填充
isCellFilled: function(x,y){
if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){
return false;
}
return true;
},
//重新开始
restart: function(){
if(this.timer){
clearInterval(this.timer);
}
for(var i=0; i<this.rowCount;i++){
this.tbl.deleteRow(0);
}
this.body = [];
this.init();
this.speed = 250;
},
//加速
speedUp: function(time){
if(!this.paused){
if(this.speed+time<10||this.speed+time>2000){
return;
}
this.speed +=time;
this.pause();
this.move();
}
},
//产生食物。
generateDood: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
var x = Math.floor(Math.random()*this.colCount);
var y = Math.floor(Math.random()*this.rowCount);
var colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
};
</script>
<body onload="Snake.init();">
/*************************************************************<br />
* javascript贪吃蛇 v2.4<br />
* author: sunxing007 05/14/2009<br />
* 转载请注明来自 <a href="http://blog.csdn.net/sunxing007">http://blog.csdn.net/sunxing007</a> 谢谢!<br />
**************************************************************/<br />
<table id="main" border="1" cellspacing="0" cellpadding="0"></table>
<input type="button" id="btn" value="开始/暂停" />点左边按钮或按Enter开始/暂停游戏<br />
<input type="button" id="reset" value="重新开始" /><br />
<input type="button" id="upSpeed" value="加速" />点左边按钮或按Ctrl + ↑加速<br />
<input type="button" id="downSpeed" value="减速" />点左边按钮或按Ctrl + ↓减速
<script>
$('btn').onclick = function(){
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
Snake.pause();
Snake.paused = true;
}
};
$("reset").onclick = function(){
Snake.restart();
this.blur();
};
$("upSpeed").onclick = function(){
Snake.speedUp(-20);
};
$("downSpeed").onclick = function(){
Snake.speedUp(20);
};
</script>
</body>
</html>
7. 如何用javascript写一个贪吃蛇
<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8'>
<title>贪吃蛇</title>
<script type="text/javascript">
var map; //地图
var snake; //蛇
var food; //食物
var timer; //定时器
var initSpeed=200; //初始定时器时间间隔(毫秒),间接代表蛇移动速度
var nowSpeed=initSpeed; //游戏进行时蛇移动速度
var grade=0; //积分
var flag=1; //(可间接看做)关卡
//地图类
function Map(){
this.width=800;
this.height=400;
this.position='absolute';
this.color='#EEEEEE';
this._map=null;
//生成地图
this.show=function(){
this._map=document.createElement('div');
this._map.style.width=this.width+'px';
this._map.style.height=this.height+'px';
this._map.style.position=this.position;
this._map.style.backgroundColor=this.color;
document.getElementsByTagName('body')[0].appendChild(this._map);
}
}
//食物类
function Food(){
this.width=20;
this.height=20;
this.position='absolute';
this.color='#00FF00';
this.x=0;
this.y=0;
this._food;
//生成食物
this.show=function(){
this._food=document.createElement('div');
this._food.style.width=this.width+'px';
this._food.style.height=this.height+'px';
this._food.style.position=this.position;
this._food.style.backgroundColor=this.color;
this.x=Math.floor(Math.random()*map.width/this.width);
this.y=Math.floor(Math.random()*map.height/this.width);
this._food.style.left=this.x*this.width;
this._food.style.top=this.y*this.height;
map._map.appendChild(this._food);
}
}
//蛇类
function Snake(){
this.width=20;
this.height=20;
this.position='absolute';
this.direct=null;//移动方向
//初始蛇身
this.body=new Array(
[3,2,'red',null],//蛇头
[2,2,'blue',null],
[1,2,'blue',null]
);
//生成蛇身
this.show=function(){
for(var i=0;i<this.body.length;i++){
if(this.body[i][3]==null){
this.body[i][3]=document.createElement('div');
this.body[i][3].style.width=this.width;
this.body[i][3].style.height=this.height;
this.body[i][3].style.position=this.position;
this.body[i][3].style.backgroundColor=this.body[i][2];
map._map.appendChild(this.body[i][3]);
}
this.body[i][3].style.left=this.body[i][0]*this.width+'px';
this.body[i][3].style.top=this.body[i][1]*this.height+'px';
}
}
//控制蛇移动
this.move=function(){
var length=this.body.length-1;
for(var i=length;i>0;i--){
this.body[i][0]=this.body[i-1][0];
this.body[i][1]=this.body[i-1][1];
}
switch(this.direct){
case 'right':
this.body[0][0]=this.body[0][0]+1;
break;
case 'left':
this.body[0][0]=this.body[0][0]-1;
break;
case 'up':
this.body[0][1]=this.body[0][1]-1;
break;
case 'down':
this.body[0][1]=this.body[0][1]+1;
break;
}
this.condition();
this.show();
}
//定时器,开始游戏时,调用
this.speed=function(){
timer=setInterval('snake.move()',initSpeed);
}
//条件处理
this.condition=function(){
//吃食物
if(this.body[0][0]==food.x&&this.body[0][1]==food.y){
grade++;
this.body[[this.body.length]]=[0,0,'blue',null];
map._map.removeChild(food._food)
food.show();
}
//判断是否撞墙
if(this.body[0][0]<0||this.body[0][0]>=map.width/this.width||this.body[0][1]<0||this.body[0][1]>=map.height/this.height){
alert('game over');
clearInterval(timer);
return ;
}
//判断是否撞到自身
for(var i=1;i<this.body.length;i++){
if(this.body[0][0]==this.body[i][0]&&this.body[0][1]==this.body[i][1]){
alert('game over');
clearInterval(timer);
return ;
}
}
//速度提升处理,积分每曾2分,速度提升一倍
if(grade/2==flag){
clearInterval(timer);
flag++;
nowSpeed=initSpeed/flag;
timer=setInterval('snake.move()',nowSpeed);
}
document.title='贪吃蛇 积分'+grade+' 速度等级'+initSpeed/nowSpeed;
}
}
document.onkeydown=function(event){
//按下任意键,开始游戏
if(snake.direct===null){
snake.direct='right';
snake.speed();
}
//控制方向,W S D A分别代表 上下右左
switch(window.event?window.event.keyCode:event.keyCode){//浏览器兼容处理
case 87 :
snake.direct=snake.body[0][0]==snake.body[1][0]?snake.direct:'up';//避免反向移动,触发死亡bug
break;
case 83 :
snake.direct=snake.body[0][0]==snake.body[1][0]?snake.direct:'down';
break;
case 68 :
snake.direct=snake.body[0][1]==snake.body[1][1]?snake.direct:'right';
break;
case 65 :
snake.direct=snake.body[0][1]==snake.body[1][1]?snake.direct:'left';
break;
}
}
//自动加载游戏
window.onload=function(){
map=new Map();
map.show();
food=new Food();
food.show();
snake=new Snake();
snake.show();
}
</script>
</head>
<body>
</body>
</html>
8. HTML5是有哪里能让他这么火,我搜了h5写的贪吃蛇,发现功能主要在js里面,但是用到的又是很多c
canvas是h5的新特性,但是基本上得用js才能玩出东西来。其实h5其他很多东西也基本靠js...
9. 使用html5开发贪吃蛇小游戏怎么设定撞到自己就会结束
这个当然要自己检测。用二维数组。用不同的数值来代表身体。头部。空白。食物。判断下一个位置。很简单
10. 20行js代码实现的贪吃蛇大战
稍等,我先留个坑,一会儿上电脑看看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><title>贪吃蛇重构</title><style>
body {display: flex;height: 100vh;margin: 0;padding: 0;justify-content: center;align-items: center;}</style>
</head>
<body>
<canvas id="can" width="400" height="400" style="background-color: black">对不起,您的浏览器不支持canvas</canvas>
<script>var snake = [41, 40], //snake队列表示蛇身,初始节点存在但不显示direction = 1, //1表示向右,-1表示向左,20表示向下,-20表示向上
food = 43, //食物的位置
n, //与下次移动的位置有关box = document.getElementById('can').getContext('2d');//从0到399表示box里[0~19]*[0~19]的所有节点,每20px一个节点function draw(seat, color) {box.fillStyle = color;box.fillRect(seat % 20 *20 + 1, ~~(seat / 20) * 20 + 1, 18, 18);//用color填充一个矩形,以前两个参数为x,y坐标,后两个参数为宽和高。}
document.onkeydown = function(evt) {//当键盘上下左右键摁下的时候改变directiondirection = snake[1] - snake[0] == (n = [-1, -20, 1, 20][(evt || event).keyCode - 37] || direction) ? direction : n;};
!function() {snake.unshift(n = snake[0] + direction);//此时的n为下次蛇头出现的位置,n进入队列 if(snake.indexOf(n, 1) > 0 || n < 0 || n > 399 || direction == 1 && n % 20 == 0 || direction == -1 && n % 20 == 19) {//if语句判断贪吃蛇是否撞到自己或者墙壁,碰到时返回,结束程序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);//每隔0.15秒执行函数一次,可以调节蛇的速度
}();
</script>
</body>
</html>
这不是我写的,是我看见的那个网址里面写的详细的。这个提交是电脑端,我发现一提交就自动压缩,乱了。