寒云不落格
Yoky Blog
首页
计算机
艺术&设计
音乐&图片
哲学&文学
资源&网摘
其他
标签
留言
登录
-用户面板-
用户名:
密码:
记住我
-日历-
7
3
2010 - 9
4
8
日
一
二
三
四
五
六
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-统计-
计算机(6)
艺术&设计(1)
音乐&图片(2)
哲学&文学(1)
资源&网摘(13)
其他(2)
日志:26
评论:1
留言:2076
在线:236
访问:338678
注册人数:7
-最新日志-
Javascript数
网页配色方案
PoemMaker,我
vs2008中文版下载
30个被浪费的顶级域名
世界上最早注册的前10
趣味智力问题
javascript,
老人与海
资源站搜集
表单中回车键实现Tab
46个精选奇趣网站
分享奥美广告创意观念
网络广告的种类
用JavaScript
怎样才能把自己的网站做
常用的HR相关英文词汇
asp常见错误
企业网站常用词汇中英文
汉字读音表GB2312
-最新评论-
[color=#8B0
-链接-
美丽城市
M-BLOG
雪眼泪
缘份天津
泪眼问花
kanny7
PoemMaker
天城学生网
-其他信息-
繁體中文
减小字体
|
增大字体
用JavaScript写的贪食蛇游戏
By:yoky | Date:2007-5-8 | Time:15:25:18 | Weather:
CODE:
<html>
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=gb2312"
/>
<style type=
"text/css"
>
.snake{ background:#090}
.food0{ background:#f90}
.food1{ background:#f00}
.bar{ background:#000}
#state{ color:#090; font-weight:bold}
</style>
<script type=
"text/javascript"
>
var
rows =
16
;
var
cells =
30
;
var
squareWidth =
20
;
var
speed =
5000
;
var
borderWidth =
5
;
var
times =
200
;
//
0.2
秒
var
start =
0
;
var
pause =
0
;
var
sUp;
window.onload =
function
createMap()
{
mapWidth = eval(cells*squareWidth+
2
*borderWidth);
mapHeight = eval(rows*squareWidth+
2
*borderWidth);
mapLeft = (document.body.clientWidth-mapWidth)/
2
;
mapTop = (document.body.clientHeight-mapHeight)/
2
;
document.body.innerHTML +=
"<div id=\"
map\
" style=\"
position:absolute; left:
" + mapLeft + "
px; top:
" + mapTop + "
px; width:
" + mapWidth + "
px; height:
" + mapHeight + "
px; border:
" + borderWidth + "
px solid #789;\
"></div>"
;
gameMap =
new
Array()
for
(y=
0
;y<rows;y++)
{
gameMap
[y]
=
new
Array();
for
(x=
0
;x<cells;x++)
{
gameMap
[y]
[x]
=
'0'
;
}
}
snakeX = parseInt(Math.random()*cells);
snakeY = parseInt(Math.random()*rows);
createSnake();
createFood();
createBar();
snakeDIV = map.all.tags(
"DIV"
);
}
function
createSnake()
//创建蛇
{
map.innerHTML +=
"<div x=\"
" + snakeX + "
\
" y=\"
" + snakeY + "
\
" style=\"
position:absolute; left:
" + snakeX * squareWidth + "
px; top:
" + snakeY * squareWidth + "
px; width:
" + squareWidth + "
px; height:
" + squareWidth + "
px; overflow:hidden;\
" class=\"
snake\
"></div>"
;
gameMap
[snakeY]
[snakeX]
=
's'
;
}
function
createFood()
//创建食物
{
foodX = parseInt(Math.random() * cells);
foodY = parseInt(Math.random() * rows);
if
(gameMap
[foodY]
[foodX]
==
'0'
)
{
map.innerHTML +=
"<span id=\"
food\
" style=\"
position:absolute; left:
" + foodX * squareWidth + "
; top:
" + foodY * squareWidth + "
; width:
" + squareWidth + "
; height:
" + squareWidth +"
; overflow:hidden;\
" class=\"
food
" + parseInt(Math.random() *
2
) + "
\
"></span>"
gameMap
[foodY]
[foodX]
=
'f'
;
}
else
{
createFood();
}
}
function
createBar()
//创建障碍物
{
barX = parseInt(Math.random() * cells);
barY = parseInt(Math.random() * rows);
if
(gameMap
[barY]
[barX]
==
'0'
)
{
map.innerHTML +=
"<span id=\"
bar\
" style=\"
position:absolute; left:
" + barX * squareWidth + "
; top:
" + barY * squareWidth + "
; width:
" + squareWidth + "
; height:
" + squareWidth +"
; overflow:hidden;\
" class=\"
bar\
"></span>"
gameMap
[barY]
[barX]
=
'b'
;
}
else
{
createBar();
}
}
document.onkeydown =
function
keyDown(){
key = event.keyCode;
switch(key)
{
case
37
:
//左
snakeGo(-
1
,0);
document.getElementById(
"state"
).innerHTML =
"左行"
;
break
;
case
39
:
//右
snakeGo(1,0);
document.getElementById(
"state"
).innerHTML =
"右行"
;
break
;
case
38
:
//上
snakeGo(0,-
1
);
document.getElementById(
"state"
).innerHTML =
"上行"
;
break
;
case
40
:
//下
snakeGo(0,1);
document.getElementById(
"state"
).innerHTML =
"下行"
;
break
;
case
32
:
//空格键
if
(start ==
1
)
{
pause =
1
;
document.getElementById(
"state"
).innerHTML +=
"(暂停)"
;
clearTimeout(sUp);
}
break
;
}
return
false;
}
function
snakeGo(x,y)
{
goX = x;
goY = y;
if
(start ==
0
)
{
speedUp();
start =
1
;
move();
}
else
{
if
(pause ==
1
)
{
speedUp();
pause =
0
;
move();
}
}
}
function
speedUp()
//蛇越行越快
{
times -=
4
;
if
(times >
4
)
{
sUp = setTimeout(
"speedUp()"
,speed);
}
}
function
move()
//移动并判断蛇前面是什么
{
snakeX += goX;
snakeY += goY;
if
(snakeY<0||snakeY>=rows||snakeX<0||snakeX>=cells)
{
gameOver(
"撞墙了"
);
}
else
{
snakeFront = gameMap
[snakeY]
[snakeX]
;
if
(snakeFront ==
'0'
)
//蛇前面为空
{
crawl();
}
else
if(snakeFront ==
'f'
)
//蛇前面为食物
{
eat();
}
else
if(snakeFront ==
'b'
)
//蛇前面为障碍物
{
gameOver(
"撞到障碍物"
);
}
else
if(snakeFront ==
's'
)
//蛇前面为蛇
{
gameOver(
"咬到自己了"
);
}
}
}
function
gameOver(type)
//游戏结束
{
document.getElementById(
"state"
).innerHTML =
"游戏结束"
;
reStart =
confirm
(
"^_^啊哦~,"
+type+
",重新开始吗?"
)
if
(reStart)
{
window.location.reload();
//刷新页面
}
}
function
crawl()
//蛇前是空地时
{
//清除蛇尾,创建蛇头,产生爬行效果
gameMap
[snakeDIV[0]
.y]
[snakeDIV[0]
.x] =
'0'
;
snakeDIV
[0]
.removeNode(true);
createSnake();
if
(pause ==
0
)
{
setTimeout(
"move()"
,times);
}
}
function
eat()
//蛇前面是食物时
{
createSnake();
//增加一个蛇的节点
document.getElementById(
"food"
).removeNode(true);
//清除食物
createFood();
//创建食物
if
(pause ==
0
)
{
setTimeout(
"move()"
,times);
}
}
</script>
<title>贪食蛇</title>
</head>
<body>
<h2>贪食蛇</h2>
<hr style=
"color:#333"
size=
"1px"
/>
<p style=
"font-size:13px;"
>说明:1.绿色方块代表蛇,橙色或红色方块代表食物,黑色方块代表障碍物。2.用方向键控制蛇的方向,空格键暂停,方向键开始。<br />当前状态:<span id=
"state"
>准备开始</span><br />Author:Yoky</p>
</body>
</html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <style type="text/css"> .snake{ background:#090} .food0{ background:#f90} .food1{ background:#f00} .bar{ background:#000} #state{ color:#090; font-weight:bold} </style> <script type="text/javascript"> var rows = 16; var cells = 30; var squareWidth = 20; var speed = 5000; var borderWidth = 5; var times = 200;//0.2秒 var start = 0; var pause = 0; var sUp; window.onload = function createMap() { mapWidth = eval(cells*squareWidth+2*borderWidth); mapHeight = eval(rows*squareWidth+2*borderWidth); mapLeft = (document.body.clientWidth-mapWidth)/2; mapTop = (document.body.clientHeight-mapHeight)/2; document.body.innerHTML += "<div id=\"map\" style=\"position:absolute; left:" + mapLeft + "px; top:" + mapTop + "px; width:" + mapWidth + "px; height:" + mapHeight + "px; border:" + borderWidth + "px solid #789;\"></div>"; gameMap = new Array() for(y=0;y<rows;y++) { gameMap[y]=new Array(); for(x=0;x<cells;x++) { gameMap[y][x]='0'; } } snakeX = parseInt(Math.random()*cells); snakeY = parseInt(Math.random()*rows); createSnake(); createFood(); createBar(); snakeDIV = map.all.tags("DIV"); } function createSnake()//创建蛇 { map.innerHTML += "<div x=\"" + snakeX + "\" y=\"" + snakeY + "\" style=\"position:absolute; left:" + snakeX * squareWidth + "px; top:" + snakeY * squareWidth + "px; width:" + squareWidth + "px; height:" + squareWidth + "px; overflow:hidden;\" class=\"snake\"></div>"; gameMap[snakeY][snakeX]='s'; } function createFood()//创建食物 { foodX = parseInt(Math.random() * cells); foodY = parseInt(Math.random() * rows); if(gameMap[foodY][foodX]=='0') { map.innerHTML += "<span id=\"food\" style=\"position:absolute; left:" + foodX * squareWidth + "; top:" + foodY * squareWidth + "; width:" + squareWidth + "; height:" + squareWidth +"; overflow:hidden;\" class=\"food" + parseInt(Math.random() * 2) + "\"></span>" gameMap[foodY][foodX]='f'; } else { createFood(); } } function createBar()//创建障碍物 { barX = parseInt(Math.random() * cells); barY = parseInt(Math.random() * rows); if(gameMap[barY][barX]=='0') { map.innerHTML += "<span id=\"bar\" style=\"position:absolute; left:" + barX * squareWidth + "; top:" + barY * squareWidth + "; width:" + squareWidth + "; height:" + squareWidth +"; overflow:hidden;\" class=\"bar\"></span>" gameMap[barY][barX]='b'; } else { createBar(); } } document.onkeydown = function keyDown(){ key = event.keyCode; switch(key) { case 37://左 snakeGo(-1,0); document.getElementById("state").innerHTML = "左行"; break; case 39://右 snakeGo(1,0); document.getElementById("state").innerHTML = "右行"; break; case 38://上 snakeGo(0,-1); document.getElementById("state").innerHTML = "上行"; break; case 40://下 snakeGo(0,1); document.getElementById("state").innerHTML = "下行"; break; case 32://空格键 if(start == 1) { pause = 1; document.getElementById("state").innerHTML += "(暂停)"; clearTimeout(sUp); } break; } return false; } function snakeGo(x,y) { goX = x; goY = y; if(start == 0) { speedUp(); start = 1; move(); } else { if(pause == 1) { speedUp(); pause = 0; move(); } } } function speedUp()//蛇越行越快 { times -= 4; if(times > 4) { sUp = setTimeout("speedUp()",speed); } } function move()//移动并判断蛇前面是什么 { snakeX += goX; snakeY += goY; if(snakeY<0||snakeY>=rows||snakeX<0||snakeX>=cells) { gameOver("撞墙了"); } else { snakeFront = gameMap[snakeY][snakeX]; if(snakeFront == '0')//蛇前面为空 { crawl(); } else if(snakeFront == 'f')//蛇前面为食物 { eat(); } else if(snakeFront == 'b')//蛇前面为障碍物 { gameOver("撞到障碍物"); } else if(snakeFront == 's')//蛇前面为蛇 { gameOver("咬到自己了"); } } } function gameOver(type)//游戏结束 { document.getElementById("state").innerHTML = "游戏结束"; reStart = confirm("^_^啊哦~,"+type+",重新开始吗?") if(reStart) { window.location.reload();//刷新页面 } } function crawl()//蛇前是空地时 { //清除蛇尾,创建蛇头,产生爬行效果 gameMap[snakeDIV[0].y][snakeDIV[0].x] = '0'; snakeDIV[0].removeNode(true); createSnake(); if(pause == 0) { setTimeout("move()",times); } } function eat()//蛇前面是食物时 { createSnake();//增加一个蛇的节点 document.getElementById("food").removeNode(true);//清除食物 createFood();//创建食物 if(pause == 0) { setTimeout("move()",times); } } </script> <title>贪食蛇</title> </head> <body> <h2>贪食蛇</h2> <hr style="color:#333" size="1px" /> <p style="font-size:13px;">说明:1.绿色方块代表蛇,橙色或红色方块代表食物,黑色方块代表障碍物。2.用方向键控制蛇的方向,空格键暂停,方向键开始。<br />当前状态:<span id="state">准备开始</span><br />Author:Yoky</p> </body> </html>
[提示:你可先修改部分代码,再按运行]
Tags:
JavaScript
|
游戏
|
文章来自:
本站原创
评论:(
0
) | 查看次数:(
624
)
评论:
1
评论
昵称:
主页:
字体:
宋体
楷体
新宋体
黑体
隶书
字体大小:
1
2
3
4
5
颜色:
请选择颜色
#F0F8FF
#FAEBD7
#00FFFF
#7FFFD4
#F0FFFF
#F5F5DC
#FFE4C4
#000000
#FFEBCD
#0000FF
#8A2BE2
#A52A2A
#DEB887
#5F9EA0
#7FFF00
#D2691E
#FF7F50
#6495ED
#FFF8DC
#DC143C
#00FFFF
#00008B
#008B8B
#B8860B
#A9A9A9
#006400
#BDB76B
#8B008B
#556B2F
#FF8C00
#9932CC
#8B0000
#E9967A
#8FBC8F
#483D8B
#2F4F4F
#00CED1
#9400D3
#FF1493
#00BFFF
#696969
#1E90FF
#B22222
#FFFAF0
#228B22
#FF00FF
#DCDCDC
#F8F8FF
#FFD700
#DAA520
#808080
#008000
#ADFF2F
#F0FFF0
#FF69B4
#CD5C5C
#4B0082
#FFFFF0
#F0E68C
#E6E6FA
#FFF0F5
#7CFC00
#FFFACD
#ADD8E6
#F08080
#E0FFFF
#FAFAD2
#90EE90
#D3D3D3
#FFB6C1
#FFA07A
#20B2AA
#87CEFA
#778899
#B0C4DE
#FFFFE0
#00FF00
#32CD32
#FAF0E6
#FF00FF
#800000
#66CDAA
#0000CD
#BA55D3
#9370DB
#3CB371
#7B68EE
#00FA9A
#48D1CC
#C71585
#191970
#F5FFFA
#FFE4E1
#FFE4B5
#FFDEAD
#000080
#FDF5E6
#808000
#6B8E23
#FFA500
#FF4500
#DA70D6
#EEE8AA
#98FB98
#AFEEEE
#DB7093
#FFEFD5
#FFDAB9
#CD853F
#FFC0CB
#DDA0DD
#B0E0E6
#800080
#FF0000
#BC8F8F
#4169E1
#8B4513
#FA8072
#F4A460
#2E8B57
#FFF5EE
#A0522D
#C0C0C0
#87CEEB
#6A5ACD
#708090
#FFFAFA
#00FF7F
#4682B4
#D2B48C
#008080
#D8BFD8
#FF6347
#40E0D0
#EE82EE
#F5DEB3
#FFFFFF
#F5F5F5
#FFFF00
#9ACD32
内容: