-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
323 lines (249 loc) · 5.3 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<html>
<head>
<title>
Dry as a Bone
</title>
<style>
body {
background-color: gray;
}
.headline {
color: blue;
text-align: center;
font-size: 80px;
font-family: impact;
background-image: url("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQ_kZYkFzHhQC3WqaD7UGibq-FHL_SVGpmvXP-yzAUE_IUTQ30b");
margin-top: 0px;
}
#game {
border: solid;
margin-top: 100px;
margin: auto;
}
</style>
</head>
<body>
<h1 class="headline">
Dry as a Bone
</h1>
<canvas id='game' width='1000' height='700'>
</canvas>
</body>
<script>
alert("Ready to begin");
var canvas = document.getElementById('game');
var ctx = canvas.getContext('2d');
var WIDTH = 1000;
var HEIGHT = 700;
var speedRect = 200;
var x = 0;
var y = 680;
var xPos = [0];
var yPos = [0];
var speed = [1];
var rainDrops = 10;
var rectWidth = 20;
var rectHeight = 20;
var lives = 5;
var winX = WIDTH/2;
var winY = HEIGHT/2;
var winWidth = 40;
var winLength = 40;
var safeX = 0;
var safeY = HEIGHT-30;
var safeWidth = 30;
var safeHeight = 30;
var level = 0;
var secs = 0;
//Timer
window.onload = function() {
var id = setInterval(function(){
secs++;
if(level === 11 || lives === 0)
clearTimeout(id);
}, 1000);
};
// Handle keyboard controls
var keysDown = {};
addEventListener("keydown", function (e) {
keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function (e) {
delete keysDown[e.keyCode];
}, false);
// Update game objects
var update = function (modifier) {
if (38 in keysDown) { // Player holding up
y -= speedRect * modifier;
}
if (40 in keysDown) { // Player holding down
y += speedRect * modifier;
}
if (37 in keysDown) { // Player holding left
x -= speedRect * modifier;
}
if (39 in keysDown) { // Player holding right
x += speedRect * modifier;
}
};
//Lock Screen
window.addEventListener("keydown", function(e) {
// Lock arrow keys
if( [37, 38, 39, 40,].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
}, false);
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
// Level Creation
function reset () {
x = 0;
y = HEIGHT-20;
winX = (Math.random() * WIDTH)-40 ;
winY = (Math.random() * HEIGHT)-40;
if(level === 0) {
winX=WIDTH/2;
winY=HEIGHT/2;
} else if (winX > WIDTH || winX < 0 || winY > HEIGHT || winY < 0) {
winX=WIDTH/2;
winY=HEIGHT/2;
}
level++;
rainDrops+=10;
};
//Safe zone
function safeHouse () {
ctx.beginPath();
ctx.rect(safeX, safeY, safeWidth, safeHeight);
ctx.fillStyle = 'blue';
ctx.fill();
ctx.closePath();
};
// End zone
function winRect () {
ctx.beginPath();
ctx.rect(winX, winY, winWidth, winLength);
ctx.fillStyle = 'green';
ctx.fill();
ctx.closePath();
};
//Player
function rectangle () {
ctx.beginPath();
ctx.rect(x, y, rectWidth, rectHeight);
ctx.fillStyle = 'red';
ctx.fill();
ctx.closePath();
}
function rainFall () {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
//background
ctx.beginPath();
ctx.rect(0,0, WIDTH, HEIGHT);
ctx.fillStyle = 'black';
ctx.fill();
ctx.closePath();
for (var i = 0; i < rainDrops; i++) {
xPos.push(Math.random()* WIDTH);
yPos.push(0);
speed.push(Math.random() * 5);
}
for (var i = 0; i <rainDrops; i++) {
//Rain
ctx.beginPath();
ctx.arc(xPos[i], yPos[i], 3, 0, 2 * Math.PI);
ctx.fillStyle = 'blue';
ctx.fill();
ctx.closePath();
//Rain movement
yPos[i]+=speed[i];
//Rain Loop
if (yPos[i] > HEIGHT) {
yPos[i]= 0;
yPos[i]+=speed[0];
}
//Sets Boundaries
if (x < 0){
x = 0;
}
if (x > WIDTH-20){
x = WIDTH-20;
}
if (y > HEIGHT-20){
y = HEIGHT-20;
}
if(y < 0) {
y = 0;
}
//Collision detection
if (xPos[i]>=x && xPos[i] <= x +rectWidth && yPos[i] >= y && yPos[i] <= y + rectHeight){
x = 0;
y = HEIGHT-20;
lives -=1;
wait(1000);
}
//Game Beaten
if (level === 11) {
yPos[i] = 0;
speed[i] = 0;
ctx.beginPath();
ctx.font = "150px Arial";
ctx.fillText("YOU DID IT!!",50, HEIGHT/2);
ctx.fill();
ctx.closePath();
//End-zone Collision
}else if (x + rectWidth > winX && x + rectWidth < winX + winWidth + rectWidth && y + rectHeight > winY && y + rectHeight < winY + winLength + rectHeight){
reset();
wait(1000);
}
//Game Over Display
if (lives === 0){
ctx.beginPath();
ctx.font = "150px Arial";
ctx.fillText("Game Over!!!",50, HEIGHT/2);
ctx.fillStyle = 'red';
ctx.fill();
ctx.closePath();
yPos[i] = 0;
speed[i] = 0;
}
//Winning Display
if (xPos[i] >=safeX && xPos[i] <= safeX + safeWidth && yPos[i] >= safeY && yPos[i] <= safeY +safeHeight){
yPos[i] = 0;
yPos[i] += speed[0];
}
}
//Screen Lives and level display
ctx.beginPath();
ctx.fillStyle = 'green';
ctx.font = '30px Arial';
ctx.fillText("You have " + lives + " lives", 10, 30);
ctx.fillText("Level " + level, 850, 30);
ctx.fillText("Time: " + secs, 450, 30);
ctx.fill();
ctx.closePath();
//Draws the safehouse, player, and endzone
safeHouse();
rectangle();
winRect();
};
//Runs the main function of rain falling and player movement
var main = function () {
var now = Date.now();
var delta = now - then;
update(delta / 1000);
rainFall();
then = now;
// Request to do this again ASAP
requestAnimationFrame(main);
};
var then = Date.now();
reset();
main();
</script>
</html>