Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
贪吃蛇
提交
6f81ab12
贪吃蛇
项目概览
张重言
/
贪吃蛇
通知
11
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
贪吃蛇
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6f81ab12
编写于
9月 07, 2020
作者:
张重言
🌈
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Let us start eat
上级
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
214 addition
and
0 deletion
+214
-0
game.html
game.html
+214
-0
未找到文件。
game.html
0 → 100644
浏览文件 @
6f81ab12
<!DOCTYPE html>
<html>
<head>
<title>
贪吃蛇游戏
</title>
<style
type=
"text/css"
>
.chicken-square
{
background-color
:
black
;
width
:
5px
;
height
:
5px
;
float
:
left
;
}
.square
{
width
:
5px
;
height
:
5px
;
float
:
left
;
}
#main
{
height
:
502px
;
width
:
502px
;
border
:
1px
solid
black
;
}
.black-square
{
background-color
:
black
;
width
:
5px
;
height
:
5px
;
float
:
left
;
}
.hide
{
display
:
none
;
}
.show
{
display
:
block
;
}
.red
{
color
:
red
;
}
</style>
</head>
<body>
<div
id=
"main"
>
</div>
<button
id=
"start"
onclick=
"load()"
>
开始游戏
</button>
<p>
你的得分:
<span
id=
"score"
class=
"red"
>
0
</span></p>
<script
type=
"text/javascript"
>
function
load
(){
document
.
getElementById
(
"
start
"
).
className
=
"
hide
"
;
var
player
=
{
name
:
""
,
score
:
0
};
var
snake
=
{
body
:
[],
intervarID
:
1
,
status
:
true
,
length
:
1
,
position
:
[
30
,
30
],
direction
:
1
,
turnLeft
:
function
(){
if
(
this
.
direction
!==
3
&&
this
.
direction
!==
1
){
this
.
direction
=
3
}
else
{
return
false
;
}
},
turnRight
:
function
(){
if
(
this
.
direction
!==
1
&&
this
.
direction
!==
3
){
this
.
direction
=
1
}
else
{
return
false
;
}
},
turnTop
:
function
(){
if
(
this
.
direction
!==
4
&&
this
.
direction
!==
2
){
this
.
direction
=
4
}
else
{
return
false
;
}
},
turnDown
:
function
(){
if
(
this
.
direction
!==
2
&&
this
.
direction
!==
4
){
this
.
direction
=
2
}
else
{
return
false
;
}
},
move
:
function
(){
if
(
this
.
position
[
0
]
<
100
&&
this
.
position
[
0
]
>
1
&&
this
.
position
[
1
]
<
100
&&
this
.
position
[
1
]
>
1
){
switch
(
this
.
direction
){
case
1
:
this
.
position
[
0
]
+=
1
this
.
change
();
break
;
case
2
:
this
.
position
[
1
]
+=
1
this
.
change
();
break
;
case
3
:
this
.
position
[
0
]
-=
1
this
.
change
();
break
;
case
4
:
this
.
position
[
1
]
-=
1
this
.
change
();
break
;
}
}
else
{
window
.
clearInterval
(
this
.
intervarID
);
alert
(
"
game over and your score is :
"
+
player
.
score
)
document
.
getElementById
(
"
start
"
).
className
=
"
show
"
;
return
false
;
}
},
change
:
function
(){
var
x
=
this
.
position
[
0
]
var
y
=
this
.
position
[
1
]
this
.
body
.
unshift
([
x
,
y
])
this
.
body
.
pop
()
this
.
draw
();
},
draw
:
function
(){
if
(
this
.
body
[
0
][
0
]
==
chicken
.
position
[
0
]
&&
this
.
body
[
0
][
1
]
==
chicken
.
position
[
1
]){
switch
(
this
.
direction
){
case
1
:
var
x
=
this
.
position
[
0
]
-
1
;
var
y
=
this
.
position
[
1
];
this
.
body
.
push
([
x
,
y
]);
break
;
case
2
:
var
x
=
this
.
position
[
0
];
var
y
=
this
.
position
[
1
]
-
1
;
this
.
body
.
push
([
x
,
y
]);
break
;
case
3
:
var
x
=
this
.
position
[
0
]
+
1
;
var
y
=
this
.
position
[
1
];
this
.
body
.
push
([
x
,
y
]);
break
;
case
4
:
var
x
=
this
.
position
[
0
];
var
y
=
this
.
position
[
1
]
+
1
;
this
.
body
.
push
([
x
,
y
]);
break
;
}
player
.
score
+=
1
;
document
.
getElementById
(
"
score
"
).
innerHTML
=
player
.
score
chicken
.
init
();
}
var
squares
=
document
.
getElementsByClassName
(
"
black-square
"
);
for
(
var
i
=
0
;
i
<
squares
.
length
;
i
++
){
squares
[
i
].
className
=
"
square
"
}
for
(
var
i
=
0
;
i
<
this
.
body
.
length
;
i
++
){
var
pos
=
(
this
.
body
[
i
][
1
])
*
100
+
this
.
body
[
i
][
0
];
document
.
querySelectorAll
(
"
#main>div
"
)[
pos
-
1
].
className
=
'
black-square
'
;
}
},
cricle
:
function
(
x
){
y
=
50
+
Math
.
sqrt
(
49
*
49
-
Math
.
pow
((
50
-
x
),
2
));
return
parseInt
(
y
);
},
init
:
function
(){
this
.
body
.
push
(
this
.
position
)
this
.
intervalID
=
window
.
setInterval
(
function
(){
snake
.
move
()},
80
);
chicken
.
init
();
}
};
var
map
=
{
height
:
5
,
width
:
5
,
init
:
function
(){
document
.
getElementById
(
"
main
"
).
innerHTML
=
""
;
for
(
var
i
=
1
;
i
<
10001
;
i
++
){
var
node
=
"
<div class='square' id='
"
+
i
+
"
'></div>
"
;
document
.
getElementById
(
"
main
"
).
insertAdjacentHTML
(
'
beforeend
'
,
node
);
}
}
};
var
chicken
=
{
position
:
[
1
,
1
],
init
:
function
(){
var
current_chicken
=
document
.
getElementsByClassName
(
"
chicken-square
"
)[
0
]
if
(
current_chicken
){
current_chicken
.
className
=
'
square
'
}
this
.
position
[
0
]
=
Math
.
floor
(
Math
.
random
()
*
100
);
this
.
position
[
1
]
=
Math
.
floor
(
Math
.
random
()
*
100
);
var
pos
=
(
this
.
position
[
1
])
*
100
+
this
.
position
[
0
];
document
.
querySelectorAll
(
"
#main>div
"
)[
pos
-
1
].
className
=
'
chicken-square
'
;
}
};
map
.
init
();
snake
.
init
();
window
.
onkeydown
=
function
()
{
console
.
log
(
event
.
keyCode
)
switch
(
event
.
keyCode
){
case
37
:
snake
.
turnLeft
();
break
;
case
38
:
snake
.
turnTop
();
break
;
case
39
:
snake
.
turnRight
();
break
;
case
40
:
snake
.
turnDown
();
break
;
}
}
}
</script>
</body>
</html>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录