Spaces:
Running
Running
Update index.html
Browse files- index.html +73 -72
index.html
CHANGED
|
@@ -52,77 +52,78 @@
|
|
| 52 |
var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
|
| 53 |
var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
|
| 54 |
scene.add(paddle);
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
paddle.position.z += paddleSpeed;
|
| 63 |
-
}
|
| 64 |
-
if (keys["65"] || keys["37"]) { // a or left arrow
|
| 65 |
-
paddle.position.x -= paddleSpeed;
|
| 66 |
-
}
|
| 67 |
-
if (keys["68"] || keys["39"]) { // d or right arrow
|
| 68 |
-
paddle.position.x += paddleSpeed;
|
| 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 |
</body>
|
| 128 |
</html>
|
|
|
|
| 52 |
var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
|
| 53 |
var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
|
| 54 |
scene.add(paddle);
|
| 55 |
+
function animate() {
|
| 56 |
+
requestAnimationFrame(animate);
|
| 57 |
+
var paddleSpeed = 1.5;
|
| 58 |
+
if (keys["87"] || keys["38"]) { // w or up arrow
|
| 59 |
+
paddle.position.z -= paddleSpeed;
|
| 60 |
+
}
|
| 61 |
+
if (keys["83"] || keys["40"]) {
|
| 62 |
+
paddle.position.z += paddleSpeed;
|
| 63 |
+
}
|
| 64 |
+
if (keys["65"] || keys["37"]) { // a or left arrow
|
| 65 |
+
paddle.position.x -= paddleSpeed;
|
| 66 |
+
}
|
| 67 |
+
if (keys["68"] || keys["39"]) { // d or right arrow
|
| 68 |
+
paddle.position.x += paddleSpeed;
|
| 69 |
+
}
|
| 70 |
+
var mouseSpeed = 0.1;
|
| 71 |
+
if (mouseX !== null && mouseY !== null) {
|
| 72 |
+
paddle.position.x = (mouseX / window.innerWidth) * 50 - 25;
|
| 73 |
+
paddle.position.z = -(mouseY / window.innerHeight) * 50 + 25;
|
| 74 |
+
}
|
| 75 |
+
for (var i = 0; i < balls.length; i++) {
|
| 76 |
+
balls[i].position.add(balls[i].velocity);
|
| 77 |
+
// Collision detection with the walls and paddle updated for wider paddle
|
| 78 |
+
if (balls[i].position.x + 0.5 > 25 || balls[i].position.x - 0.5 < -25) {
|
| 79 |
+
balls[i].velocity.x = -balls[i].velocity.x;
|
| 80 |
+
}
|
| 81 |
+
if (balls[i].position.y + 0.5 > 25 || balls[i].position.y - 0.5 < -25) {
|
| 82 |
+
balls[i].velocity.y = -balls[i].velocity.y;
|
| 83 |
+
}
|
| 84 |
+
if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
|
| 85 |
+
balls[i].velocity.z = -balls[i].velocity.z;
|
| 86 |
+
}
|
| 87 |
+
if (balls[i].position.y - 0.5 < -15 && balls[i].position.x + 0.5 > paddle.position.x - 3 && balls[i].position.x - 0.5 < paddle.position.x + 3 && balls[i].position.z + 0.5 > paddle.position.z - 0.5 && balls[i].position.z - 0.5 < paddle.position.z + 0.5) { // Adjusted for wider paddle
|
| 88 |
+
balls[i].velocity.y = Math.abs(balls[i].velocity.y);
|
| 89 |
+
}
|
| 90 |
+
for (var j = 0; j < bricks.length; j++) {
|
| 91 |
+
if (balls[i].position.distanceTo(bricks[j].position) <= 2) {
|
| 92 |
+
scene.remove(bricks[j]);
|
| 93 |
+
bricks.splice(j, 1);
|
| 94 |
+
balls[i].velocity.negate();
|
| 95 |
+
addBrick();
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
renderer.render(scene, camera);
|
| 100 |
+
}
|
| 101 |
+
var keys = {};
|
| 102 |
+
var mouseX = null;
|
| 103 |
+
var mouseY = null;
|
| 104 |
+
document.addEventListener('mousedown', function(event) {
|
| 105 |
+
if (event.button === 0) {
|
| 106 |
+
addBrick();
|
| 107 |
+
}
|
| 108 |
+
if (event.button === 2) {
|
| 109 |
+
addBall();
|
| 110 |
+
}
|
| 111 |
+
});
|
| 112 |
+
document.addEventListener('keydown', function(event) {
|
| 113 |
+
keys[event.keyCode] = true;
|
| 114 |
+
});
|
| 115 |
+
document.addEventListener('keyup', function(event) {
|
| 116 |
+
keys[event.keyCode] = false;
|
| 117 |
+
});
|
| 118 |
+
document.addEventListener('mousemove', function(event) {
|
| 119 |
+
mouseX = event.clientX;
|
| 120 |
+
mouseY = event.clientY;
|
| 121 |
+
});
|
| 122 |
+
document.addEventListener('mouseleave', function(event) {
|
| 123 |
+
mouseX = null;
|
| 124 |
+
mouseY = null;
|
| 125 |
+
});
|
| 126 |
+
animate();
|
| 127 |
+
</script>
|
| 128 |
</body>
|
| 129 |
</html>
|