awacke1 commited on
Commit
e011b85
·
1 Parent(s): 321b959

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +183 -135
index.html CHANGED
@@ -1,142 +1,190 @@
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
- #score {
10
- position: absolute;
11
- top: 0;
12
- left: 50%;
13
- transform: translateX(-50%);
14
- font-size: 24px;
15
- font-weight: bold;
16
- color: white;
17
- text-shadow: 1px 1px 1px black;
18
- }
19
- </style>
20
- <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
 
 
 
 
 
 
 
21
  </head>
22
  <body>
23
- <canvas id="gameCanvas"></canvas>
24
- <div id="score">Score: 0</div>
25
- <script>
26
- var scene = new THREE.Scene();
27
- var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
28
- camera.position.set(0, 30, 0);
29
- camera.lookAt(new THREE.Vector3(0, 0, 0));
30
- scene.add(camera);
31
- var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
32
- renderer.setClearColor(0x000000);
33
- renderer.setPixelRatio(window.devicePixelRatio);
34
- renderer.setSize(window.innerWidth, window.innerHeight);
35
- var ballGeometry = new THREE.SphereGeometry(0.5, 32, 32);
36
- var ballMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
37
- var balls = [];
38
- var brickGeometry = new THREE.BoxGeometry(2, 1, 1);
39
- var bricks = [];
40
- var score = 0;
41
- var scoreElement = document.getElementById('score');
42
- function addBall() {
43
- var ball = new THREE.Mesh(ballGeometry, ballMaterial);
44
- ball.position.set((Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25);
45
- ball.velocity = new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
46
- scene.add(ball);
47
- balls.push(ball);
48
- }
49
- function addBrick() {
50
- var brickMaterial = new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff });
51
- var brick = new THREE.Mesh(brickGeometry, brickMaterial);
52
- brick.position.set((Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50);
53
- scene.add(brick);
54
- bricks.push(brick);
55
- }
56
- for (var i = 0; i < 50; i++) {
57
- addBrick();
58
- }
59
- var light = new THREE.PointLight(0xffffff, 1, 100);
60
- light.position.set(0, 30, 0);
61
- scene.add(light);
62
- var paddleGeometry = new THREE.BoxGeometry(4, 1, 1);
63
- var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
64
- var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
65
- scene.add(paddle);
66
- function animate() {
67
- requestAnimationFrame(animate);
68
- var paddleSpeed = 1.5;
69
- if (keys["87"] || keys["38"]) { // w or up arrow
70
- paddle.position.z -= paddleSpeed;
71
- }
72
- if (keys["83"] || keys["40"]) {
73
- paddle.position.z += paddleSpeed;
74
- }
75
- if (keys["65"] || keys["37"]) { // a or left arrow
76
- paddle.position.x -= paddleSpeed;
77
- }
78
- if (keys["68"] || keys["39"]) { // d or right arrow
79
- paddle.position.x += paddleSpeed;
80
- }
81
- var mouseSpeed = 0.1;
82
- if (mouseX !== null && mouseY !== null) {
83
- paddle.position.x = (mouseX / window.innerWidth) * 50 - 25;
84
- paddle.position.z = -(mouseY / window.innerHeight) * 50 + 25;
85
- }
86
- for (var i = 0; i < balls.length; i++) {
87
- balls[i].position.add(balls[i].velocity);
88
- if (balls[i].position.x + 0.5 > 25 || balls[i].position.x - 0.5 < -25) {
89
- balls[i].velocity.x = -balls[i].velocity.x;
90
- }
91
- if (balls[i].position.y + 0.5 > 25 || balls[i].position.y - 0.5 < -25) {
92
- balls[i].velocity.y = -balls[i].velocity.y;
93
- }
94
- if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
95
- balls[i].velocity.z = -balls[i].velocity.z;
96
- }
97
- 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) {
98
- balls[i].velocity.y = Math.abs(balls[i].velocity.y);
99
- }
100
- for (var j = 0; j < bricks.length; j++) {
101
- if (balls[i].position.distanceTo(bricks[j].position) <= 2) {
102
- scene.remove(bricks[j]);
103
- bricks.splice(j, 1);
104
- balls[i].velocity.negate();
105
- addBrick();
106
- score++;
107
- scoreElement.innerHTML = "Score: " + score;
108
- }
109
- }
110
- }
111
- renderer.render(scene, camera);
112
- }
113
- var keys = {};
114
- var mouseX = null;
115
- var mouseY = null;
116
- document.addEventListener('mousedown', function(event) {
117
- if (event.button === 0) {
118
- addBrick();
119
- }
120
- if (event.button === 2) {
121
- addBall();
122
- }
123
- });
124
- document.addEventListener('keydown', function(event) {
125
- keys[event.keyCode] = true;
126
- });
127
- document.addEventListener('keyup', function(event) {
128
- keys[event.keyCode] = false;
129
- });
130
- document.addEventListener('mousemove', function(event) {
131
- mouseX = event.clientX;
132
- mouseY = event.clientY;
133
- });
134
- document.addEventListener('mouseleave', function(event) {
135
- mouseX = null;
136
- mouseY =null;
137
- });
138
- animate();
139
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
 
 
141
  </body>
142
- </html>
 
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <title>🤓🕹️📱 3D Breakout Game</title>
6
+ <style>
7
+ body {
8
+ margin: 0;
9
+ }
10
+
11
+ canvas {
12
+ width: 100%;
13
+ height: 100%;
14
+ }
15
+
16
+ #score {
17
+ position: absolute;
18
+ top: 0;
19
+ left: 50%;
20
+ transform: translateX(-50%);
21
+ font-size: 24px;
22
+ font-weight: bold;
23
+ color: white;
24
+ text-shadow: 1px 1px 1px black;
25
+ }
26
+ </style>
27
+ <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
28
  </head>
29
  <body>
30
+ <canvas id="gameCanvas"></canvas>
31
+ <div id="score">Score: 0</div>
32
+ <script>
33
+ var scene = new THREE.Scene();
34
+ var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
35
+ camera.position.set(0, 30, 0);
36
+ camera.lookAt(new THREE.Vector3(0, 0, 0));
37
+ scene.add(camera);
38
+
39
+ var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
40
+ renderer.setClearColor(0x000000);
41
+ renderer.setPixelRatio(window.devicePixelRatio);
42
+ renderer.setSize(window.innerWidth, window.innerHeight);
43
+
44
+ var ballGeometry = new THREE.SphereGeometry(0.5, 32, 32);
45
+ var ballMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
46
+ var balls = [];
47
+
48
+ var brickGeometry = new THREE.BoxGeometry(2, 1, 1);
49
+ var bricks = [];
50
+
51
+ var score = 0;
52
+ var scoreElement = document.getElementById('score');
53
+
54
+ function addBall() {
55
+ var ball = new THREE.Mesh(ballGeometry, ballMaterial);
56
+ ball.position.set((Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25);
57
+ ball.velocity = new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
58
+ scene.add(ball);
59
+ balls.push(ball);
60
+ }
61
+
62
+ function addBrick() {
63
+ var brickMaterial = new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff });
64
+ var brick = new THREE.Mesh(brickGeometry, brickMaterial);
65
+ brick.position.set((Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50, (Math.random() - 0.5) * 50);
66
+ scene.add(brick);
67
+ bricks.push(brick);
68
+ }
69
+
70
+ for (var i = 0; i < 50; i++) {
71
+ addBrick();
72
+ }
73
+
74
+ var light = new THREE.PointLight(0xffffff, 1, 100);
75
+ light.position.set(0, 30, 0);
76
+ scene.add(light);
77
+
78
+ var paddleGeometry = new THREE.BoxGeometry(4, 1, 1);
79
+ var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
80
+ var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);
81
+ scene.add(paddle);
82
+
83
+ function animate() {
84
+ requestAnimationFrame(animate);
85
+
86
+ var paddleSpeed = 1.5;
87
+
88
+ if (keys["87"] || keys["38"]) { // w or up arrow
89
+ paddle.position.z -= paddleSpeed;
90
+ }
91
+ if (keys["83"] || keys["40"]) {
92
+ paddle.position.z += paddleSpeed;
93
+ }
94
+ if (keys["65"] || keys["37"]) { // a or left arrow
95
+ paddle.position.x -= paddleSpeed;
96
+ }
97
+ if (keys["68"] || keys["39"]) { // d or right arrow
98
+ paddle.position.x += paddleSpeed;
99
+ }
100
+
101
+ var mouseSpeed = 0.1;
102
+
103
+ if (mouseX !== null && mouseY !== null) {
104
+ paddle.position.x = (mouseX / window.innerWidth) * 50 - 25;
105
+ paddle.position.z = -(mouseY / window.innerHeight) * 50 + 25;
106
+ }
107
+
108
+ for (var i = 0; i < balls.length; i++) {
109
+ balls[i].position.add(balls[i].velocity);
110
+
111
+ if (balls[i].position.x + 0.5 > 25 || balls[i].position.x - 0.5 < -25) {
112
+ balls[i].velocity.x = -balls[i].velocity.x;
113
+ }
114
+ if (balls[i].position.y + 0.5 > 25 || balls[i].position.y - 0.5 < -25) {
115
+ balls[i].velocity.y = -balls[i].velocity.y;
116
+ }
117
+ if (balls[i].position.z + 0.5 > 25 || balls[i].position.z - 0.5 < -25) {
118
+ balls[i].velocity.z = -balls[i].velocity.z;
119
+ }
120
+
121
+ 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) {
122
+ balls[i].velocity.y = Math.abs(balls[i].velocity.y);
123
+ }
124
+
125
+ for (var j = 0; j < bricks.length; j++) {
126
+ if (balls[i].position.distanceTo(bricks[j].position) <= 2) {
127
+ scene.remove(bricks[j]);
128
+ bricks.splice(j, 1);
129
+ balls[i].velocity.negate();
130
+ addBrick();
131
+ score++;
132
+ scoreElement.innerHTML = "Score: " + score;
133
+ }
134
+ }
135
+ }
136
+
137
+ renderer.render(scene, camera);
138
+ }
139
+
140
+ var keys = {};
141
+ var mouseX = null;
142
+ var mouseY = null;
143
+
144
+ document.addEventListener('mousedown', function(event) {
145
+ if (event.button === 0) {
146
+ addBrick();
147
+ }
148
+ if (event.button === 2) {
149
+ addBall();
150
+ }
151
+ });
152
+
153
+ document.addEventListener('keydown', function(event) {
154
+ keys[event.keyCode] = true;
155
+ if (event.keyCode >= 65 && event.keyCode <= 90) {
156
+ var letter = String.fromCharCode(event.keyCode).toLowerCase();
157
+ var letterGeometry = new THREE.TextGeometry(letter, {
158
+ font: new THREE.FontLoader().load('https://threejs.org/examples/fonts/helvetiker_regular.typeface.json'),
159
+ size: 1,
160
+ height: 0.5,
161
+ curveSegments: 12,
162
+ bevelEnabled: false
163
+ });
164
+ var letterMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
165
+ var letterMesh = new THREE.Mesh(letterGeometry, letterMaterial);
166
+ letterMesh.position.set((Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25, (Math.random() - 0.5) * 25);
167
+ letterMesh.velocity = new THREE.Vector3((Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2, (Math.random() - 0.5) * 2).normalize();
168
+ scene.add(letterMesh);
169
+ balls.push(letterMesh);
170
+ }
171
+ });
172
+
173
+ document.addEventListener('keyup', function(event) {
174
+ keys[event.keyCode] = false;
175
+ });
176
+
177
+ document.addEventListener('mousemove', function(event) {
178
+ mouseX = event.clientX;
179
+ mouseY = event.clientY;
180
+ });
181
+
182
+ document.addEventListener('mouseleave', function(event) {
183
+ mouseX = null;
184
+ mouseY = null;
185
+ });
186
 
187
+ animate();
188
+ </script>
189
  </body>
190
+ </html>