-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
75 lines (64 loc) · 2.51 KB
/
game.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
const character = document.getElementById('character');
const collectible = document.getElementById('collectible');
const container = document.getElementById('gameContainer');
const scoreDisplay = document.getElementById('score');
const restartButton = document.getElementById('restartButton');
let score = 0;
const step = 10; // Number of pixels the character moves per step
const containerBounds = container.getBoundingClientRect();
document.addEventListener('keydown', (event) => {
let characterBounds = character.getBoundingClientRect();
switch (event.key) {
case 'ArrowUp':
if (characterBounds.top > containerBounds.top) {
character.style.top = `${character.offsetTop - step}px`;
}
break;
case 'ArrowDown':
if (characterBounds.bottom < containerBounds.bottom) {
character.style.top = `${character.offsetTop + step}px`;
}
break;
case 'ArrowLeft':
if (characterBounds.left > containerBounds.left) {
character.style.left = `${character.offsetLeft - step}px`;
}
break;
case 'ArrowRight':
if (characterBounds.right < containerBounds.right) {
character.style.left = `${character.offsetLeft + step}px`;
}
break;
}
checkCollision();
});
function checkCollision() {
const characterBounds = character.getBoundingClientRect();
const collectibleBounds = collectible.getBoundingClientRect();
if (
characterBounds.left < collectibleBounds.right &&
characterBounds.right > collectibleBounds.left &&
characterBounds.top < collectibleBounds.bottom &&
characterBounds.bottom > collectibleBounds.top
) {
score += 1;
scoreDisplay.textContent = `Score: ${score}`;
moveCollectible();
}
}
function moveCollectible() {
const maxLeft = container.clientWidth - collectible.clientWidth;
const maxTop = container.clientHeight - collectible.clientHeight;
const randomLeft = Math.floor(Math.random() * maxLeft);
const randomTop = Math.floor(Math.random() * maxTop);
collectible.style.left = `${randomLeft}px`;
collectible.style.top = `${randomTop}px`;
}
restartButton.addEventListener('click', () => {
character.style.top = '225px';
character.style.left = '225px';
score = 0;
scoreDisplay.textContent = `Score: ${score}`;
moveCollectible();
});
moveCollectible(); // Initialize the position of the collectible