File size: 2,352 Bytes
e762863
 
 
 
 
5e8e113
2fe2a19
e762863
 
5e8e113
 
 
e762863
78ba153
 
 
 
5e8e113
 
 
 
78ba153
 
5e8e113
 
 
 
78ba153
 
 
5e8e113
 
78ba153
 
 
 
 
5e8e113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e762863
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Futuristic Catch Game</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="game-container">
        <div class="ball"></div>
        <div class="platform"></div>
    </div>
    <div class="controls">
        <button id="left-button"></button>
        <button id="right-button"></button>
    </div>
    <script>
        const platform = document.querySelector('.platform');
        const ball = document.querySelector('.ball');
        const gameContainer = document.querySelector('.game-container');
        const leftButton = document.getElementById('left-button');
        const rightButton = document.getElementById('right-button');

        let platformPosition = 50; // Initial position of the platform (centered).
        let ballPosition = { top: 0, left: Math.random() * 90 }; // Random horizontal start for the ball.

        // Mobile controls
        leftButton.addEventListener('click', () => {
            if (platformPosition > 0) {
                platformPosition -= 5; // Move left
            }
            platform.style.left = platformPosition + '%';
        });

        rightButton.addEventListener('click', () => {
            if (platformPosition < 90) {
                platformPosition += 5; // Move right
            }
            platform.style.left = platformPosition + '%';
        });

        // Move ball
        function moveBall() {
            ballPosition.top += 2; // Ball falls down
            ball.style.top = ballPosition.top + '%';
            ball.style.left = ballPosition.left + '%';

            // Check for collision with platform
            if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
                alert('You caught the ball!');
                resetGame();
            }

            // Check if ball missed
            if (ballPosition.top >= 100) {
                alert('Game Over!');
                resetGame();
            }

            requestAnimationFrame(moveBall);
        }

        // Reset game
        function resetGame() {
            ballPosition = { top: 0, left: Math.random() * 90 };
        }

        moveBall();
    </script>
</body>
</html>