-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
214 lines (190 loc) · 5.43 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta charset="utf-8">
<title>DROWNMAN</title>
<link type="text/css" rel="stylesheet" href="reset.css">
<link type="text/css" rel="stylesheet" href="style.css">
<link rel="shortcut icon" href="images/favicon.png" >
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
</head>
<body>
<div class="menu visible">
<div class="label">NEW GAME</div>
<div class="newGame">
<div class="ownWord">
PLAY MY WORD:
<input type="password" name="">
<a href="">GO</a>
</div>
<div class="loadWord">
CHOOSE A RANDOM WORD FOR ME
<a href="">GO</a>
</div>
</div>
<div class="difficulty">
DIFFICULTY:
<div id='d1'></div>
<div id='d2'></div>
<div id='d3'></div>
</div>
</div>
<div class="letters"></div>
<div class="dashes"></div>
<div class="water">
<div class="waterLevel">
<img src="images/stop.svg">
</div>
</div>
<div class="sand"></div>
<div class="infoBox">
<div><p class="info"></p></div>
</div>
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<script>
jQuery.extend(jQuery.expr[':'], {
focus: "a == document.activeElement"
});
$('.waterLevel img').click(function() {
var wl = $('.waterLevel');
wl.toggleClass('stopFlow');
if(wl.hasClass('stopFlow')) {
$(this).attr('src', 'images/start.svg');
} else {
$(this).attr('src', 'images/stop.svg');
}
});
$('.menu .label').click(function() {
$(this).parent().toggleClass('visible');
})
$('#d1').click(function() {
difficulty = 1;
$('#d2').css('background-color', '#aaa');
$('#d3').css('background-color', '#aaa');
});
$('#d2').click(function() {
difficulty = 2;
$(this).css('background-color', '#F67845');
$('#d3').css('background-color', '#aaa');
});
$('#d3').click(function() {
difficulty = 3;
$('#d2').css('background-color', '#F67845');
$(this).css('background-color', '#D33B33');
});
var initialWaterLevel = $('.waterLevel').height();
var difficulty = 1;
var words = [];
$.get('words.txt', function(data) {
words = data.split(",");
});
resetGame();
$('.menu .ownWord input').focus();
$('.menu .ownWord a').click(function(ev) {
ev.preventDefault();
var inputWord = $('.menu .ownWord input').val().toUpperCase();
resetGame();
playWord(inputWord);
});
$('.menu .loadWord a').click(function(ev) {
ev.preventDefault();
resetGame();
var i = Math.floor(Math.random() * words.length);
playWord(words[i].toUpperCase());
words.splice(i,1);
});
$(document).keypress(function(event) {
if(event.which == 13 && $('.menu .ownWord input').val() != '') {
var inputWord = $('.menu .ownWord input').val().toUpperCase();
resetGame();
playWord(inputWord);
}
});
function playWord(word) {
console.log(word + ' ' + word.length);
var tries = 12 - 2*difficulty;
var waterLevel = initialWaterLevel;
var waterLevelIncrease = ($('.water').height() - initialWaterLevel) / tries;
var over = false;
var lettersLeft = word.length;
var msg = 'YAY, YOU WON<br>Congratulations!';
$('.menu .ownWord input').blur();
$('.menu').removeClass('visible');
(function makeLetters() {
var lettersContent = "";
for (var i = 65; i <= 90; i++) {
lettersContent += '<div>&#' + i + '</div>';
}
$('.letters').html(lettersContent);
})();
(function makeDashes() {
var dashesContent = "";
for (var i = 0; i < word.length; i++) {
dashesContent += '<div></div>';
}
$('.dashes')[0].innerHTML = dashesContent;
})();
$('.letters > div').click(handleClicks);
$(document).keydown(handleKeys);
$('.infoBox').click(function() {
resetGame();
});
function checkLetter(letter) {
var found = false;
for(var i = 0; i < word.length; i++) {
if(word.substr(i,1) === letter) {
$('.dashes > div')[i].innerHTML = letter;
found = true;
lettersLeft--;
}
}
if(!found) {
waterLevel += waterLevelIncrease;
$('.waterLevel').height(waterLevel);
}
}
function handleClicks() {
if(!$(this).hasClass('letterUsed')) {
$(this).addClass('letterUsed');
checkLetter(this.innerHTML);
checkIfOver();
}
}
function handleKeys(event) {
var letterCode = event.which;
if(letterCode >= 65 && letterCode <= 90 && !($('.menu .ownWord input').is(':focus'))) {
var letterDiv = '.letters > div:nth-child(' + (letterCode - 64) +')';
if(!$(letterDiv).hasClass('letterUsed')) {
$(letterDiv).addClass('letterUsed');
checkLetter(String.fromCharCode(letterCode));
checkIfOver();
}
}
}
function checkIfOver() {
if(waterLevel >= $('.water').height()) {
over = true;
msg = 'YOU LOST :(<br>The word was:<br>" ' + word + '"';
}
if(over || lettersLeft == 0) {
$('.letters > div').off('click', handleClicks);
$(document).off('keydown', handleKeys);
$('.info').html(msg);
$('.infoBox').addClass('showMsg');
}
}
}
function resetGame() {
$('.waterLevel').height(initialWaterLevel);
$('.letters').html('');
$('.dashes').html('');
$('.menu .ownWord input').val('');
$('.menu .ownWord input').focus();
$('.letters > div').off('click');
$(document).off('keydown');
$('.infoBox').removeClass('showMsg');
$('.menu').addClass('visible');
}
</script>
</html>