Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
眼红的小孩
VueJS_673864
提交
95db06bc
V
VueJS_673864
项目概览
眼红的小孩
/
VueJS_673864
与 Fork 源项目一致
Fork自
inscode / VueJS
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
VueJS_673864
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
95db06bc
编写于
3月 18, 2025
作者:
Q
qq_63480508
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tue Mar 18 16:05:00 CST 2025 inscode
上级
aa50c4cb
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
137 addition
and
32 deletion
+137
-32
src/components/HelloWorld.vue
src/components/HelloWorld.vue
+137
-32
未找到文件。
src/components/HelloWorld.vue
浏览文件 @
95db06bc
<
script
setup
>
defineProps
({
msg
:
{
type
:
String
,
required
:
true
}
})
</
script
>
<
template
>
<
template
>
<div
class=
"greetings"
>
<div
class=
"game-container"
>
<h1
class=
"green"
>
{{
msg
}}
</h1>
<canvas
ref=
"canvas"
width=
"500"
height=
"400"
></canvas>
<h3>
<p>
Score:
{{
score
}}
</p>
You’ve successfully created a project with
<a
target=
"_blank"
href=
"https://vitejs.dev/"
>
Vite
</a>
+
<a
target=
"_blank"
href=
"https://vuejs.org/"
>
Vue 3
</a>
.
</h3>
</div>
</div>
</
template
>
</
template
>
<
style
scoped
>
<
script
>
h1
{
import
{
ref
,
onMounted
}
from
'
vue
'
;
font-weight
:
500
;
font-size
:
2.6rem
;
top
:
-10px
;
}
h3
{
export
default
{
font-size
:
1.2rem
;
name
:
"
SnakeGnme
"
,
setup
(){
const
canvas
=
ref
(
null
);
let
ctx
=
null
;
let
snake
=
[];
let
food
=
{};
let
dx
=
20
;
let
dy
=
0
;
let
score
=
0
;
let
gameInterval
=
null
;
const
initGname
=
()
=>
{
ctx
=
canvas
.
value
.
getContext
()
}
}
}
.greetings
h1
,
const
generateFood
=
()
=>
{
.greetings
h3
{
food
=
{
text-align
:
center
;
x
:
Math
.
round
(
Math
.
random
()
*
(
canvas
.
value
.
width
-
20
)
/
20
)
*
20
,
y
:
Math
.
round
(
Math
.
random
()
*
(
canvas
.
value
.
height
-
20
)
/
20
)
*
20
,
};
};
const
draw
=
()
=>
{
ctx
.
clearRect
(
0
,
0
,
canvas
.
value
.
width
,
canvas
.
value
.
height
);
snake
.
forEach
(({
x
,
y
})
=>
{
ctx
.
fillStyle
=
'
green
'
;
ctx
.
fillRect
(
x
,
y
,
20
,
20
);
});
ctx
.
fillStyle
=
'
red
'
;
ctx
.
fillRect
(
food
.
x
,
food
.
y
,
20
,
20
);
document
.
querySelector
(
'
.game-container p
'
).
textContent
=
`Score:
${
score
}
`
;
};
const
moveSnake
=
()
=>
{
const
head
=
{
x
:
snake
[
0
].
x
+
dx
,
y
:
snake
[
0
].
y
+
dy
};
snake
.
unshift
(
head
);
if
(
head
.
x
===
food
.
x
&&
head
.
y
===
food
.
y
)
{
score
+=
10
;
generateFood
();
}
else
{
snake
.
pop
();
}
};
const
checkCollision
=
()
=>
{
const
head
=
snake
[
0
];
if
(
head
.
x
<
0
||
head
.
y
<
0
||
head
.
x
>=
canvas
.
value
.
width
||
head
.
y
>=
canvas
.
value
.
height
)
{
clearInterval
(
gameInterval
);
initGame
();
return
true
;
}
for
(
let
i
=
1
;
i
<
snake
.
length
;
i
++
)
{
if
(
head
.
x
===
snake
[
i
].
x
&&
head
.
y
===
snake
[
i
].
y
)
{
clearInterval
(
gameInterval
);
initGame
();
return
true
;
}
}
return
false
;
};
const
startGame
=
()
=>
{
if
(
gameInterval
)
clearInterval
(
gameInterval
);
gameInterval
=
setInterval
(()
=>
{
moveSnake
();
draw
();
checkCollision
();
},
100
);
};
const
changeDirection
=
(
event
)
=>
{
const
LEFT_KEY
=
37
;
const
RIGHT_KEY
=
39
;
const
UP_KEY
=
38
;
const
DOWN_KEY
=
40
;
const
keyPressed
=
event
.
keyCode
;
const
goingUp
=
dy
===
-
20
;
const
goingDown
=
dy
===
20
;
const
goingRight
=
dx
===
20
;
const
goingLeft
=
dx
===
-
20
;
if
(
keyPressed
===
LEFT_KEY
&&
!
goingRight
)
{
dx
=
-
20
;
dy
=
0
;
}
if
(
keyPressed
===
UP_KEY
&&
!
goingDown
)
{
dx
=
0
;
dy
=
-
20
;
}
if
(
keyPressed
===
RIGHT_KEY
&&
!
goingLeft
)
{
dx
=
20
;
dy
=
0
;
}
if
(
keyPressed
===
DOWN_KEY
&&
!
goingUp
)
{
dx
=
0
;
dy
=
20
;
}
};
onMounted
(()
=>
{
initGame
();
window
.
addEventListener
(
'
keydown
'
,
changeDirection
);
});
return
{
canvas
,
score
};
},
};
</
script
>
<
style
scoped
>
.game-container
{
display
:
flex
;
flex-direction
:
column
;
align-items
:
center
;
}
}
@media
(
min-width
:
1024px
)
{
canvas
{
.greetings
h1
,
border
:
1px
solid
black
;
.greetings
h3
{
text-align
:
left
;
}
}
}
</
style
>
</
style
>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录