awacke1 commited on
Commit
dd8e521
·
verified ·
1 Parent(s): 9346d4c

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +120 -66
index.html CHANGED
@@ -1,73 +1,127 @@
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
4
- <title>Three.js Scene and Animation Example</title>
5
- <style>
6
- body {
7
- margin: 0;
8
- padding: 0;
9
- }
10
- canvas {
11
- display: block;
12
- }
13
- </style>
14
  </head>
15
  <body>
16
- <script src="https://threejs.org/build/three.min.js"></script>
17
- <script>
18
- // Set up the scene, camera, and renderer
19
- const scene = new THREE.Scene();
20
- const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
21
- const renderer = new THREE.WebGLRenderer();
22
- renderer.setSize(window.innerWidth, window.innerHeight);
23
- document.body.appendChild(renderer.domElement);
24
 
25
- // Add lighting to the scene
26
- const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
27
- scene.add(ambientLight);
28
- const pointLight = new THREE.PointLight(0xffffff, 1);
29
- pointLight.position.set(5, 5, 5);
30
- scene.add(pointLight);
31
-
32
- // Create a complex shaded shape geometry and material
33
- const geometry = new THREE.TorusKnotGeometry(1, 0.4, 256, 32, 2, 3, 1);
34
- const material = new THREE.MeshPhongMaterial({
35
- color: 0xffaaff,
36
- specular: 0x999999,
37
- shininess: 50,
38
- flatShading: false,
39
- });
40
-
41
- // Create a shape mesh and add it to the scene
42
- const shape = new THREE.Mesh(geometry, material);
43
- scene.add(shape);
44
-
45
- // Set up animation loop
46
- const animate = function () {
47
- requestAnimationFrame(animate);
48
-
49
- // Rotate the shape
50
- shape.rotation.x += 0.01;
51
- shape.rotation.y += 0.01;
52
-
53
- // Move the camera in a circle around the shape
54
- const time = Date.now() * 0.001;
55
- const radius = 10;
56
- camera.position.x = Math.sin(time) * radius;
57
- camera.position.z = Math.cos(time) * radius;
58
- camera.lookAt(new THREE.Vector3());
59
-
60
- // Update the lighting position to follow the camera
61
- pointLight.position.copy(camera.position);
62
-
63
- renderer.render(scene, camera);
64
- };
65
-
66
- // Start the animation loop
67
- animate();
68
-
69
- // Adjust camera position
70
- camera.position.z = 10;
71
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  </body>
73
- </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/[email protected]/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.set(0, 30, 0);
19
+ camera.lookAt(new THREE.Vector3(0, 0, 0));
20
+ scene.add(camera);
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
+ var ballGeometry = new THREE.SphereGeometry(0.5, 32, 32);
26
+ var ballMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
27
+ var balls = [];
28
+ var brickGeometry = new THREE.BoxGeometry(2, 1, 1);
29
+ var bricks = [];
30
+ function addBall() {
31
+ var ball = new THREE.Mesh(ballGeometry, ballMaterial);
32
+ ball.position.set((Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25);
33
+ ball.velocity = new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
34
+ scene.add(ball);
35
+ balls.push(ball);
36
+ }
37
+ function addBrick() {
38
+ var brickMaterial = new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff });
39
+ var brick = new THREE.Mesh(brickGeometry, brickMaterial);
40
+ brick.position.set((Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50);
41
+ scene.add(brick);
42
+ bricks.push(brick);
43
+ }
44
+ for (var i = 0; i < 50; i++) {
45
+ addBrick();
46
+ }
47
+ var light = new THREE.PointLight(0xffffff, 1, 100);
48
+ light.position.set(0, 30, 0);
49
+ scene.add(light);
50
+ var paddleGeometry = new THREE.BoxGeometry(4, 1, 1);
51
+ var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
52
+ var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
53
+ scene.add(paddle);
54
+ function animate() {
55
+ requestAnimationFrame(animate);
56
+ var paddleSpeed = 1.5;
57
+ if (keys["87"] || keys["38"]) { // w or up arrow
58
+ paddle.position.z -= paddleSpeed;
59
+ }
60
+ if (keys["83"] || keys["40"]) {
61
+ paddle.position.z += paddleSpeed;
62
+ }
63
+ if (keys["65"] || keys["37"]) { // a or left arrow
64
+ paddle.position.x -= paddleSpeed;
65
+ }
66
+ if (keys["68"] || keys["39"]) { // d or right arrow
67
+ paddle.position.x += paddleSpeed;
68
+ }
69
+ var mouseSpeed = 0.1;
70
+ if (mouseX !== null && mouseY !== null) {
71
+ paddle.position.x = (mouseX / window.innerWidth) * 50 - 25;
72
+ paddle.position.z = -(mouseY / window.innerHeight) * 50 + 25;
73
+ }
74
+ for (var i = 0; i < balls.length; i++) {
75
+ balls[i].position.add(balls[i].velocity);
76
+ if (balls[i].position.x + 0.5 > 25 || balls[i].position.x - 0.5 < -25) {
77
+ balls[i].velocity.x = -balls[i].velocity.x;
78
+ }
79
+ if (balls[i].position.y + 0.5 > 25 || balls[i].position.y - 0.5 < -25) {
80
+ balls[i].velocity.y = -balls[i].velocity.y;
81
+ }
82
+ if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
83
+ balls[i].velocity.z = -balls[i].velocity.z;
84
+ }
85
+ 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) {
86
+ balls[i].velocity.y = Math.abs(balls[i].velocity.y);
87
+ }
88
+ for (var j = 0; j < bricks.length; j++) {
89
+ if (balls[i].position.distanceTo(bricks[j].position) <= 2) {
90
+ scene.remove(bricks[j]);
91
+ bricks.splice(j, 1);
92
+ balls[i].velocity.negate();
93
+ addBrick();
94
+ }
95
+ }
96
+ }
97
+ renderer.render(scene, camera);
98
+ }
99
+ var keys = {};
100
+ var mouseX = null;
101
+ var mouseY = null;
102
+ document.addEventListener('mousedown', function(event) {
103
+ if (event.button === 0) {
104
+ addBrick();
105
+ }
106
+ if (event.button === 2) {
107
+ addBall();
108
+ }
109
+ });
110
+ document.addEventListener('keydown', function(event) {
111
+ keys[event.keyCode] = true;
112
+ });
113
+ document.addEventListener('keyup', function(event) {
114
+ keys[event.keyCode] = false;
115
+ });
116
+ document.addEventListener('mousemove', function(event) {
117
+ mouseX = event.clientX;
118
+ mouseY = event.clientY;
119
+ });
120
+ document.addEventListener('mouseleave', function(event) {
121
+ mouseX = null;
122
+ mouseY = null;
123
+ });
124
+ animate();
125
+ </script>
126
  </body>
127
+ </html>