awacke1 commited on
Commit
ddcfa02
·
verified ·
1 Parent(s): dffc370

Update index.html

Browse files
Files changed (1) hide show
  1. 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
- 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
- if (balls[i].position.x + 0.5 > 25 || balls[i].position.x - 0.5 < -25) {
78
- balls[i].velocity.x = -balls[i].velocity.x;
79
- }
80
- if (balls[i].position.y + 0.5 > 25 || balls[i].position.y - 0.5 < -25) {
81
- balls[i].velocity.y = -balls[i].velocity.y;
82
- }
83
- if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
84
- balls[i].velocity.z = -balls[i].velocity.z;
85
- }
86
- if (balls[i].position.y - 0.5 < -15 && balls[i].position.x + 0.5 > paddle.position.x - 2 && balls[i].position.x - 0.5 < paddle.position.x + 2 && balls[i].position.z + 0.5 > paddle.position.z - 0.5 && balls[i].position.z - 0.5 < paddle.position.z + 0.5) {
87
- balls[i].velocity.y = Math.abs(balls[i].velocity.y);
88
- }
89
- for (var j = 0; j < bricks.length; j++) {
90
- if (balls[i].position.distanceTo(bricks[j].position) <= 2) {
91
- scene.remove(bricks[j]);
92
- bricks.splice(j, 1);
93
- balls[i].velocity.negate();
94
- addBrick();
95
- }
96
- }
97
- }
98
- renderer.render(scene, camera);
99
- }
100
- var keys = {};
101
- var mouseX = null;
102
- var mouseY = null;
103
- document.addEventListener('mousedown', function(event) {
104
- if (event.button === 0) {
105
- addBrick();
106
- }
107
- if (event.button === 2) {
108
- addBall();
109
- }
110
- });
111
- document.addEventListener('keydown', function(event) {
112
- keys[event.keyCode] = true;
113
- });
114
- document.addEventListener('keyup', function(event) {
115
- keys[event.keyCode] = false;
116
- });
117
- document.addEventListener('mousemove', function(event) {
118
- mouseX = event.clientX;
119
- mouseY = event.clientY;
120
- });
121
- document.addEventListener('mouseleave', function(event) {
122
- mouseX = null;
123
- mouseY = null;
124
- });
125
- animate();
126
- </script>
 
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>