-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
352 lines (298 loc) · 10.4 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<!-- Für Mobilgeräte initialen Zoom abschalten -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Angaben für Suchmaschinen -->
<!-- Beschreibung -->
<meta name="description" content="Das lustige Spiel direkt im Browser">
<!-- Stichwörter -->
<meta name="keywords" content="SantaJump, HTML">
<title>
Niklas' Santa Jump
</title>
<link id="css" rel="stylesheet" type="text/css" href="santaStyle.css">
<script type="text/javascript" src="shake.js"></script>
</head>
<body>
<div id="game">
<h1 id="score-text">Score: <span id="score">0</span></h1>
<img src= images/santa_freigestellt.png id="santa"></img>
<div class="pole" id="pole-1"></div>
<div class="pole" id="pole-2"></div>
<div class="pole" id="mid-pole"></div>
<div class="pole" id="large-pole"></div>
<div id="floor"></div>
<div id="width-center-wrapper">
<div id="center-wrapper">
<div id="image-wrapper">
<img src="images/Santa_Jump_Title.png" class="title" id="title-big">
<img src="images/Santa_Jump_Title_small.png" class="title" id="title-small">
</div>
<h3 id="how-to">Click, Tap, Press Space or shake (not available on all devices) to jump</h3>
<button id="restart-btn"></button>
</div>
</div>
</div>
<script type="text/javascript">
"use strict";
//Save DOM objects to constant variables
const santa = document.querySelector('#santa');
const poles = document.querySelectorAll('.pole');
const pole1 = document.querySelector('#pole-1');
const pole2 = document.querySelector('#pole-2');
const game = document.querySelector('#game');
const restartBtn = document.querySelector('#restart-btn');
const howTo = document.querySelector('#how-to');
const scoreText = document.querySelector('#score-text');
const fps = 30;
const fpsInterval = 1000 / fps;
const maxAmnPoles = 3;
const test = document.querySelector('#test');
const floorHeight = 0.15;
const minimalGapBetweenPoles = 400;
const poleHeight = 8 + "%";
const scoreSpan = document.querySelector('#score');
/**The maximum width for a screen to be treated as a
*small screen. To identify small screens, one have to use
* screenWidth <= smallScreenBorder and not '<'.
*/
const smallScreenBorder = 800;
const title = document.querySelector('#image-wrapper');
//make some variables accessible to functions
let score;
let playing;
let speed;
let scoreUpdated;
let jumping;
let placedPoles;
let lastPole;
let gameWidth = game.clientWidth;
let gameHeight = game.clientHeight;
//Place the initial poles and set the flag for the first game
placeInitialPoles();
var firstGame = true;
scoreText.classList.add('hide');
//Initialize shake.js
var myShakeEvent = new Shake({
threshold: 1,
timeout: 200
});
//Variables for fixed fps rate
var now, delta, then;
//Variables for internal fps log
var lastFpsTime, frames;
//Initialize, what method should be called, when shake was noticed.
window.addEventListener('shake', shakeOccured, false);
//Restarts the game, initializes all necessary variables
function restart() {
//Remove 'restart'-button from UI.
restartBtn.removeEventListener('click', restart);
restartBtn.classList.toggle('hide');
title.classList.add('hide');
santa.classList.add('.running');
howTo.classList.add('hide');
scoreText.classList.remove('hide');
speed = 15;
score = 0;
scoreSpan.textContent = score;
scoreUpdated = false;
myShakeEvent.start();
then = Date.now();
frames = 0;
lastFpsTime = Date.now();
playing = true;
//If this is not the first game, replace the initial poles...
if (!firstGame) {
placeInitialPoles();
}
firstGame = false;
gameLoop();
}
//Updates the displayed data on screen
function update() {
//Update screensize if necessary
gameWidth = game.clientWidth;
gameHeight = game.clientHeight;
//Check each pole:
poles.forEach((pole) => {
let polesCurrentPos = parseFloat(window.getComputedStyle(pole).getPropertyValue("right"));
//Update score
if (polesCurrentPos > gameWidth * 0.8) {
if (!scoreUpdated) {
score += 1;
scoreUpdated = true;
}
}
//Check whether a pole went outside of the game area
if (polesCurrentPos > gameWidth) {
//window.console.log(polesCurrentPos);
//Set it back to the start and hide the pole
pole.removeAttribute('placed');
pole.removeAttribute('counted');
pole.classList.toggle('hide');
polesCurrentPos = 0;
pole.style.right = 0;
placedPoles -= 1;
//Update speed
speed += 0.125;
scoreUpdated = false;
}
//Move pole
if (pole.getAttribute('placed') == 'true') {
pole.style.right = (polesCurrentPos / gameWidth) * 100 + (speed / 16) + "%";
}
});
//Increase the last pole counter
lastPole += speed;
//Try to place a new pole
if (placedPoles < maxAmnPoles) {
placedPoles += 1;
setTimeout(function () {
placePole();
}, (Math.random() * 1000));
}
//check for collisions
checkForCollisions();
scoreSpan.textContent = score;
}
/**
* Places the initial poles (two small ones) on the field and sets the other
* ones as 'hide'.
*/
function placeInitialPoles() {
//Hide all poles
poles.forEach((pole => {
pole.classList.add('hide');
pole.style.right = 0 + "%";
pole.removeAttribute('placed');
pole.removeAttribute('counted');
}));
placedPoles = 0;
//Set initial poles
do {
var firstPolePos = Math.random() * 60;
var secondPolePos = Math.random() * 60;
} while (Math.abs(firstPolePos - secondPolePos) < 20);
pole1.style.right = firstPolePos + "%";
pole2.style.right = secondPolePos + "%";
pole1.setAttribute('placed', 'true');
placedPoles += 1;
pole1.classList.toggle('hide');
pole2.setAttribute('placed', 'true');
placedPoles += 1;
pole2.classList.toggle('hide');
lastPole = Math.min(firstPolePos, secondPolePos);
}
function placePole() {
if (lastPole > minimalGapBetweenPoles) {
var idx;
//Find a pole which is currently not placed
do {
idx = parseInt(Math.random() * poles.length);
} while (poles[idx].getAttribute('placed') == 'true');
poles[idx].setAttribute('placed', 'true');
poles[idx].classList.toggle('hide');
lastPole = 0;
} else {
setTimeout(function () {
placePole();
}, (Math.random() * 1000));
}
}
function checkForCollisions() {
let santaRight = santa.getBoundingClientRect().right;
let santaLeft = santaRight - 20;
let santaBottom = santa.getBoundingClientRect().bottom;
poles.forEach((pole) => {
if (pole.getAttribute('placed') == "true") {
let poleRight = pole.getBoundingClientRect().right;
let poleLeft = poleRight - pole.getBoundingClientRect().width;
let poleTop = pole.getBoundingClientRect().top;
if (santaRight >= poleLeft && santaLeft < poleRight && santaBottom >= poleTop) {
gameOver();
}
}
});
}
/**
* Method lets Santa perform a jump, is he is currently walking on the ground. If Santa is already
* performing a jump and is not touching the ground, no jump will be performed.
*/
function jump() {
santa.classList.add("jump");
//Default timeoutTime is 500
var timeoutTime = 500;
//For small screens timeoutTime is 700
if (gameWidth < smallScreenBorder) {
timeoutTime = 700;
}
setTimeout(function() {
santa.classList.remove("jump");
}, timeoutTime);
}
function gameOver() {
santa.classList.remove('.running');
myShakeEvent.stop();
window.console.log("game over");
playing = false;
restartBtn.classList.toggle('hide');
restartBtn.addEventListener('click', restart);
title.classList.remove('hide');
howTo.classList.remove('hide');
scoreText.classList.add('hide');
}
/**
* Method executes a game loop. Therefor it calculates and updates all necessary values and
* requests a new animation frame for the game. Method also ensures, that the framerate doesn't
* exceed the maximum framerate [fps].
*/
function gameLoop() {
if (playing) {
requestAnimationFrame(gameLoop);
now = Date.now();
delta = now - then;
//Wenn letztes Update lang genug her ist
if (delta > fpsInterval) {
frames += 1;
update();
then = now - (delta % fpsInterval);
}
if ((now - lastFpsTime) > 1000) {
window.console.log("fps: " + frames + " fps");
lastFpsTime = now;
frames = 0;
}
}
}
//TODO collision
//Method that will be called, when a shake was noticed.
function shakeOccured() {
if(playing) {
jump();
}
}
//Listen for spacebar presses
document.addEventListener("keydown", function (e) {
var key = e.key;
if (key == " ") {
if (!playing) {
restart();
} else {
jump();
}
}
});
//Listen for mouse clicks
game.addEventListener("mousedown", function (e) {
if (!playing) {
restart();
} else {
jump();
}
});
//restart();
</script>
</body>
</html>