-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_es6.js
175 lines (152 loc) · 4.63 KB
/
main_es6.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 设置画布
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
const width = canvas.width = window.innerWidth
const height = canvas.height = window.innerHeight
// 定义关于 ctx 的常量
let ctxConfig = {
bgColor: 'rgba(0, 0, 0)', // 背景颜色
transColor: 'rgba(0, 0, 0, 0.25)' // 球的运动过渡背景颜色,透明度越高,球的运动轨迹越明显
}
// 定义玩家
let user = {}
function initUser() {
user = JSON.parse('{}')
user = new User()
user.updateText()
}
// 初始化所有彩球
function initBalls() {
Ball.initBalls({ width, height })
ctx.fillStyle = ctxConfig.bgColor
ctx.fillRect(0, 0, width, height)
}
// 定义关于 ctx 的点击事件
canvas.onclick = function(e) {
for(let j = 0; j < Ball.balls.length; j++) {
const dx = e.offsetX - Ball.balls[j].x
const dy = e.offsetY - Ball.balls[j].y
const distance = Math.sqrt(dx * dx + dy * dy)
// 是否在此球和其周围发生的点击事件
if (distance < Ball.balls[j].velY + 2 * Ball.balls[j].size) {
console.log('distance', distance)
Ball.balls[j].color.clicked += 1
// 满足点击次数要求
if (Ball.balls[j].color.clicked >= Ball.balls[j].color.click) {
// 点击球的处理
clickWork(Ball.balls[j])
// 重置
Ball.balls[j].init({ width, height })
}
}
}
}
// 球触到下边界的处理
function borderWork(ball) {
// console.log('borderWork ball', ball)
if (user.life <= 0) return
user.life += ball.color.border
console.log('color: ' + ball.color.color + ',border: ' + ball.color.border + ',life: ' + user.life)
user.life = user.life < 0 ? 0 : user.life
user.updateText()
if (user.life <= 0) {
// 需使用线程调用pauseGame()
const st = setTimeout(() => {
pauseGame()
clearTimeout(st)
alert('游戏结束')
}, 100)
}
}
// 点击球的处理
function clickWork(ball) {
// console.log('clickWork ball', ball)
if (user.life <= 0) return
user.score += ball.color.score
user.life += ball.color.life
console.log('color: ' + ball.color.color + ',click: ' + ball.color.click+ ',clicked: ' + ball.color.clicked + ',life: ' + user.life)
user.life = user.life < 0 ? 0 : user.life
user.updateText()
if (user.life <= 0) {
// 需使用线程调用pauseGame()
const st = setTimeout(() => {
pauseGame()
clearTimeout(st)
alert('游戏结束')
}, 100)
}
}
// 动画兼容性
let requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame
let cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame
// 定义一个循环来不停地播放
let myReq
function loop() {
// 将整个画布的颜色设置成半透明的黑色,在下一个视图画出来时用来遮住之前的视图的。如果不这样做得话,你就会在屏幕上看到一条蛇的形状而不是小球的运动了。
ctx.fillStyle = ctxConfig.transColor
ctx.fillRect(0, 0, width, height)
for(let i = 0; i < Ball.balls.length; i++) {
Ball.balls[i].draw(ctx)
Ball.balls[i].update({ width, height, borderWork })
Ball.balls[i].collisionDetect()
}
myReq = requestAnimationFrame(loop)
}
// 开始动画
function startGame() {
if (user.life <= 0) {
alert('生命值为0,请点击重来')
return
}
myReq = requestAnimationFrame(loop)
}
// 暂停动画
function pauseGame() {
if(myReq) {
cancelAnimationFrame(myReq)
myReq = null
}
}
// 重来
function againGame() {
pauseGame()
initBalls()
initUser()
}
againGame()
// 玩法
function showIntroduce() {
showSomeDom('introduce')
}
// 设置
function showSetting() {
pauseGame()
showSomeDom('setting')
}
// 保存设置
function saveSetting() {
// 设置球相关属性
const keys = Object.keys(Ball.ballConfig)
keys.forEach(key => {
if (document.getElementsByName(key).length > 0) {
Ball.ballConfig[key] = Number(document.getElementsByName(key)[0].value)
}
})
console.log('Ball.ballConfig', Ball.ballConfig)
// 设置玩家相关属性
const userKeys = Object.keys(User.userConfig)
userKeys.forEach(key => {
if (document.getElementsByName(key).length > 0) {
User.userConfig[key] = Number(document.getElementsByName(key)[0].value)
}
})
console.log('User.userConfig', User.userConfig)
againGame()
showSomeDom('setting')
}
// 显示或隐藏dom
function showSomeDom(selector) {
const el = document.querySelector(selector)
el.style.visibility = el.style.visibility === 'visible' ? 'hidden' : 'visible'
}