Update index.html
Browse files- index.html +139 -16
index.html
CHANGED
@@ -1,19 +1,142 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
</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 |
+
#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>
|