forked from jollen/html5-game-bubble-r2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
86 lines (64 loc) · 1.59 KB
/
main.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
var bubble = (function(document, $) {
var ballX,
ballY,
radi;
var canvasWidth,
canvasHeight;
var timeoutVar,
counter;
var scores;
function drawBall() {
var canvas = document.getElementById('board');
var background = $('#game-screen')[0],
rect = background.getBoundingClientRect();
var ctx = canvas.getContext('2d');
canvas.width = rect.width;
canvas.height = rect.height;
canvasWidth = rect.width;
canvasHeight = rect.height;
ballX = Math.floor(Math.random() * 300);
ballY = Math.floor(Math.random() * 500);
radi = Math.floor(Math.random() * 100) + 30;
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(ballX, ballY, radi, 0, Math.PI * 2, true);
ctx.fill();
if (counter >= 10) {
clearTimeout(timeoutVar);
} else {
counter = counter + 1;
timeoutVar = setTimeout(drawBall, 1000);
}
}
function touchEvent(evt) {
console.log('clicked: ' + evt.clientX + " , " + evt.clientY);
var x1,
x2,
y1,
y2;
// 四個角
x1 = ballX - radi;
x2 = ballX + radi;
y1 = ballY - radi;
y2 = ballY + radi;
// 是否擊中
if ((evt.clientX > x1) && (evt.clientX < x2)) {
if ((evt.clientY > y1) && (evt.clientY < y2)) {
scores = scores + (canvasWidth - radi);
console.log("Hit. Scores: " + scores);
}
}
// prevent default
evt.preventDefault();
}
function start() {
counter = 1;
scores = 0;
document.getElementById("game-screen").
addEventListener("click", touchEvent, false);
timeoutVar = setTimeout(drawBall, 1000);
}
return {
start: start
}
}) (document, Sizzle);