-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgame.js
58 lines (50 loc) · 1.62 KB
/
game.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
var $elements = {
game_over: document.getElementById('game-over'),
you_won: document.getElementById('you-won'),
instructions: document.getElementById('instructions')
};
var canvas = SVG('canvas').size(window.innerWidth-40, window.innerHeight-document.getElementById('canvas').offsetTop-20);
var board = new Board(canvas);
var stopwatch = new StopWatch({delay: 5, timer: $elements.instructions});
var level = 4;
var MAX_LEVEL = 9;
var scoreboard = new Scoreboard({
score: document.getElementById('score-container'),
best: document.getElementById('best-container')
})
board.on('draw', function() {
stopwatch.reset();
stopwatch.start();
});
board.on('start', function() {
if (level == 4) {
document.getElementById('scoreboard').classList.add("slide-in");
$elements.instructions.classList.add("game-started");
}
});
board.on('done', function() {
stopwatch.stop();
scoreboard.add(Math.round(20000/stopwatch.elapsed()*level*level));
document.getElementById("level"+level).classList.add("done");
if (level == MAX_LEVEL) {
$elements.you_won.classList.add("active");
} else {
level += 1;
setTimeout(function() {
board.draw(level);
}, 500);
}
});
board.on('fail', function() {
stopwatch.stop();
$elements.game_over.classList.add("active");
});
board.draw(level);
document.getElementById('start-over').addEventListener('click', function() {
location.reload();
});
document.getElementById('retry').addEventListener('click', function() {
$elements.game_over.classList.remove("active");
board.draw(level);
});
document.body.addEventListener('touchmove', function(e){ e.preventDefault(); });