-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgame.js
170 lines (148 loc) · 4.11 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
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
(function() { return "use strict"; })();
( function ($) {
// Prefetch Dom elements & init
var dodgeball = $('#dodgeball'),
startButton = $('.play'),
pauseButton = $('.pause'),
score = $('#current-score'),
gameSummary = $('.game-area .summary'),
summaryScore = $('.game-area .summary .score'),
gameArea = $('.game-area'),
gameAreaHeight = gameArea.height(),
gameAreaWidth = gameArea.width(),
playing = false,
gameOver = false,
gameTime = 120, // 120 seconds <==> 2 minutes
timerDisplay = $('#timer'),
timer, colors = [
'#FF0000', '#0000FF', '#FF0066', '#00CC00', '#800000', '#FF9900', '#660066'
];
// Populate the timer box with the game running time
new Countdown(gameTime, $('#timer')).render();
// Disables/Enables a button
var toggleButton = function(button) {
if (button.attr('disabled')) {
button.removeAttr('disabled');
} else {
button.attr('disabled', 'disabled');
}
}
// Handles Enabling / Disabling play/pause guttons
function toggleButtons() {
toggleButton(startButton);
toggleButton(pauseButton);
}
// Reset game area for new game
function resetGame() {
gameArea.removeClass('game-over');
dodgeball.css('background-color', '#0000FF');
gameSummary.hide();
}
// End current game & show stats
function finishGame() {
playing = false;
gameOver = true;
toggleButtons();
gameArea.addClass('game-over');
summaryScore.html(sum);
sum = 0; // Reset score for a new game
gameSummary.show();
dodgeball.hide();
}
// New game session
function newGame() {
timer = new Countdown(gameTime, timerDisplay);
timer.fire();
playing = true;
score.html(sum); // Update the score label
toggleButtons();
dodgeball.show();
animateDiv();
}
// Resume Existing
function resumeGame() {
playing = true;
timer.fire();
animateDiv();
toggleButtons();
}
// Pause game
function pauseGame() {
toggleButtons();
clearInterval(timer.ticker);
playing = false;
}
// Converts RGB color to HEX equivalent
function toHex(colorval) {
var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); // Match the format rgb(0, 0, 255)
delete(parts[0]); // remove the first element which matches the entire string
for (var i = 1; i <= 3; ++i) {
parts[i] = parseInt(parts[i]).toString(16);
if (parts[i].length == 1) parts[i] = '0' + parts[i];
}
return '#' + parts.join('');
}
// Randomly pick new background color for the dodgeball
function nextColor(currentColor) {
var i = Math.floor(Math.random() * colors.length);
while(colors[i] == currentColor.toUpperCase()) { // ensure the current color is not reselected
i = Math.floor(Math.random() * colors.length);
}
return colors[i];
}
// Make dodgeball flicker
function colorize() {
var currentColor = toHex(dodgeball.css('background-color'));
dodgeball.css('background-color', nextColor(currentColor));
}
// Start the game. This action handles either creating a brand new game
// ,resuming a paused game
// or starting a whole new game if a previous game has ended
startButton.click(function() {
if (gameOver) {
resetGame();
newGame()
} else {
if (!playing) {
if (!timer) {
newGame();
} else {
resumeGame();
}
}
}
});
// Handle pause click
pauseButton.click(function() {
pauseGame(); // obviously
});
var sum = 0;
dodgeball.click(function() {
if (playing) {
colorize();
sum += 100;
score.html(sum);
}
});
// This function returns an array of length 2 that contains a random point in my game-area div
function makeNewPosition() {
var h = gameAreaHeight - 50;
var w = gameAreaWidth - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
}
// Creating the animate function to animate the div
function animateDiv(){
var newq = makeNewPosition();
dodgeball.animate({ top: newq[0], left: newq[1] }, 1000, function(){
if (timer.active) {
if (playing) {
animateDiv();
}
} else {
finishGame();
}
});
}
} )(jQuery);