forked from swapnilsparsh/30DaysOfJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (55 loc) · 2.19 KB
/
Copy pathscript.js
File metadata and controls
69 lines (55 loc) · 2.19 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
let ballPosition = 0;
let gameOver = false;
function shuffle() {
const message = document.getElementById('message');
message.textContent = '';
document.getElementById('playAgain').style.display = 'none';
const ball = document.getElementById('ball');
initialballPosition = Math.floor(Math.random() * 3) + 1;
const initialBowl = document.getElementById(`bowl${initialballPosition}`);
const initialBowlRect = initialBowl.getBoundingClientRect();
ball.style.top = `${initialBowlRect.top - 40}px`;
ball.style.left = `${initialBowlRect.left + (initialBowlRect.width / 2) - 15}px`;
ball.style.display = 'block';
ballPosition = Math.floor(Math.random() * 3)+1;
setTimeout(() => {
ball.style.top = `${initialBowlRect.top + 20}px`;
ball.style.display = 'none';
const bowls = [1, 2, 3];
for (let i = 0; i < 3; i++) {
const bowl = document.getElementById(`bowl${bowls[i]}`);
bowl.style.transform = 'translateY(-20px)';
}
setTimeout(() => {
for (let i = 0; i < 3; i++) {
const bowl = document.getElementById(`bowl${bowls[i]}`);
bowl.style.transform = 'translateY(0)';
}
gameOver = false;
document.getElementById('bowl1').disabled = false;
document.getElementById('bowl2').disabled = false;
document.getElementById('bowl3').disabled = false;
}, 1000);
}, 2000);
}
function checkBowl(bowlNumber) {
const message = document.getElementById('message');
if (gameOver) {
return;
}
if (bowlNumber === ballPosition) {
message.textContent = 'You found the ball! :)';
} else {
message.textContent = `Try again! :( The ball was under bowl ${ballPosition}. Please click the shuffle or play again button to play again.`;
}
gameOver = true;
document.getElementById('bowl1').disabled = true;
document.getElementById('bowl2').disabled = true;
document.getElementById('bowl3').disabled = true;
document.getElementById('playAgain').style.display = 'block';
}
function playAgain() {
gameOver = false;
shuffle();
}
shuffle();