Update index.html
Browse files- index.html +183 -135
index.html
CHANGED
@@ -1,142 +1,190 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
</head>
|
22 |
<body>
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
paddle.
|
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 |
-
balls[i].
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
}
|
110 |
-
|
111 |
-
|
112 |
-
}
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
}
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
}
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>
|