GarGerry commited on
Commit
07012de
·
verified ·
1 Parent(s): a1b39e1

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +92 -64
index.html CHANGED
@@ -1,82 +1,110 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
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="styles.css">
8
  </head>
9
  <body>
10
- <div class="game-container">
11
- <div class="falling-ball" id="ball"></div>
12
- <div class="catcher" id="catcher"></div>
13
- <div id="score">Score: 0</div>
14
- </div>
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- <script>
17
- const ball = document.getElementById("ball");
18
- const catcher = document.getElementById("catcher");
19
- const scoreDisplay = document.getElementById("score");
20
- let score = 0;
21
- let isGameOver = false;
22
 
23
- // Function to move the falling ball
24
- function startFalling() {
25
- let ballPosition = 0;
26
- const ballSpeed = 4;
27
- const catcherPosition = catcher.offsetLeft;
28
-
29
- function fallBall() {
30
- if (isGameOver) return;
31
 
32
- ballPosition += ballSpeed;
33
- ball.style.top = ballPosition + "px";
 
 
 
 
 
34
 
35
- // If the ball reaches the bottom, check if it's caught by the catcher
36
- if (ballPosition >= window.innerHeight - 100) {
37
- if (ballPosition >= window.innerHeight - 120 &&
38
- ballPosition <= window.innerHeight - 100 &&
39
- (catcherPosition <= ball.offsetLeft &&
40
- catcherPosition + catcher.offsetWidth >= ball.offsetLeft + ball.offsetWidth)) {
41
- score++;
42
- scoreDisplay.textContent = "Score: " + score;
43
- }
44
 
45
- // Reset ball position after it falls
46
- ballPosition = 0;
47
- ball.style.left = Math.random() * (window.innerWidth - 50) + "px";
48
- }
49
 
50
- // Continue falling
51
- if (!isGameOver) {
52
- requestAnimationFrame(fallBall);
53
- }
54
- }
 
55
 
56
- fallBall();
57
- }
 
 
 
 
58
 
59
- // Function to move the catcher based on mouse movement
60
- function moveCatcher(event) {
61
- const catcherWidth = catcher.offsetWidth;
62
- if (event.clientX >= 0 && event.clientX <= window.innerWidth - catcherWidth) {
63
- catcher.style.left = event.clientX - catcherWidth / 2 + "px";
64
- }
65
- }
66
 
67
- document.addEventListener("mousemove", moveCatcher);
 
 
 
 
 
 
68
 
69
- // Function to start the game
70
- function startGame() {
71
- score = 0;
72
- scoreDisplay.textContent = "Score: " + score;
73
- isGameOver = false;
74
- ball.style.top = "0px";
75
- ball.style.left = Math.random() * (window.innerWidth - 50) + "px";
76
- startFalling();
77
- }
 
 
 
 
 
 
 
 
 
78
 
79
- startGame();
80
- </script>
 
81
  </body>
82
  </html>
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Futuristic Catch Game</title>
7
+ <link rel="stylesheet" href="style.css">
8
  </head>
9
  <body>
10
+ <div class="game-container">
11
+ <div class="ball"></div>
12
+ <div class="platform"></div>
13
+ <div class="score-board">Score: <span id="score">0</span></div>
14
+ </div>
15
+ <div class="controls">
16
+ <button id="left-button">◀</button>
17
+ <button id="right-button">▶</button>
18
+ </div>
19
+ <script>
20
+ const platform = document.querySelector('.platform');
21
+ const ball = document.querySelector('.ball');
22
+ const leftButton = document.getElementById('left-button');
23
+ const rightButton = document.getElementById('right-button');
24
+ const scoreDisplay = document.getElementById('score');
25
+ const gameContainer = document.querySelector('.game-container');
26
 
27
+ let platformPosition = 50; // Platform awal di tengah.
28
+ let ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul secara acak.
29
+ let isCaught = false; // Apakah bola ditangkap.
30
+ let score = 0; // Skor awal.
 
 
31
 
32
+ // Fungsi untuk memindahkan platform ke kiri
33
+ function movePlatformLeft() {
34
+ if (platformPosition > 0) {
35
+ platformPosition -= 5;
36
+ }
37
+ platform.style.left = platformPosition + '%';
38
+ }
 
39
 
40
+ // Fungsi untuk memindahkan platform ke kanan
41
+ function movePlatformRight() {
42
+ if (platformPosition < 90) {
43
+ platformPosition += 5;
44
+ }
45
+ platform.style.left = platformPosition + '%';
46
+ }
47
 
48
+ // Keyboard controls
49
+ window.addEventListener('keydown', (e) => {
50
+ if (e.key === 'ArrowLeft') movePlatformLeft();
51
+ if (e.key === 'ArrowRight') movePlatformRight();
52
+ });
 
 
 
 
53
 
54
+ // Button controls
55
+ leftButton.addEventListener('click', movePlatformLeft);
56
+ rightButton.addEventListener('click', movePlatformRight);
 
57
 
58
+ // Mouse/Touch controls
59
+ gameContainer.addEventListener('mousemove', (e) => {
60
+ const containerWidth = gameContainer.offsetWidth;
61
+ platformPosition = (e.clientX / containerWidth) * 100;
62
+ platform.style.left = platformPosition + '%';
63
+ });
64
 
65
+ gameContainer.addEventListener('touchmove', (e) => {
66
+ const touch = e.touches[0];
67
+ const containerWidth = gameContainer.offsetWidth;
68
+ platformPosition = (touch.clientX / containerWidth) * 100;
69
+ platform.style.left = platformPosition + '%';
70
+ });
71
 
72
+ // Fungsi untuk menggerakkan bola
73
+ function moveBall() {
74
+ if (isCaught) return; // Jangan lanjut jika bola ditangkap.
75
+ ballPosition.top += 2; // Bola turun.
76
+ ball.style.top = ballPosition.top + '%';
77
+ ball.style.left = ballPosition.left + '%';
 
78
 
79
+ // Deteksi tabrakan (collision detection)
80
+ if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
81
+ isCaught = true;
82
+ score++; // Tambahkan skor.
83
+ scoreDisplay.textContent = score; // Update skor di layar.
84
+ resetGame();
85
+ }
86
 
87
+ // Game over jika bola melewati platform
88
+ if (ballPosition.top >= 100) {
89
+ if (!isCaught) {
90
+ alert('Game Over! Final Score: ' + score);
91
+ score = 0; // Reset skor.
92
+ scoreDisplay.textContent = score; // Update skor.
93
+ }
94
+ resetGame();
95
+ }
96
+
97
+ requestAnimationFrame(moveBall);
98
+ }
99
+
100
+ // Fungsi untuk mereset permainan
101
+ function resetGame() {
102
+ isCaught = false;
103
+ ballPosition = { top: 0, left: Math.random() * 90 }; // Bola muncul ulang.
104
+ }
105
 
106
+ // Memulai permainan
107
+ moveBall();
108
+ </script>
109
  </body>
110
  </html>