Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuhang139
弹球游戏2.0
提交
6ecfbe25
弹
弹球游戏2.0
项目概览
xuhang139
/
弹球游戏2.0
与 Fork 源项目一致
Fork自
inscode / VueJS
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
弹
弹球游戏2.0
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6ecfbe25
编写于
6月 20, 2023
作者:
6
6481820c3fb4c850008641af
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tue Jun 20 06:26:01 UTC 2023 inscode
上级
a5450115
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
67 addition
and
38 deletion
+67
-38
src/App.vue
src/App.vue
+67
-38
未找到文件。
src/App.vue
浏览文件 @
6ecfbe25
<
script
setup
>
<
script
setup
>
import
HelloWorld
from
'
./components/HelloWorld.vue
'
import
{
onMounted
}
from
'
vue
'
;
import
TheWelcome
from
'
./components/TheWelcome.vue
'
</
script
>
<
template
>
<header>
<img
alt=
"Vue logo"
class=
"logo"
src=
"./assets/logo.svg"
width=
"125"
height=
"125"
/>
<div
class=
"wrapper"
>
const
BALL_SIZE
=
20
;
<HelloWorld
msg=
"You did it!"
/>
const
BAT_SIZE
=
50
;
</div>
let
BAT_X
=
0
;
</header>
let
BAT_Y
=
0
;
const
SCORE_MAX
=
100
;
<main>
let
score
=
0
;
<TheWelcome
/>
let
canvas
=
null
;
</main>
let
ctx
=
null
;
</
template
>
onMounted
(()
=>
{
canvas
=
document
.
getElementById
(
'
canvas
'
);
ctx
=
canvas
.
getContext
(
'
2d
'
);
BAT_X
=
canvas
.
width
-
BAT_SIZE
;
BAT_Y
=
canvas
.
height
/
2
-
BAT_SIZE
/
2
;
draw
()
})
<
style
scoped
>
// 定义一个函数用于绘制球和球拍。
header
{
function
draw
()
{
line-height
:
1.5
;
ctx
.
clearRect
(
0
,
0
,
canvas
.
width
,
canvas
.
height
);
drawBall
();
drawBat
();
drawScore
();
}
}
// 定义一个函数用于绘制球。
.logo
{
function
drawBall
()
{
display
:
block
;
ctx
.
beginPath
();
margin
:
0
auto
2rem
;
ctx
.
arc
(
canvas
.
width
/
2
,
canvas
.
height
/
2
,
BALL_SIZE
,
0
,
Math
.
PI
*
2
);
ctx
.
fillStyle
=
'
red
'
;
ctx
.
fill
();
}
}
// 定义一个函数用于绘制球拍。
@media
(
min-width
:
1024px
)
{
function
drawBat
()
{
header
{
ctx
.
beginPath
();
display
:
flex
;
ctx
.
rect
(
BAT_X
,
BAT_Y
,
BAT_SIZE
,
BAT_SIZE
);
place-items
:
center
;
ctx
.
fillStyle
=
'
wood
'
;
padding-right
:
calc
(
var
(
--section-gap
)
/
2
);
ctx
.
fill
();
}
}
// 定义一个函数用于绘制分数。
.logo
{
function
drawScore
()
{
margin
:
0
2rem
0
0
;
ctx
.
font
=
'
30px Arial
'
;
ctx
.
textAlign
=
'
center
'
;
ctx
.
fillStyle
=
'
blue
'
;
ctx
.
fillText
(
'
Score:
'
+
score
,
canvas
.
width
/
2
,
30
);
}
// 定义一个函数用于更新球的位置和速度。
function
update
()
{
// 随机生成球的初始位置和速度
const
ballX
=
Math
.
random
()
*
(
canvas
.
width
-
BALL_SIZE
)
+
BALL_SIZE
/
2
;
const
ballY
=
Math
.
random
()
*
(
canvas
.
height
-
BALL_SIZE
)
+
BALL_SIZE
/
2
;
const
ballSpeedX
=
(
Math
.
random
()
-
0.5
)
*
3
;
const
ballSpeedY
=
(
Math
.
random
()
-
0.5
)
*
3
;
// 更新球的位置和速度
ballX
+=
ballSpeedX
;
ballY
+=
ballSpeedY
;
// 判断球是否碰到边界,如果碰到则反弹
if
(
ballX
<
0
||
ballX
>
canvas
.
width
-
BALL_SIZE
)
{
ballSpeedX
*=
-
1
;
}
}
if
(
ballY
<
0
||
ballY
>
canvas
.
height
-
BALL_SIZE
)
{
header
.wrapper
{
ballSpeedY
*=
-
1
;
display
:
flex
;
place-items
:
flex-start
;
flex-wrap
:
wrap
;
}
}
// 更新球的位置和速度,并在Canvas上重新绘制球的位置和速度的改变。
score
+=
Math
.
abs
(
ballSpeedX
)
+
Math
.
abs
(
ballSpeedY
);
// 更新分数}
}
}
</
script
>
<
template
>
<canvas
id=
"canvas"
width=
"400"
height=
"400"
></canvas>
</
template
>
<
style
scoped
>
</
style
>
</
style
>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录