-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
66 lines (51 loc) · 1.54 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
/*jslint browser: true, devel: true, closure: true */
var gameModule = (function () {
"use strict";
var counter = 0,
ballX,
ballY,
ballR,
colors = ['#ff0000', '#0000ff', 'green'],
scores = 0;
function touchEvent(evt) {
var x = evt.clientX,
y = evt.clientY,
tmp = (ballX - x) * (ballX - x) + (ballY - y) * (ballY - y);
console.log('Clicked ' + x + ", " + y);
if (tmp < ballR * ballR) {
scores += 10;
console.log("Hit ! Good. Scores: " + scores);
}
}
function gameOver() {
console.log('Game Over!');
}
function startGame() {
var canvas = document.getElementById('game'),
ctx = canvas.getContext('2d');
ballX = Math.floor(Math.random() * 500);
ballY = Math.floor(Math.random() * 300);
ballR = Math.floor(Math.random() * 150);
canvas.width = 640;
canvas.height = 360;
ctx.fillStyle = colors[counter % colors.length];
ctx.beginPath();
ctx.arc(ballX, ballY, ballR, 0, Math.PI * 2, true);
ctx.fill();
if (counter >= 9) {
gameOver();
} else {
counter = counter + 1;
console.log('Counter: ' + counter);
setTimeout(startGame, 1000);
}
}
function start() {
document.getElementById('main').addEventListener('click', touchEvent, false);
startGame();
}
return {
start: start
};
}(document));
gameModule.start();