-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
354 lines (331 loc) · 10 KB
/
index.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="display: flex">
<canvas
id="myCanvas"
width="800"
height="600"
style="border: 1px solid black"
></canvas>
<div style="margin: 24px">
<div style="display: flex">
<button
id="startButton"
style="width: 100%; height: 40px; background-color: azure"
>
Start
</button>
<button
id="restartButton"
style="width: 100%; height: 40px; background-color: cornflowerblue"
onclick="window.location.reload()"
>
Restart
</button>
</div>
<!-- <div style="font-size: 32px; color: slategrey">
畫線倒數計時:
<span id="linetimer" style="margin-left: 8px">2</span>
</div> -->
<div style="font-size: 32px; color: hotpink">
遊戲倒數計時:
<span id="gametimer" style="margin-left: 8px">10</span>
</div>
<div id="result"></div>
</div>
</div>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var isLose = false;
var isDrawing = false;
var points = [];
var isStop = false;
var dogInfo = {
x: Math.round(Math.random() * canvas.width),
y: Math.round(Math.random() * canvas.height),
radius: 150,
};
var honeycombInfo = {
x: 5,
y: 5,
radius: 60,
};
var interval;
var gameInterval;
// create dog
var imgDog = new Image();
imgDog.onload = function () {
ctx.drawImage(
imgDog,
dogInfo.x,
dogInfo.y,
dogInfo.radius,
dogInfo.radius
);
};
imgDog.src = "dog.png";
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fillRect(
imgDog,
dogInfo.x,
dogInfo.y,
dogInfo.radius,
dogInfo.radius
);
var imgHoneycomb = new Image();
imgHoneycomb.onload = function () {
ctx.drawImage(
imgHoneycomb,
honeycombInfo.x,
honeycombInfo.y,
honeycombInfo.radius,
honeycombInfo.radius
);
};
imgHoneycomb.src = "honeycomb.png";
ctx.fillStyle = "rgba(0,0,0,0)";
ctx.fillRect(
imgHoneycomb,
honeycombInfo.x,
honeycombInfo.y,
honeycombInfo.radius,
honeycombInfo.radius
);
// timer
// var linetimer = document.querySelector("#linetimer");
var gametimer = document.querySelector("#gametimer");
var result = document.querySelector("#result");
var number = 2;
var gameNumber = 10;
// detect mouse draw line
canvas.addEventListener("mousedown", function (event) {
points = [];
isStop = false;
isDrawing = true;
points.push({ x: event.clientX, y: event.clientY });
// setTimeout(() => {
// isStop = true;
// }, number * 1000);
// linetimer.innerText = number;
// const timeInterval = setInterval(function () {
// number--;
// if (number <= 0) {
// number = 0;
// clearInterval(timeInterval);
// }
// linetimer.innerText = number + 0;
// }, 1000);
});
canvas.addEventListener("mousemove", function (event) {
if (isDrawing && !isStop) {
points.push({ x: event.clientX, y: event.clientY });
drawLine();
}
});
canvas.addEventListener("mouseup", function (event) {
isDrawing = false;
});
var x = 5;
var y = Math.floor(canvas.height * Math.random());
var dx = 3;
var dy = -3;
var radius = 48;
var x2 = 5;
var y2 = 5;
var dx2 = 2;
var dy2 = 1;
var radius2 = 48;
var x3 = 5;
var y3 = 5;
var dx3 = -1;
var dy3 = 3;
var radius3 = 48;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
var img = new Image();
img.onload = function () {
ctx.drawImage(img, x, y, radius, radius);
ctx.drawImage(img, x2, y2, radius2, radius2);
ctx.drawImage(img, x3, y3, radius3, radius3);
};
img.src = "./bee.png";
ctx.fillStyle = "rgba(0,0,0,0)";
ctx.fillRect(img, x, y, radius, radius);
ctx.fillStyle = "rgba(0,0,0,0)";
ctx.fillRect(img, x2, y2, radius2, radius2);
ctx.fillStyle = "rgba(0,0,0,0)";
ctx.fillRect(img, x3, y3, radius3, radius3);
var imgDog = new Image();
imgDog.onload = function () {
ctx.drawImage(
imgDog,
dogInfo.x,
dogInfo.y,
dogInfo.radius,
dogInfo.radius
);
};
imgDog.src = "dog.png";
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fillRect(
imgDog,
dogInfo.x,
dogInfo.y,
dogInfo.radius,
dogInfo.radius
);
var imgHoneycomb = new Image();
imgHoneycomb.onload = function () {
ctx.drawImage(
imgHoneycomb,
honeycombInfo.x,
honeycombInfo.y,
honeycombInfo.radius,
honeycombInfo.radius
);
};
imgHoneycomb.src = "honeycomb.png";
ctx.fillStyle = "rgba(0,0,0,0)";
ctx.fillRect(
imgHoneycomb,
honeycombInfo.x,
honeycombInfo.y,
honeycombInfo.radius,
honeycombInfo.radius
);
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (var i = 1; i < points.length; i++) {
ctx.lineTo(points[i].x, points[i].y);
}
ctx.stroke();
x += dx;
y += dy;
x2 += dx2;
y2 += dy2;
x3 += dx3;
y3 += dy3;
if (
(x > dogInfo.x &&
x < dogInfo.x + dogInfo.radius &&
y > dogInfo.y &&
y < dogInfo.y + dogInfo.radius) ||
(x2 > dogInfo.x &&
x2 < dogInfo.x + dogInfo.radius &&
y2 > dogInfo.y &&
y2 < dogInfo.y + dogInfo.radius) ||
(x3 > dogInfo.x &&
x3 < dogInfo.x + dogInfo.radius &&
y3 > dogInfo.y &&
y3 < dogInfo.y + dogInfo.radius)
) {
var imgDog = new Image();
imgDog.onload = function () {
ctx.drawImage(
imgDog,
dogInfo.x,
dogInfo.y,
dogInfo.radius,
dogInfo.radius
);
};
imgDog.src = "doghit.png";
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fillRect(
imgDog,
dogInfo.x,
dogInfo.y,
dogInfo.radius,
dogInfo.radius
);
clearInterval(interval);
clearInterval(gameInterval);
result.innerText = "LOSE!";
result.style = "color:red;font-size:64px";
isLose = true;
}
if (x + dx > canvas.width || x + dx < 0) {
dx = -dx;
} else if (checkIsTouched(x + dx, y + dy, points, radius)) {
dx = -(dx + 0.2 * Math.random() * (Math.random() > 0.5 ? 1 : -1));
}
if (y + dy > canvas.height || y + dy < 0) {
dy = -dy;
} else if (checkIsTouched(x + dx, y + dy, points, radius)) {
dy = -(dy + 0.2 * Math.random() * (Math.random() > 0.5 ? 1 : -1));
}
if (x2 + dx2 > canvas.width || x2 + dx2 < 0) {
dx2 = -dx2;
} else if (checkIsTouched(x2 + dx2, y2 + dy2, points, radius2)) {
dx2 = -(dx2 + 0.2 * Math.random() * (Math.random() > 0.5 ? 1 : -1));
}
if (y2 + dy2 > canvas.height || y2 + dy2 < 0) {
dy2 = -dy2;
} else if (checkIsTouched(x2 + dx2, y2 + dy2, points, radius2)) {
dy2 = -(dy2 + 0.2 * Math.random() * (Math.random() > 0.5 ? 1 : -1));
}
if (x3 + dx3 > canvas.width || x3 + dx3 < 0) {
dx3 = -dx3;
} else if (checkIsTouched(x3 + dx3, y3 + dy3, points, radius3)) {
dx3 = -(dx3 + 0.2 * Math.random() * (Math.random() > 0.5 ? 1 : -1));
}
if (y3 + dy3 > canvas.height || y3 + dy3 < 0) {
dy3 = -dy3;
} else if (checkIsTouched(x3 + dx3, y3 + dy3, points, radius3)) {
dy3 = -(dy3 + 0.2 * Math.random() * (Math.random() > 0.5 ? 1 : -1));
}
}
startButton.addEventListener("click", () => {
interval = setInterval(draw, 0.00001);
setTimeout(() => {
if (!isLose) {
result.innerText = "WIN!";
result.style = "color: orange;font-size:64px";
}
clearInterval(interval);
clearInterval(gameInterval);
}, 11000);
gametimer.innerText = gameNumber;
gameInterval = setInterval(function () {
gameNumber--;
if (gameNumber <= 0) {
gameNumber = 0;
clearInterval(gameInterval);
clearInterval(interval);
}
gametimer.innerText = gameNumber + 0;
}, 1000);
});
function drawLine() {
// ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (var i = 1; i < points.length; i++) {
ctx.lineTo(points[i].x, points[i].y);
}
ctx.stroke();
}
function checkIsTouched(x, y, points, radius) {
let isTouched = false;
for (let i = 0; i < points.length; i++) {
const distance = Math.sqrt(
Math.pow(points[i].x - x, 2) + Math.pow(points[i].y - y, 2)
);
if (distance < radius) {
isTouched = true;
break;
}
}
return isTouched;
}
</script>
</body>
</html>