-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
227 lines (215 loc) · 5.85 KB
/
script.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
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
$(document).ready(function () {
$('#strt').click(function () {start();});
$('#rstrt').click(function () {location.reload();});
var word;
var wrong;
var guess;
var right;
var stopped = false;
function start() {
word = null;
wrong = 0;
guess = ['a', 'e', 'i', 'o', 'u'];
right = 0;
stopped = false;
$('center').each(function () {
$(this).css('display', 'none');
});
$('#game-c').css('display', 'block');
words = []
c = $('select :selected').val();
switch (c) {
case 'badminton':
words = ['lindan', 'chongwei', 'petergade', 'racket', 'shuttle'];
break;
case 'google':
words = ['larry page', 'android', 'colourful', 'cheerful', 'rainbow'];
break;
case 'languages':
words = ['malay', 'english', 'tamil', 'korean', 'chinese'];
break;
}
n = Math.floor(Math.random() * 5);
word = words[n];
draw(0, null);
$('#game-i').focus();
$('#game-i').blur(function () {$('#game-i').focus();});
$('#game-i').keyup(function (e) {
if (!stopped) {
l = String.fromCharCode(e.which);
if ($.inArray(l.toLowerCase(), guess) == -1) {
guess.push(l);
draw(1, l.toLowerCase());
}else {
draw(2, l.toUpperCase());
}
}
});
}
function new_msg(board, msg) {
board.beginPath();
board.fillStyle = 'black';
board.fillRect(0,0,600,74.5);
board.closePath();
board.font = "40px Arial";
board.textAlign = "center";
board.fillStyle = 'white';
board.fillText(msg, 300, 50);
console.log(msg);
}
function draw(step, letter) {
canvas = document.getElementById('game');
board = canvas.getContext("2d");
l = word.length;
s = 600 / (l*2+1);
switch (step) {
case 0:
board.fillStyle = 'black';
board.fillRect(0,0,600,400);
t = 1;
i = 0;
v = 0;
while (l > 0) {
l--;
board.beginPath();
board.moveTo(s*t,350);
t++;
board.lineTo(s*t, 350);
t++;
board.strokeStyle = 'white';
board.stroke();
board.closePath();
p = word.split('')[i];
if ($.inArray(p, ['a','e','i','o','u']) != -1) {
board.font = "40px Arial";
board.textAlign = "center";
//x = s*(t-2);
x = 0.5*((s*(t-2)) + (s*(t-1)));
board.fillStyle = 'white';
board.fillText(p.toUpperCase(),x,325,s);
v++;
}
i++;
}
right += v;
board.beginPath();
board.moveTo(50.5,250.5);
board.lineTo(200.5,250.5);
board.lineWidth = '5';
board.stroke();
board.closePath();
board.beginPath();
board.moveTo(125.5,75.5);
board.lineTo(125.5,250.5);
board.stroke();
board.closePath();
board.beginPath();
board.moveTo(125.5,75.5);
board.lineTo(250.5,75.5);
board.stroke();
board.closePath();
msg = 'Type a Letter To Try It!';
board.fillText(msg, 300, 50);
break;
case 1:
a = word.split('');
c = 0;
pos = -1;
a.forEach(function(e) {
if (e == letter) {
c++;
pos = a.indexOf(e, pos+1);
board.font = "40px Arial";
board.textAlign = "center";
x = 0.5*((s*((pos*2)+1)) + (s*((pos*2)+2)));
board.fillStyle = 'white';
board.fillText(e.toUpperCase(),x,325,s);
}
});
right += c;
console.log(c);
if (right == l) {
stopped = true;
board.beginPath();
board.fillStyle = 'black';
board.fillRect(0,0,600,400);
board.closePath();
board.font = "40px Arial";
board.textAlign = 'center';
board.fillStyle = 'green';
board.fillText('You won! The word is ' + word, 300, 200);
}else if (c > 0) {
new_msg(board, letter.toUpperCase() + ' is in the word!');
}else {
wrong++;
new_msg(board, letter.toUpperCase() + ' is wrong! ' + wrong + ' wrong guesses!');
hang(board, wrong);
}
if (wrong >= 5) {
stopped = true;
board.beginPath();
board.fillStyle = 'black';
board.fillRect(0,0,600,400);
board.closePath();
board.font = "40px Arial";
board.textAlign = 'center';
board.fillStyle = 'red';
board.fillText('You lost! The word was ' + word, 300, 200);
}
break;
case 2:
new_msg(board, 'You Already Guessed ' + letter);
break;
}
}
function hang(b, w) {
switch (w) {
case 1:
//250.5,75.5
b.beginPath();
b.lineWidth = '5';
b.strokeStyle = 'white';
b.arc(250.5, 105.5, 30, 0, 2*Math.PI);
b.stroke();
b.closePath();
break;
case 2:
b.beginPath();
b.lineWidth = '5';
b.strokeStyle = 'white';
b.moveTo(250.5, 135.5);
b.lineTo(250.5, 230.5);
b.stroke();
b.closePath();
break;
case 3:
b.beginPath();
b.lineWidth = '5';
b.strokeStyle = 'white';
b.moveTo(250.5, 165.5);
b.lineTo(200.5, 140.5);
b.stroke();
b.closePath();
b.beginPath();
b.moveTo(250.5, 165.5);
b.lineTo(300.5, 140.5);
b.stroke();
b.closePath();
break;
case 4:
b.beginPath();
b.lineWidth = '5';
b.strokeStyle = 'white';
b.moveTo(250.5, 230.5);
b.lineTo(220.5, 260.5);
b.stroke();
b.closePath();
b.beginPath();
b.moveTo(250.5, 230.5);
b.lineTo(280.5, 260.5);
b.stroke();
b.closePath();
break;
}
}
});