File size: 8,945 Bytes
2282144
99068ab
2282144
 
dcde2b7
2282144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcde2b7
2282144
 
 
 
 
 
dcde2b7
2282144
 
 
dcde2b7
2282144
dcde2b7
 
 
2282144
dcde2b7
 
2282144
 
 
dcde2b7
2282144
 
 
 
 
 
 
 
dcde2b7
 
 
 
 
 
2282144
 
dcde2b7
 
2282144
 
 
 
 
 
 
 
 
dcde2b7
2282144
 
 
 
dcde2b7
2282144
 
dcde2b7
 
 
 
 
2282144
dcde2b7
 
2282144
 
 
 
 
 
 
dcde2b7
2282144
dcde2b7
 
 
 
 
 
 
 
 
 
 
 
2282144
 
 
 
 
 
 
 
dcde2b7
 
 
2282144
 
 
 
 
 
dcde2b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2282144
 
 
dcde2b7
 
 
2282144
 
 
 
 
 
 
dcde2b7
 
 
 
 
 
 
2282144
 
dcde2b7
2282144
 
dcde2b7
2282144
 
 
 
 
 
dcde2b7
 
 
2282144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcde2b7
2282144
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>A-Frame 3D Breakout Shooter Game (Enhanced Speed)</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/aframe/1.2.0/aframe.min.js"></script>
    <style>
        #hud {
            position: fixed;
            top: 10px;
            left: 10px;
            color: white;
            font-family: Arial, sans-serif;
            font-size: 16px;
            z-index: 999;
        }
        #instructions {
            position: fixed;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            color: white;
            font-family: Arial, sans-serif;
            font-size: 14px;
            text-align: center;
            z-index: 999;
        }
    </style>
</head>
<body>
    <div id="hud">
        <div>Score: <span id="score">0</span></div>
        <div>Lives: <span id="lives">3</span></div>
        <div>Balls: <span id="ballCount">1</span></div>
    </div>
    <div id="instructions">
        Move mouse to control paddle | Click to shoot | Press spacebar to launch new ball
    </div>

    <a-scene background="color: #87CEEB">
        <!-- Scene setup remains the same -->
    </a-scene>

    <script>
        // ... (previous variable declarations remain the same)

        const INITIAL_BALL_SPEED = 10; // Increased from 5
        const MAX_BALL_SPEED = 25; // Increased from 15
        const BULLET_SPEED = 30;

        function init() {
            // ... (initialization remains the same)
        }

        function createBlocks() {
            // ... (createBlocks function remains the same)
        }

        function createBall() {
            const ball = document.createElement('a-sphere');
            ball.setAttribute('class', 'ball collidable');
            ball.setAttribute('radius', '0.2');
            ball.setAttribute('material', 'color: #EF2D5E');
            ball.setAttribute('position', {x: 0, y: -5, z: 0});
            const angle = Math.random() * Math.PI / 2 - Math.PI / 4; // Random angle between -45 and 45 degrees
            ball.velocity = new THREE.Vector3(
                Math.sin(angle) * INITIAL_BALL_SPEED,
                Math.cos(angle) * INITIAL_BALL_SPEED,
                0
            );
            ballsContainer.appendChild(ball);
            balls.push(ball);
            updateBallCount();
            updatePaddleSize();
        }

        function shootBullet() {
            const bullet = document.createElement('a-sphere');
            bullet.setAttribute('class', 'bullet collidable');
            bullet.setAttribute('radius', '0.1');
            bullet.setAttribute('material', 'color: #FFFFFF');
            const paddlePos = paddle.getAttribute('position');
            bullet.setAttribute('position', {x: paddlePos.x, y: paddlePos.y + 0.5, z: 0});
            bullet.velocity = new THREE.Vector3(0, BULLET_SPEED, 0);
            bulletsContainer.appendChild(bullet);
            bullets.push(bullet);
        }

        // ... (other functions remain the same)

        function update() {
            const currentTime = Date.now();
            const deltaTime = (currentTime - lastUpdateTime) / 1000; // Convert to seconds
            lastUpdateTime = currentTime;

            balls.forEach((ball, ballIndex) => {
                const ballPos = ball.getAttribute('position');
                ballPos.x += ball.velocity.x * deltaTime;
                ballPos.y += ball.velocity.y * deltaTime;

                // Wall collisions
                if (ballPos.x <= -9.8 || ballPos.x >= 9.8) ball.velocity.x *= -1;
                if (ballPos.y >= 9.8) ball.velocity.y *= -1;

                // Paddle collision
                const paddlePos = paddle.getAttribute('position');
                const paddleWidth = paddle.getAttribute('geometry').width;
                if (ballPos.y <= paddlePos.y + 0.5 && ballPos.y >= paddlePos.y &&
                    ballPos.x >= paddlePos.x - paddleWidth/2 && ballPos.x <= paddlePos.x + paddleWidth/2) {
                    
                    // Realistic ball bounce (remains the same)
                    ball.velocity.y = Math.abs(ball.velocity.y);
                    ball.velocity.x += paddleVelocity.x * 0.5;
                    const hitPosition = (ballPos.x - paddlePos.x) / (paddleWidth / 2);
                    ball.velocity.x += hitPosition * 2;
                    
                    // Normalize and scale velocity
                    const speed = Math.sqrt(ball.velocity.x * ball.velocity.x + ball.velocity.y * ball.velocity.y);
                    ball.velocity.x = (ball.velocity.x / speed) * INITIAL_BALL_SPEED;
                    ball.velocity.y = (ball.velocity.y / speed) * INITIAL_BALL_SPEED;
                }

                // Block collisions
                const blocks = document.querySelectorAll('.block');
                blocks.forEach(block => {
                    const blockPos = block.getAttribute('position');
                    if (ballPos.x >= blockPos.x - 1 && ballPos.x <= blockPos.x + 1 &&
                        ballPos.y >= blockPos.y - 0.5 && ballPos.y <= blockPos.y + 0.5) {
                        if (block.getAttribute('special') === 'true') {
                            createBall();
                        }
                        blocksContainer.removeChild(block);
                        ball.velocity.y *= -1;
                        updateScore();
                    }
                });

                // Ball-bullet collisions
                bullets.forEach((bullet, bulletIndex) => {
                    const bulletPos = bullet.getAttribute('position');
                    const distance = new THREE.Vector3(bulletPos.x - ballPos.x, bulletPos.y - ballPos.y, 0).length();
                    if (distance < 0.3) { // Assuming ball radius (0.2) + bullet radius (0.1)
                        // Accelerate the ball
                        const bulletVelocity = bullet.velocity;
                        ball.velocity.x = bulletVelocity.x;
                        ball.velocity.y = bulletVelocity.y;
                        
                        // Remove the bullet
                        bulletsContainer.removeChild(bullet);
                        bullets.splice(bulletIndex, 1);
                    }
                });

                // Check if ball is out of bounds
                if (ballPos.y < -10) {
                    ballsContainer.removeChild(ball);
                    balls.splice(ballIndex, 1);
                    updateBallCount();
                    updatePaddleSize();
                    if (balls.length === 0) {
                        updateLives();
                        if (lives > 0) createBall();
                    }
                } else {
                    ball.setAttribute('position', ballPos);
                }

                // Limit ball speed
                const speed = Math.sqrt(ball.velocity.x * ball.velocity.x + ball.velocity.y * ball.velocity.y);
                if (speed > MAX_BALL_SPEED) {
                    ball.velocity.x = (ball.velocity.x / speed) * MAX_BALL_SPEED;
                    ball.velocity.y = (ball.velocity.y / speed) * MAX_BALL_SPEED;
                }
            });

            // Update bullets
            bullets.forEach((bullet, index) => {
                const bulletPos = bullet.getAttribute('position');
                bulletPos.y += bullet.velocity.y * deltaTime;

                const blocks = document.querySelectorAll('.block');
                blocks.forEach(block => {
                    const blockPos = block.getAttribute('position');
                    if (bulletPos.x >= blockPos.x - 1 && bulletPos.x <= blockPos.x + 1 &&
                        bulletPos.y >= blockPos.y - 0.5 && bulletPos.y <= blockPos.y + 0.5) {
                        if (block.getAttribute('special') === 'true') {
                            createBall();
                        }
                        blocksContainer.removeChild(block);
                        bulletsContainer.removeChild(bullet);
                        bullets.splice(index, 1);
                        updateScore();
                    }
                });

                if (bulletPos.y > 10) {
                    bulletsContainer.removeChild(bullet);
                    bullets.splice(index, 1);
                } else {
                    bullet.setAttribute('position', bulletPos);
                }
            });

            // Check win condition
            if (document.querySelectorAll('.block').length === 0) {
                clearInterval(paddleColorChangeInterval);
                alert('Congratulations! You won! Your score: ' + score);
                location.reload();
            }

            requestAnimationFrame(update);
        }

        document.querySelector('a-scene').addEventListener('loaded', init);
    </script>
</body>
</html>