zoukankan
html css js c++ java
贪食蛇JS源码
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head
>
<
title
>
Snake
</
title
>
<
style type
=
"
text/css
"
>
body
{
background
-
color:White;
font
-
size:12px;
}
#main
{
position:absolute;
410px;
height:310px;
border
-
style:inset;
border
-
color:#0000cc;
border
-
5px;
}
#monitor
{
position:absolute;
top:330px;
}
.snake
{
20px;
height:20px;
overflow:hidden;
background
-
color:blue;
position:absolute;
}
.food
{
20px;
height:20px;
overflow:hidden;
background
-
color:red;
position:absolute;
}
</
style
>
<
script type
=
"
text/jscript
"
>
var
AllDiv, AllSpan
var
Sx, Sy;
//
Snake的头
var
Gox, Goy;
//
移动方向
var
IsStart
=
false
;
//
是否开始
var
Fx, Fy;
//
食物的地址
var
TimeHandle;
var
IsOver
=
false
;
var
Sec
=
0
;
//
记时
function
page_load()
{
CreateSnake(
0
,
0
);
CreateSnake(
0
,
20
);
CreateSnake(
20
,
20
);
AllDiv
=
main.all.tags('DIV');
CreatFood();
AllSpan
=
main.all.tags('SPAN');
}
function
CreateSnake(x,y)
{
main.innerHTML
+=
"
<div class=\
"
snake\
"
style=\
"
top:
"
+y+
"
; left:
"
+x+
"
\
"
></div>
"
;
Sx
=
x;
Sy
=
y;
dSx.innerText
=
Sx;
dSy.innerText
=
Sy;
}
function
CreatFood()
{
if
(AllSpan
!=
null
)
{
AllSpan[
0
].removeNode(
true
);
}
do
{
Fx
=
Math.round(Math.random()
*
19
)
*
20
;
Fy
=
Math.round(Math.random()
*
14
)
*
20
;
dFx.innerText
=
Fx;
dFy.innerText
=
Fy;
}
while
(CheckBody(Fx,Fy))
main.innerHTML
+=
"
<span class=\
"
food\
"
style=\
"
top:
"
+Fy+
"
; left:
"
+Fx+
"
\
"
></span>
"
;
}
function
Move()
{
var
NSx
=
Sx
+
Gox;
var
NSy
=
Sy
+
Goy;
//
下个格子
//
是否GameOver
IsOver
=
IsGameOver(NSx,NSy);
if
(IsOver)
{
return
;
}
if
(NSx
==
Fx
&&
NSy
==
Fy)
{
CreatFood();
}
else
{
AllDiv[
0
].removeNode(
true
);
//
移动
}
CreateSnake(NSx,NSy);
}
function
IsGameOver(x,y)
{
if
(x
<
0
||
x
>=
400
||
y
>=
300
||
y
<
0
)
{
return
true
;
}
return
CheckBody(x,y)
}
//
键盘控制
function
KeyDown(){
if
(IsOver)
return
;
Key
=
event.keyCode
switch
(Key){
case
37
:Dir(
-
1
,
0
);
break
//
左
case
39
:Dir(
1
,
0
);
break
//
右
case
38
:Dir(
0
,
-
1
);
break
//
上
case
40
:Dir(
0
,
1
);
break
}
//
下
return
false
}
function
Dir(x,y)
{
if
(Gox
==
-
x
*
20
&&
Goy
==
-
y
*
20
)
return
Gox
=
x
*
20
;
Goy
=
y
*
20
;
if
(IsStart
==
false
)
{
IsStart
=
true
;
PlayGame();
}
dGox.innerText
=
Gox;
dGoy.innerText
=
Goy;
}
//
开始游戏,并不停移动
function
PlayGame()
{
Sec
++
;
if
(IsOver)
{
clearTimeout(TimeHandle);
}
else
{
Move();
TimeHandle
=
setTimeout('PlayGame()',
50
)
}
var
d
=
new
Date(
0
,
0
,
0
,
0
,
0
,
0
,
0
);
d.setTime(d.getTime()
+
Sec
*
50
);
dSec.innerText
=
Sec;
dTime.innerText
=
d.toLocaleTimeString();
}
//
检测食物
function
CheckBody(x,y)
{
for
(
var
i
=
0
; i
<
AllDiv.length;i
++
)
{
if
(AllDiv[i].style.left
==
x
+
"
px
"
&&
AllDiv[i].style.top
==
y
+
"
px
"
)
{
return
true
;
}
}
return
false
;
}
</
script
>
</
head
>
<
body onload
=
"
page_load()
"
onkeydown
=
"
KeyDown()
"
>
<
div id
=
"
main
"
>
</
div
>
<
div id
=
"
monitor
"
>
<
input type
=
"
button
"
value
=
"
test
"
onclick
=
"
clearTimeout(TimeHandle)
"
/>
<
fieldset
>
<
legend
>
参数
</
legend
>
<
ul
>
<
li
>
Sx:
<
span id
=
"
dSx
"
></
span
></
li
>
<
li
>
Sy:
<
span id
=
"
dSy
"
></
span
></
li
>
<
li
>
Fx:
<
span id
=
"
dFx
"
></
span
></
li
>
<
li
>
Fy:
<
span id
=
"
dFy
"
></
span
></
li
>
<
li
>
Gox:
<
span id
=
"
dGox
"
></
span
></
li
>
<
li
>
Goy:
<
span id
=
"
dGoy
"
></
span
></
li
>
<
li
>
Sec:
<
span id
=
"
dSec
"
></
span
></
li
>
<
li
>
Time:
<
span id
=
"
dTime
"
></
span
></
li
>
</
ul
>
</
fieldset
>
</
div
>
</
body
>
</
html
>
查看全文
相关阅读:
struts2的结果类型
struts2的基本配置
struts2拦截器
struts2入门程序
struts2工作原理
Mysql处理字符串函数(转)
oracle调用java方法的例子(下面所有代码都是在sql/plus中写)
Oracle数据库中调用Java类开发存储过程、函数的方法
Oracle触发器反向调用Java程序
javaweb项目打包成war包
原文地址:https://www.cnblogs.com/goodspeed/p/67616.html
最新文章
python正则表达式
算法总结之 最大值减去最小值或等于num的子数组数量
算法总结之 构造数组MaxTree
算法总结之 生成窗口的最大值数组
操作系统笔试
算法总结之 用一个栈实现另一个栈的排序
娱乐门户网站
Nginx开启Gzip压缩大幅提高页面加载速度
[运营经验] 网站分析:网站为什么只收录首页,内页一直不收录呢!
CentOS 6.4下编译安装MySQL 5.6.14
热门文章
http://www.shippingcity.com/
linux shell if 参数
lua的时间和日期函数
php新版本废弃 preg_replace /e 修饰符
CentOS上扩充lv-root空间步骤详解
SMARTY 变量
struts2国际化
Struts2与ServletAPI解耦
struts2类型转换
struts2的通配符与动态方法调用
Copyright © 2011-2022 走看看