-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
80 lines (67 loc) · 2.26 KB
/
Copy pathscripts.js
File metadata and controls
80 lines (67 loc) · 2.26 KB
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
const basket = document.querySelector('.basket');
const fallingObject = document.querySelector('.falling-object');
const scoreElement = document.getElementById('score');
let basketPosition = 160; // Initial position of basket
let fallingObjectPosition = { top: 0, left: 185 };
let score = 0;
let isGameOver = false;
let basketSpeed = 10; // Speed of basket movement
// To track whether the left or right arrow is held down
let isMovingLeft = false;
let isMovingRight = false;
document.addEventListener('keydown', handleKeyDown);
document.addEventListener('keyup', handleKeyUp);
function handleKeyDown(event) {
if (isGameOver) return;
if (event.key === 'ArrowLeft') {
isMovingLeft = true;
} else if (event.key === 'ArrowRight') {
isMovingRight = true;
}
}
function handleKeyUp(event) {
if (event.key === 'ArrowLeft') {
isMovingLeft = false;
} else if (event.key === 'ArrowRight') {
isMovingRight = false;
}
}
function moveBasket() {
if (isMovingLeft && basketPosition > 0) {
basketPosition -= basketSpeed;
basket.style.left = `${basketPosition}px`;
}
if (isMovingRight && basketPosition < 320) {
basketPosition += basketSpeed;
basket.style.left = `${basketPosition}px`;
}
}
function dropObject() {
if (isGameOver) return;
fallingObjectPosition.top += 5;
fallingObject.style.top = `${fallingObjectPosition.top}px`;
if (fallingObjectPosition.top > 580) {
if (fallingObjectPosition.left > basketPosition && fallingObjectPosition.left < basketPosition + 80) {
score++;
scoreElement.textContent = score;
} else {
alert('Game Over! Final Score: ' + score);
isGameOver = true;
return;
}
resetObject();
}
requestAnimationFrame(dropObject);
}
function resetObject() {
fallingObjectPosition.top = 0;
fallingObjectPosition.left = Math.floor(Math.random() * 370);
fallingObject.style.left = `${fallingObjectPosition.left}px`;
}
// Start the game loop for object drop and basket movement
function gameLoop() {
moveBasket();
requestAnimationFrame(gameLoop);
}
dropObject();
gameLoop();