Update index.html
Browse files- index.html +50 -133
index.html
CHANGED
@@ -3,150 +3,67 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>Futuristic
|
7 |
<link rel="stylesheet" href="style.css">
|
8 |
</head>
|
9 |
<body>
|
10 |
<div class="game-container">
|
11 |
-
<div id="ball" class="ball"></div>
|
12 |
-
<div id="platform" class="platform"></div>
|
13 |
<div id="score">Score: 0</div>
|
14 |
<div id="level">Level: 1</div>
|
15 |
-
<div id="lives">
|
16 |
-
|
17 |
-
<img class="life" src="life-icon.png" alt="Life" id="life2">
|
18 |
-
<img class="life" src="life-icon.png" alt="Life" id="life3">
|
19 |
-
<img class="life" src="life-icon.png" alt="Life" id="life4">
|
20 |
-
</div>
|
21 |
</div>
|
22 |
-
|
23 |
<script>
|
24 |
-
let
|
25 |
-
let platform = document.getElementById("platform");
|
26 |
-
let score = document.getElementById("score");
|
27 |
-
let levelDisplay = document.getElementById("level");
|
28 |
-
let livesDisplay = document.getElementById("lives");
|
29 |
-
|
30 |
-
let currentScore = 0;
|
31 |
let level = 1;
|
32 |
-
let
|
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 |
-
isTouching = false;
|
71 |
-
});
|
72 |
-
|
73 |
-
let updateLives = () => {
|
74 |
-
for (let i = 1; i <= 4; i++) {
|
75 |
-
let life = document.getElementById(`life${i}`);
|
76 |
-
if (i > remainingLives) {
|
77 |
-
life.style.visibility = "hidden";
|
78 |
-
} else {
|
79 |
-
life.style.visibility = "visible";
|
80 |
-
}
|
81 |
-
}
|
82 |
-
};
|
83 |
-
|
84 |
-
let updateBallPosition = () => {
|
85 |
-
ballY += ballSpeed;
|
86 |
-
|
87 |
-
// Update ball position
|
88 |
-
ball.style.top = ballY + "px";
|
89 |
-
ball.style.left = ballX + "px";
|
90 |
-
|
91 |
-
let ballRect = ball.getBoundingClientRect();
|
92 |
-
let platformRect = platform.getBoundingClientRect();
|
93 |
-
|
94 |
-
if (ballY + 30 >= platformRect.top && ballY + 30 <= platformRect.bottom) {
|
95 |
-
if (ballX + 30 >= platformRect.left && ballX <= platformRect.right) {
|
96 |
-
// Ball hits the platform
|
97 |
-
currentScore++;
|
98 |
-
if (currentScore % 10 === 0) {
|
99 |
-
level++;
|
100 |
-
levelDisplay.innerHTML = "Level: " + level;
|
101 |
-
fallSpeed -= 0.2;
|
102 |
-
ballSpeed += 0.1; // Make ball fall faster
|
103 |
}
|
104 |
-
score.innerHTML = "Score: " + currentScore;
|
105 |
-
resetBall();
|
106 |
-
}
|
107 |
-
} else if (ballY >= window.innerHeight) {
|
108 |
-
// Ball missed the platform
|
109 |
-
remainingLives--;
|
110 |
-
updateLives();
|
111 |
-
if (remainingLives <= 0) {
|
112 |
-
alert("Game Over! Your score is: " + currentScore);
|
113 |
-
resetGame();
|
114 |
}
|
115 |
-
|
116 |
-
}
|
117 |
-
};
|
118 |
-
|
119 |
-
let resetBall = () => {
|
120 |
-
// Stop the animation when ball is reset
|
121 |
-
ball.style.animation = 'none';
|
122 |
-
setTimeout(() => {
|
123 |
-
ball.style.animation = `fall ${fallSpeed}s linear infinite`;
|
124 |
-
}, 50); // Delay to restart the animation
|
125 |
-
|
126 |
-
ballY = -30; // Reset ball to start position
|
127 |
-
ballX = Math.floor(Math.random() * (window.innerWidth - 30)); // New random X position
|
128 |
-
ball.style.top = ballY + "px";
|
129 |
-
ball.style.left = ballX + "px";
|
130 |
-
};
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
currentScore = 0;
|
135 |
-
level = 1;
|
136 |
-
ballSpeed = 2;
|
137 |
-
fallSpeed = 3;
|
138 |
-
score.innerHTML = "Score: " + currentScore;
|
139 |
-
levelDisplay.innerHTML = "Level: " + level;
|
140 |
-
updateLives();
|
141 |
-
resetBall();
|
142 |
-
};
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
requestAnimationFrame(gameLoop); // Loop the game
|
147 |
-
};
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
</body>
|
152 |
-
</html>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Futuristic Ball Game</title>
|
7 |
<link rel="stylesheet" href="style.css">
|
8 |
</head>
|
9 |
<body>
|
10 |
<div class="game-container">
|
|
|
|
|
11 |
<div id="score">Score: 0</div>
|
12 |
<div id="level">Level: 1</div>
|
13 |
+
<div id="lives">Lives: ❤️ ❤️ ❤️ ❤️</div>
|
14 |
+
<div class="platform"></div>
|
|
|
|
|
|
|
|
|
15 |
</div>
|
16 |
+
|
17 |
<script>
|
18 |
+
let score = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
let level = 1;
|
20 |
+
let lives = 4;
|
21 |
+
|
22 |
+
const scoreElement = document.getElementById('score');
|
23 |
+
const levelElement = document.getElementById('level');
|
24 |
+
const livesElement = document.getElementById('lives');
|
25 |
+
|
26 |
+
// Function to create a new ball
|
27 |
+
function createBall() {
|
28 |
+
const ball = document.createElement('div');
|
29 |
+
ball.classList.add('ball');
|
30 |
+
|
31 |
+
// Random horizontal position for the ball
|
32 |
+
ball.style.left = `${Math.random() * (window.innerWidth - 30)}px`;
|
33 |
+
document.body.appendChild(ball);
|
34 |
+
|
35 |
+
let ballFallInterval = setInterval(() => {
|
36 |
+
const ballRect = ball.getBoundingClientRect();
|
37 |
+
const platformRect = document.querySelector('.platform').getBoundingClientRect();
|
38 |
+
|
39 |
+
// Check if the ball has hit the platform
|
40 |
+
if (
|
41 |
+
ballRect.bottom >= platformRect.top &&
|
42 |
+
ballRect.left >= platformRect.left &&
|
43 |
+
ballRect.right <= platformRect.right
|
44 |
+
) {
|
45 |
+
score += 10;
|
46 |
+
scoreElement.textContent = `Score: ${score}`;
|
47 |
+
ball.remove(); // Remove the ball after it has been caught
|
48 |
+
clearInterval(ballFallInterval); // Stop the falling animation for this ball
|
49 |
+
createBall(); // Create a new ball for the next round
|
50 |
+
} else if (ballRect.top > window.innerHeight) {
|
51 |
+
lives -= 1;
|
52 |
+
livesElement.innerHTML = `Lives: ${'❤️ '.repeat(lives)}`;
|
53 |
+
ball.remove(); // Remove the ball if it falls off screen
|
54 |
+
clearInterval(ballFallInterval); // Stop the ball's fall
|
55 |
+
createBall(); // Create a new ball after losing a life
|
56 |
+
if (lives === 0) {
|
57 |
+
alert('Game Over!');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
}
|
60 |
+
}, 20); // Check every 20ms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
return ball;
|
63 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
// Create the first ball
|
66 |
+
createBall();
|
|
|
|
|
67 |
|
68 |
+
// Move the platform with mouse and touch events
|
69 |
+
const platform =
|
|
|
|