GarGerry commited on
Commit
5f58408
·
verified ·
1 Parent(s): ebf6a25

Update index.html

Browse files
Files changed (1) hide show
  1. 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 Falling Ball Game</title>
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
- <img class="life" src="life-icon.png" alt="Life" id="life1">
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 ball = document.getElementById("ball");
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 fallSpeed = 3; // Initial fall speed (seconds)
33
- let platformX = window.innerWidth / 2 - 50;
34
- platform.style.left = platformX + "px";
35
-
36
- let remainingLives = 4;
37
- let ballY = -30;
38
- let ballX = Math.floor(Math.random() * (window.innerWidth - 30));
39
- let ballSpeed = 2; // Ball fall speed per frame
40
- let isTouching = false;
41
-
42
- // Kontrol menggunakan Mouse
43
- document.addEventListener("mousemove", (e) => {
44
- platformX = e.clientX - platform.offsetWidth / 2;
45
- if (platformX < 0) platformX = 0;
46
- if (platformX > window.innerWidth - platform.offsetWidth) {
47
- platformX = window.innerWidth - platform.offsetWidth;
48
- }
49
- platform.style.left = platformX + "px";
50
- });
51
-
52
- // Kontrol menggunakan Sentuhan
53
- document.addEventListener("touchmove", (e) => {
54
- if (isTouching) {
55
- let touchX = e.touches[0].clientX;
56
- platformX = touchX - platform.offsetWidth / 2;
57
- if (platformX < 0) platformX = 0;
58
- if (platformX > window.innerWidth - platform.offsetWidth) {
59
- platformX = window.innerWidth - platform.offsetWidth;
60
- }
61
- platform.style.left = platformX + "px";
62
- }
63
- }, { passive: false });
64
-
65
- document.addEventListener("touchstart", (e) => {
66
- isTouching = true;
67
- });
68
-
69
- document.addEventListener("touchend", (e) => {
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
- resetBall();
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
- let resetGame = () => {
133
- remainingLives = 4;
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
- let gameLoop = () => {
145
- updateBallPosition();
146
- requestAnimationFrame(gameLoop); // Loop the game
147
- };
148
 
149
- gameLoop(); // Start the game loop
150
- </script>
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 =