-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
289 lines (279 loc) · 15.8 KB
/
game.html
File metadata and controls
289 lines (279 loc) · 15.8 KB
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
<html style="background-color: rgb(0, 171, 255);">
<head>
<title>
Fruit punch!
</title>
<!-- // Begin CSS Imports -->
<link rel="stylesheet" href="https://bootswatch.com/3/cyborg/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="css/game.css" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.8/sweetalert2.min.css">
<!-- // End CSS Imports -->
</head>
<body style="background-color: transparent" id="tehbody">
<div class="loading">
<center>
<br>
<br>
<br>
<br>
<br>
<br>
<img src="/img/blue_room_logo.png"><br>
<img src="/img/loader2.gif">
<br>
<h3>
Designed at
</h3>
<h3 style="font-family: Consolas, monaco, monospace; color: #000000; background-color: #ffff00; display: inline">coding(brum);</h3>
<h3>
By:<br>
Jay Williams<br>
Nikash Chekuri<br>
Gokul Menon<br>
Rory Sharp<br>
Shaquille<br>
</center>
</div>
<!-- HACK: Preload images
TODO: Theres got to be a better solution to the display flashing than this -_-
-->
<img src="lemon1.jpg" style="display:none;" alt="" />
<img src="lemon2.jpg" style="display:none;" alt="" />
<img src="lemon3.jpg" style="display:none;" alt="" />
<img src="lemon4.jpg" style="display:none;" alt="" />
<img src="banana1.jpg" style="display:none;" alt="" />
<img src="banana2.jpg" style="display:none;" alt="" />
<img src="banana3.jpg" style="display:none;" alt="" />
<img src="banana4.jpg" style="display:none;" alt="" />
<img src="lime1.jpg" style="display:none;" alt="" />
<img src="lime2.jpg" style="display:none;" alt="" />
<img src="lime3.jpg" style="display:none;" alt="" />
<img src="lime4.jpg" style="display:none;" alt="" />
<img src="orange1.jpg" style="display:none;" alt="" />
<img src="orange2.jpg" style="display:none;" alt="" />
<img src="orange3.jpg" style="display:none;" alt="" />
<img src="orange4.jpg" style="display:none;" alt="" />
<div class="gamecomponent" id="dummy" style="display: none;"></div>
<h1 class="gamecomponent" id="scorecount" style="color: black; padding-left: 20px"> </h1> <!--// Create the score count display element -->
<center class="gamecomponent">
<h1 class="words" style="font-family: captcha"></h1> <!--
// Create the element to display the fruit you got to hit TODO: Refactor QUESTION: Should we create this in JS? -->
<h1 class="win2" style="display: none"></h1> <!--// Create the element that displays "You got xx points!" and make invis-->
<center class="sboard" style="display: none"> <!--// Create a centered element to hold a -->
<ul class="list-group leaderboard" style="width: 30em"> <!--// List of top scores -->
<!--li class="list-group-item">
Jay Williams - 39
</li-->
</ul>
</center>
</center>
</body>
<!-- // Begin JS imports -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>
<script src="js/nanobar.min.js" crossorigin="anonymous"></script>
<script src="color.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.8/sweetalert2.min.js"></script>
<!-- // End JS imports -->
<script>
var fruits = [
{
name: "Lemon",
pics: ["url('lemon1.jpg')", "url('lemon2.jpg')", "url('lemon3.jpg')", "url('lemon4.jpg')"],
key: "Digit2"
},
{
name: "Banana",
pics: ["url('banana1.jpg')", "url('banana2.jpg')", "url('banana3.jpg')", "url('banana4.jpg')"],
key: "Digit1"
},
{
name: "Apple",
pics: ["url('apple1.jpg')", "url('apple2.jpg')", "url('apple3.jpg')"],
key: "Digit4"
},
{
name: "Orange",
pics: ["url('orange1.jpg')", "url('orange2.jpg')", "url('orange3.jpg')", "url('orange4.jpg')"],
key: "Digit3"
}
]
var options = {
gamelength: 60, // Seconds
loadingscreenlength: 2, // Seconds
time_remaining_bar: true,
loadingscreen: true,
camo: true,
randomMove: true,
demo: false,
leaderboard: true,
sounds: {
success: true,
fail: true
},
lang: {
points: "You got $score points!",
scoreboard_entry: "$name - $score"
}
};
if (options.demo) {
$(".words").css({ "border": "10px solid red"})
}
var soundUrl = "https://freesound.org/people/LittleRainySeasons/sounds/335908/download/335908__littlerainyseasons__correct.mp3";
var badSound = "https://freesound.org/people/RICHERlandTV/sounds/216090/download/216090__richerlandtv__bad-beep-incorrect.mp3";
var out = []; // Initialise array of names of fruits
var mappings = {};
var nanooptions = {
id: 'my-id',
target: document.getElementById('tehbody')
};
function playSound(soundfile) { // This will play an mp3
var snd = new Audio(soundfile);
snd.play()
}
for (var i = 0; i < fruits.length; i++){ // Go throught fruit objects
out.push(fruits[i].name) // add the name of fruit to Array
mappings[fruits[i].key] = fruits[i].name; // Create a mappings object with {KeyB: "Bannana"} etc
}
var current = {};
function getImageFromName(name) {
if (name.startsWith("url")){ // Special Case Code; If name is "url('legendary.png')", ie trump,
return name // Just return the readymade url
}
var out = {}; // Initialise object
for (var i = 0; i < fruits.length; i++){ // Go throught fruit objects
out[fruits[i].name] = choose(fruits[i].pics) // Fill the object with {bannana: "url(randombannanabic)"}
} // TODO: Why is this method so HACK y? It doesn't need to be
return out[name] // Return the choice from the array
}
if (options.loadingscreen) {
setTimeout(function() { // DO NOT LOOK AT THIS CODE
setTimeout(function(){ // THIS IS NOT A FAKE LOADING SCREEN AT ALL
initPic(getImageFromName(choose(out)), choose(out)) // Set a random background and text
}, 100)
if (options.time_remaining_bar){
var nanobar = new Nanobar( nanooptions ); // Create the time bar
var b = 0;
var meee = setInterval(function () { // And increment it
b++
nanobar.go( Math.round( (b / (options.gamelength * 10) ) * 100 ) )
if(b > options.gamelength * 10) { // If the time has elapsed
clearInterval(meee) // DELETE IT
}
}, 100) // And do that every 100 ms
}
$(".loading").css({"display": "none"}) // Remove the *cough cough* loading screen
}, options.loadingscreenlength * 1000) // <- This does not set the time the loading screen displays for
} else {
$(".loading").css({"display": "none"})
initPic(getImageFromName(choose(out)), choose(out)) // Set a random background and text
if (options.time_remaining_bar){
var nanobar = new Nanobar( nanooptions ); // Create the time bar
var b = 0;
var meee = setInterval(function () { // And increment it
b++
nanobar.go( Math.round( (b / 600 ) * 100 ) )
if(b > 600) { // If the time has elapsed
clearInterval(meee) // DELETE IT
}
}, 100) // And do that every 100 ms
}
}
function choose(choices) { // Makes a random choice from Array, eg. [1,2,5] => 1 or 2 or 5
var index = Math.floor(Math.random() * choices.length);
return choices[index];
}
function randomMove(elem){ // Takes a jQuery element like $(".words") and puts it in a random location
var posx = (Math.random() * ($(document).width() - 300)).toFixed(); // Chooses a random x
var posy = (Math.random() * ($(document).height() - 50)).toFixed(); // Chooses a random Y
elem.css({ // Sets the css of the element
'position':'absolute', // To the absolute position of x and y
'left':posx+'px',
'top':posy+'px',
'display':'inline'
})
}
function initPic(pic, word) { // Function that sets the background and word
$("html").css("background-image", pic); // Set page background to pic
$(".words").text(word); // Set "words" element to the name of what you gotta click
if (options.camo) {
setTimeout(function () { // After 70ms, so that the background loads first
getColor(pic.replace("url('", "").replace("')", ""), fruits[2].pics.indexOf(pic) !== -1, function (correctColor) { // Get the average image color
$(".words").css("color", correctColor); // Then set the word's color to it
})
}, 10)
} else {
$(".words").css("color", "black");
}
if(options.randomMove){
randomMove($(".words")) // CHANGED: Now moves text each time
}
current.pic = pic;
current.word = word;
}
setTimeout(function () { // Below code runs after 60 seconds has passed
$("html").css("background-image", ""); // Remove background image
$("html").css("background-color", "black"); // Set the background black
$(".words").remove() // Remove the current word (ie banana)
$(".win2").text(options.lang.points.replace("$score", (correct - incorrect).toString())) // Set winning text
$(".win2").fadeIn() // Fade in the winning text
if(options.leaderboard){
$(".sboard").fadeIn() // Fade in the scoreboard
$.get("/get-score/", function (d) { // Download the updated scoreboard
var arr = JSON.parse(d); // Parse what we've been given
for (var i = 0; i < arr.length ; i++) { // Go through all the top 20 scores
$(".leaderboard").append("<li class='list-group-item'> " + options.lang.scoreboard_entry.replace("$name", arr[i].name).replace("$score", arr[i].score) + " </li>") // And add them to the leaderboard
}
})
swal({ // Create a dialog box:
title: 'What\'s your name?',
input: 'text', // With an input box
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
allowOutsideClick: false
}).then(function (name) { // Then when user clicks submit
$.post("/add-score/", {name: name, score: (correct - incorrect)}, function (data) { // Then when that data is successfully sent
console.info(data);
setTimeout(function () {
$.get("/get-score/", function (d) { // Download the updated scoreboard
var arr = JSON.parse(d); // Parse what we've been given
$(".leaderboard").html("") // Remove old scores
for (var i = 0; i < arr.length ; i++) { // Go through all the top 20 scores
$(".leaderboard").append("<li class='list-group-item'> " + arr[i].name + " - " + arr[i].score + " </li>") // And add them to the leaderboard
}
})
}, 500)
})
});
};
document.onkeyup = function () {}; // Finally, set the "onkeyup" function to empty so that users can't continue playing
}, options.loadingscreen ? (options.gamelength * 1000) + (options.loadingscreenlength * 1000) : options.gamelength * 1000)
var correct = 0; // Start at 0 wins
var incorrect = 0
document.onkeyup = function (e) { // When someone lets go of a key
var mapped = mappings[e.code] // Get the key from "mappings", which was defined earlier
if(mapped == undefined) return; // If the key isn't registered, forget about it
if (mapped == current.word) { // If the key is the correct one TODO: Count incorrect keys
var key = choose(out) // Then pick two random fruits
var key2 = choose(out) // ^
correct++; // Add 1 to the win counter
$("#scorecount").text(correct - incorrect)
if(options.sounds.success){
playSound(soundUrl);
}
if ((correct - incorrect) >= 40 && (correct - incorrect) <= 42) {
initPic("url('legendary.png')", key)
} else {
initPic(getImageFromName(key2), key) // And set the background and text to those random fruits
}
} else {
if(options.sounds.fail){
playSound(badSound);
}
incorrect = incorrect + 1; // If the fruit is wrong, add one to the incorrect score
$("#scorecount").text(correct - incorrect) // And update visual counter
}
}
</script>
</html>