Sure! Below is the modified version of your Simple Minecraft Clone with the following changes:
html
<!DOCTYPE html>
<html>
<head>
<title>Simple Minecraft Clone</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
width: 100vw;
height: 100vh;
display: block;
}
#instructions {
position: fixed;
top: 10px;
left: 10px;
color: white;
font-family: Arial, sans-serif;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
z-index: 1;
}
</style>
</head>
<body>
<div id="instructions">
WASD - Move<br>
SPACE - Jump<br>
LEFT CLICK - Remove block<br>
RIGHT CLICK - Place block<br>
MOUSE - Look around
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Initialize Three.js
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB); // Sky blue background
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add ambient light for better visibility
const ambientLight = new THREE.AmbientLight(0xffffff, 0.7);
scene.add(ambientLight);
// Add directional light to simulate sunlight
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(10, 20, 10);
scene.add(directionalLight);
// Create grid of blocks
const blocks = new THREE.Group();
const blockGeometry = new THREE.BoxGeometry(1, 1, 1);
const blockMaterial = new THREE.MeshLambertMaterial({ color: 0x00ff00 }); // Green blocks
// Create ground
for (let x = -10; x <= 10; x++) {
for (let z = -10; z <= 10; z++) {
const block = new THREE.Mesh(blockGeometry, blockMaterial);
block.position.set(x, -1, z);
blocks.add(block);
}
}
scene.add(blocks);
// Player movement variables
const player = {
position: new THREE.Vector3(0, 0, 5),
velocity: new THREE.Vector3(),
onGround: false,
speed: 0.2,
jumpForce: 0.6
};
// Camera controls
let pitch = 0;
let yaw = 0;
// Movement controls
const keys = {};
// Raycaster for block placement/removal
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
// Event listeners for keyboard
document.addEventListener('keydown', (e) => {
keys[e.key.toLowerCase()] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.key.toLowerCase()] = false;
});
// Prevent context menu on right click
document.addEventListener('contextmenu', (e) => e.preventDefault());
// Event listener for mouse down
document.addEventListener('mousedown', (e) => {
// Get mouse position
mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(blocks.children, false);
if (intersects.length > 0) {
const intersect = intersects[0];
if (e.button === 0) { // Left click - remove block
blocks.remove(intersect.object);
} else if (e.button === 2) { // Right click - add block
const normal = intersect.face.normal;
const position = intersect.point.add(normal.multiplyScalar(0.5));
const newBlockPos = new THREE.Vector3(
Math.round(position.x),
Math.round(position.y),
Math.round(position.z)
);
// Check if block already exists at the position
const exists = blocks.children.some(block =>
block.position.equals(newBlockPos)
);
if (!exists) {
const block = new THREE.Mesh(blockGeometry, blockMaterial);
block.position.copy(newBlockPos);
blocks.add(block);
}
}
}
});
// Pointer lock for mouse control
renderer.domElement.addEventListener('click', () => {
renderer.domElement.requestPointerLock();
});
document.addEventListener('mousemove', (e) => {
if (document.pointerLockElement === renderer.domElement) {
const sensitivity = 0.002;
yaw -= e.movementX * sensitivity;
pitch -= e.movementY * sensitivity;
// Limit pitch between -90 and 90 degrees
pitch = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, pitch));
}
});
// Player gravity and collision variables
const GRAVITY = -0.02;
const MOVE_SPEED = 0.1;
const JUMP_SPEED = 0.3;
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Handle movement
const direction = new THREE.Vector3();
if (keys['w']) direction.z -= 1;
if (keys['s']) direction.z += 1;
if (keys['a']) direction.x -= 1;
if (keys['d']) direction.x += 1;
direction.normalize();
if (direction.length() > 0) {
const moveDistance = MOVE_SPEED;
player.position.x += (Math.cos(yaw) * direction.x - Math.sin(yaw) * direction.z) * moveDistance;
player.position.z += (Math.sin(yaw) * direction.x + Math.cos(yaw) * direction.z) * moveDistance;
}
// Jump
if (keys[' '] && player.onGround) {
player.velocity.y = player.jumpForce;
player.onGround = false;
}
// Apply gravity
player.velocity.y += GRAVITY;
player.position.y += player.velocity.y;
// Simple ground collision
if (player.position.y < 0) {
player.position.y = 0;
player.velocity.y = 0;
player.onGround = true;
}
// Update camera position and rotation
camera.position.copy(player.position);
camera.position.y += 1.6; // Eye height
camera.rotation.order = 'YXZ';
camera.rotation.y = yaw;
camera.rotation.x = pitch;
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>
Feel free to further customize and enhance the clone to fit your vision. Enjoy building!