awacke1 commited on
Commit
10b277c
1 Parent(s): fb4f027

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +106 -108
index.html CHANGED
@@ -1,111 +1,109 @@
1
  <!DOCTYPE html>
2
  <html>
3
- <head>
4
- <meta charset="utf-8">
5
- <title>3D Breakout Game</title>
6
- <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
7
- </head>
8
- <body>
9
- <a-scene physics="debug: true">
10
- <!-- Add a camera to the scene -->
11
- <a-entity camera look-controls position="0 1.6 0"></a-entity>
12
-
13
- <!-- Create the game room cube -->
14
- <a-box width="10" height="10" depth="10" color="#555" static-body></a-box>
15
-
16
- <!-- Create the paddle -->
17
- <a-plane width="2" height="0.2" color="#f00" position="0 -4.9 0" kinematic-body></a-plane>
18
-
19
- <!-- Create the ball -->
20
- <a-sphere radius="0.2" color="#00f" position="0 -4.5 0" dynamic-body></a-sphere>
21
-
22
- <!-- Create the bricks -->
23
- <a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-2.5 2.5 0" static-body></a-box>
24
- <a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-1.5 2.5 0" static-body></a-box>
25
- <a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-0.5 2.5 0" static-body></a-box>
26
- <a-box width="1" height="0.5" depth="0.5" color="#0f0" position="0.5 2.5 0" static-body></a-box>
27
- <a-box width="1" height="0.5" depth="0.5" color="#0f0" position="1.5 2.5 0" static-body></a-box>
28
- <a-box width="1" height="0.5" depth="0.5" color="#0f0" position="2.5 2.5 0" static-body></a-box>
29
-
30
- <!-- Add the mouse events to move the paddle -->
31
- <script>
32
- // Get the paddle element
33
- var paddle = document.querySelector('[kinematic-body]');
34
-
35
- // Add an event listener for mouse movement
36
- window.addEventListener('mousemove', function (event) {
37
- // Get the x and y coordinates of the mouse
38
- var x = event.clientX / window.innerWidth * 2 - 1;
39
- var y = event.clientY / window.innerHeight * 2 - 1;
40
-
41
- // Move the paddle along the x-axis based on the mouse position
42
- paddle.object3D.position.x = x * 5;
43
- });
44
- </script>
45
-
46
- <!-- Add the script for ball movement and collision detection -->
47
- <script>
48
- // Get the ball element
49
- var ball = document.querySelector('[dynamic-body]');
50
-
51
- // Set the initial velocity of the ball
52
- var velocity = new THREE.Vector3(0, 0, -5);
53
- ball.body.velocity.copy(velocity);
54
-
55
- // Add an event listener for collision detection
56
- ball.addEventListener('collide', function (event) {
57
- // Get the object that the ball collided with
58
- var object = event.detail.body.el;
59
-
60
- // If the object is a
61
- // brick, remove it and update the score
62
- if (object.hasAttribute('static-body')) {
63
- object.parentNode.removeChild(object);
64
- // update the score here
65
- }
66
-
67
- // If the object is the game room cube, bounce the ball back
68
- if (object.hasAttribute('static-body') && object.tagName === 'A-BOX') {
69
- // Get the normal of the collision
70
- var normal = event.detail.contact.ni;
71
-
72
- // Reflect the velocity of the ball off the normal
73
- velocity.reflect(normal);
74
-
75
- // Set the new velocity of the ball
76
- ball.body.velocity.copy(velocity);
77
- }
78
- });
79
-
80
- // Add a function to update the position of the ball every frame
81
- function updateBallPosition() {
82
- // If the ball is below the paddle, reset the game
83
- if (ball.object3D.position.y < -5) {
84
- // reset the game here
85
- }
86
-
87
- // If the ball is outside the game room cube, bounce it back
88
- if (ball.object3D.position.x < -4.5 || ball.object3D.position.x > 4.5) {
89
- velocity.x = -velocity.x;
90
- ball.body.velocity.copy(velocity);
91
- }
92
- if (ball.object3D.position.y > 4.5) {
93
- velocity.y = -velocity.y;
94
- ball.body.velocity.copy(velocity);
95
- }
96
- if (ball.object3D.position.z < -4.5 || ball.object3D.position.z > 4.5) {
97
- velocity.z = -velocity.z;
98
- ball.body.velocity.copy(velocity);
99
- }
100
-
101
- // Update the position of the ball based on its velocity
102
- ball.object3D.position.addScaledVector(velocity, 1 / 60);
103
- }
104
-
105
- // Add the update function to the scene's update loop
106
- var scene = document.querySelector('a-scene');
107
- scene.addEventListener('update', updateBallPosition);
108
- </script>
109
- </a-scene>
110
- </body>
111
  </html>
 
1
  <!DOCTYPE html>
2
  <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>3D Breakout Game</title>
6
+ <style>
7
+ body { margin: 0; }
8
+ canvas { width: 100%; height: 100%; }
9
+ </style>
10
+ <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
11
+ </head>
12
+ <body>
13
+ <canvas id="gameCanvas"></canvas>
14
+
15
+ <script>
16
+ var scene = new THREE.Scene();
17
+ var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
18
+ camera.position.z = 5;
19
+ scene.add(camera);
20
+
21
+ var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
22
+ renderer.setClearColor(0x000000);
23
+ renderer.setPixelRatio(window.devicePixelRatio);
24
+ renderer.setSize(window.innerWidth, window.innerHeight);
25
+
26
+ var ballGeometry = new THREE.SphereGeometry(0.5, 32, 32);
27
+ var ballMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
28
+ var ball = new THREE.Mesh(ballGeometry, ballMaterial);
29
+ scene.add(ball);
30
+
31
+ var paddleGeometry = new THREE.BoxGeometry(4, 1, 1);
32
+ var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
33
+ var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
34
+ paddle.position.y = -3;
35
+ scene.add(paddle);
36
+
37
+ var brickGeometry = new THREE.BoxGeometry(2, 1, 1);
38
+ var brickMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
39
+ var bricks = [];
40
+
41
+ for (var i = 0; i < 5; i++) {
42
+ for (var j = 0; j < 6; j++) {
43
+ var brick = new THREE.Mesh(brickGeometry, brickMaterial);
44
+ brick.position.x = j * 3 - 7;
45
+ brick.position.y = i * 2 + 3;
46
+ scene.add(brick);
47
+ bricks.push(brick);
48
+ }
49
+ }
50
+
51
+ var light = new THREE.PointLight(0xffffff, 1, 100);
52
+ light.position.set(0, 0, 10);
53
+ scene.add(light);
54
+
55
+ var speed = 0.05;
56
+ var xSpeed = speed;
57
+ var ySpeed = speed;
58
+
59
+ function animate() {
60
+ requestAnimationFrame(animate);
61
+
62
+ ball.position.x += xSpeed;
63
+ ball.position.y += ySpeed;
64
+
65
+ if (ball.position.x + 0.5 > 10 || ball.position.x - 0.5 < -10) {
66
+ xSpeed = -xSpeed;
67
+ }
68
+
69
+ if (ball.position.y + 0.5 > 10) {
70
+ ySpeed = -ySpeed;
71
+ }
72
+
73
+ if (ball.position.y - 0.5 < -4) {
74
+ alert('Game Over!');
75
+ location.reload();
76
+ }
77
+
78
+ if (ball.position.y - 0.5 < paddle.position.y + 0.5 && ball.position.x + 0.5 > paddle.position.x - 2 && ball.position.x - 0.5 < paddle.position.x + 2) {
79
+ ySpeed = -ySpeed;
80
+ }
81
+
82
+ for (var i = 0; i < bricks.length; i++) {
83
+ if (ball.position.y + 0.5 > bricks[i].position.y - 0.5 && ball.position.y - 0.5 < bricks[i].position.y + 0.5 && ball.position.x + 0.5 > bricks[i].position.x - 1 && ball.position.x - 0.5 < bricks[i].position.x + 1) {
84
+ scene.remove(bricks[i]);
85
+ bricks.splice(i, 1);
86
+ ySpeed = -ySpeed;
87
+ }
88
+ }
89
+ if (bricks.length === 0) {
90
+ alert('You win!');
91
+ location.reload();
92
+ }
93
+
94
+ renderer.render(scene, camera);
95
+ }
96
+
97
+ document.addEventListener('keydown', function(event) {
98
+ if (event.keyCode === 37) {
99
+ paddle.position.x -= 0.5;
100
+ }
101
+ if (event.keyCode === 39) {
102
+ paddle.position.x += 0.5;
103
+ }
104
+ });
105
+
106
+ animate();
107
+ </script>
108
+ </body>
 
 
109
  </html>