mc / index.html
Bronze47's picture
Add 3 files
dca6436 verified
Raw History Blame Contribute Delete
20.4 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BlockCraft - Minecraft-like Game</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/PointerLockControls.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
font-family: 'Minecraft', sans-serif;
}
canvas {
display: block;
}
#crosshair {
position: absolute;
top: 50%;
left: 50%;
width: 10px;
height: 10px;
transform: translate(-50%, -50%);
background-color: white;
border: 1px solid black;
pointer-events: none;
}
#ui {
position: absolute;
top: 10px;
left: 10px;
color: white;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
font-size: 16px;
}
#inventory {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
background-color: rgba(0, 0, 0, 0.5);
padding: 5px;
border-radius: 5px;
}
.inventory-slot {
width: 40px;
height: 40px;
margin: 0 5px;
border: 2px solid rgba(255, 255, 255, 0.2);
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.inventory-slot.active {
border-color: white;
}
#start-screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
z-index: 100;
}
#start-button {
margin-top: 20px;
padding: 10px 30px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
font-size: 18px;
cursor: pointer;
}
#start-button:hover {
background-color: #45a049;
}
@font-face {
font-family: 'Minecraft';
src: url('https://fonts.cdnfonts.com/css/minecraft-4');
}
</style>
</head>
<body>
<div id="crosshair"></div>
<div id="ui">
<div>BlockCraft</div>
<div id="position">Position: (0, 0, 0)</div>
<div id="fps">FPS: 0</div>
</div>
<div id="inventory">
<div class="inventory-slot active" data-block="grass">🌿</div>
<div class="inventory-slot" data-block="dirt">🟫</div>
<div class="inventory-slot" data-block="stone">🪨</div>
<div class="inventory-slot" data-block="wood">🪵</div>
<div class="inventory-slot" data-block="sand">🟨</div>
</div>
<div id="start-screen">
<h1 style="font-size: 48px; margin-bottom: 20px;">BLOCKCRAFT</h1>
<p style="font-size: 18px; margin-bottom: 30px;">A Minecraft-like 3D world</p>
<button id="start-button">START GAME</button>
<div style="margin-top: 40px; font-size: 14px; color: #aaa;">
Controls: WASD to move, SPACE to jump, LEFT CLICK to break, RIGHT CLICK to place
</div>
</div>
<script>
// Game variables
let scene, camera, renderer, controls;
let world = {};
let selectedBlock = 'grass';
let clock = new THREE.Clock();
let lastTime = 0;
let fps = 0;
let raycaster = new THREE.Raycaster();
let mouse = new THREE.Vector2();
let isJumping = false;
let velocityY = 0;
const gravity = -0.02;
const jumpForce = 0.15;
const blockSize = 1;
const worldSize = 32;
const renderDistance = 16;
// Initialize the game
function init() {
// Create scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB); // Sky blue
// Add ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
// Add directional light (sun)
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(100, 100, 50);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// Create camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.y = 2;
// Create renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Add pointer lock controls
controls = new THREE.PointerLockControls(camera, document.body);
// Generate world
generateWorld();
// Add event listeners
setupEventListeners();
// Start game loop
animate();
}
// Generate the world
function generateWorld() {
// Create a group for all blocks
const worldGroup = new THREE.Group();
scene.add(worldGroup);
// Generate terrain
for (let x = -worldSize; x < worldSize; x++) {
for (let z = -worldSize; z < worldSize; z++) {
// Generate height using simple noise
const height = Math.floor(Math.random() * 3) + 1;
// Create ground layers
for (let y = 0; y <= height; y++) {
let blockType;
if (y === height) {
blockType = 'grass';
} else if (y >= height - 2) {
blockType = 'dirt';
} else {
blockType = 'stone';
}
addBlock(x, y, z, blockType);
}
// Add some random trees
if (Math.random() < 0.02 && height > 1) {
// Tree trunk
for (let y = height + 1; y <= height + 4; y++) {
addBlock(x, y, z, 'wood');
}
// Tree leaves
for (let dx = -2; dx <= 2; dx++) {
for (let dz = -2; dz <= 2; dz++) {
for (let dy = 0; dy < 3; dy++) {
if ((Math.abs(dx) !== 2 || Math.abs(dz) !== 2) &&
(dy !== 2 || (Math.abs(dx) + Math.abs(dz) <= 1))) {
addBlock(x + dx, height + 5 + dy, z + dz, 'leaves');
}
}
}
}
}
// Add some sand near water level
if (height === 0 && Math.random() < 0.7) {
addBlock(x, 0, z, 'sand');
}
}
}
// Add water at y=0
for (let x = -worldSize; x < worldSize; x++) {
for (let z = -worldSize; z < worldSize; z++) {
if (!world[`${x},0,${z}`]) {
addBlock(x, -1, z, 'water');
}
}
}
}
// Add a block to the world
function addBlock(x, y, z, type) {
const key = `${x},${y},${z}`;
// Don't add if block already exists
if (world[key]) return;
// Create block geometry
const geometry = new THREE.BoxGeometry(blockSize, blockSize, blockSize);
// Set material based on block type
let material;
switch (type) {
case 'grass':
material = [
new THREE.MeshStandardMaterial({ color: 0x7CFC00 }), // Top (green)
new THREE.MeshStandardMaterial({ color: 0x8B4513 }), // Bottom (dirt)
new THREE.MeshStandardMaterial({ color: 0x556B2F }), // Side (grass side)
new THREE.MeshStandardMaterial({ color: 0x556B2F }), // Side
new THREE.MeshStandardMaterial({ color: 0x556B2F }), // Side
new THREE.MeshStandardMaterial({ color: 0x556B2F }) // Side
];
break;
case 'dirt':
material = new THREE.MeshStandardMaterial({ color: 0x8B4513 });
break;
case 'stone':
material = new THREE.MeshStandardMaterial({ color: 0x808080 });
break;
case 'wood':
material = [
new THREE.MeshStandardMaterial({ color: 0x8B4513 }), // Top/bottom
new THREE.MeshStandardMaterial({ color: 0x8B4513 }),
new THREE.MeshStandardMaterial({ color: 0xA0522D }), // Side (darker)
new THREE.MeshStandardMaterial({ color: 0xA0522D }),
new THREE.MeshStandardMaterial({ color: 0xA0522D }),
new THREE.MeshStandardMaterial({ color: 0xA0522D })
];
break;
case 'sand':
material = new THREE.MeshStandardMaterial({ color: 0xF4A460 });
break;
case 'leaves':
material = new THREE.MeshStandardMaterial({ color: 0x2E8B57, transparent: true, opacity: 0.8 });
break;
case 'water':
material = new THREE.MeshStandardMaterial({
color: 0x1E90FF,
transparent: true,
opacity: 0.7,
side: THREE.DoubleSide
});
break;
default:
material = new THREE.MeshStandardMaterial({ color: 0xFF0000 }); // Red for unknown blocks
}
// Create mesh
const block = new THREE.Mesh(
geometry,
Array.isArray(material) ? material : [material, material, material, material, material, material]
);
// Position the block
block.position.set(x * blockSize, y * blockSize, z * blockSize);
block.castShadow = true;
block.receiveShadow = true;
// Add to scene
scene.add(block);
// Store in world object
world[key] = {
mesh: block,
type: type,
x: x,
y: y,
z: z
};
}
// Remove a block from the world
function removeBlock(x, y, z) {
const key = `${x},${y},${z}`;
if (world[key]) {
scene.remove(world[key].mesh);
delete world[key];
return true;
}
return false;
}
// Get block at position
function getBlock(x, y, z) {
const key = `${x},${y},${z}`;
return world[key] || null;
}
// Setup event listeners
function setupEventListeners() {
// Start button
document.getElementById('start-button').addEventListener('click', () => {
document.getElementById('start-screen').style.display = 'none';
controls.lock();
});
// Window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Mouse click for block interaction
document.addEventListener('click', (event) => {
if (controls.isLocked) {
// Left click to remove block
if (event.button === 0) {
removeTargetedBlock();
}
// Right click to place block
else if (event.button === 2) {
placeBlockInFront();
}
}
});
// Keyboard controls
const keys = {};
document.addEventListener('keydown', (event) => {
keys[event.code] = true;
// Space to jump
if (event.code === 'Space' && !isJumping) {
velocityY = jumpForce;
isJumping = true;
}
});
document.addEventListener('keyup', (event) => {
keys[event.code] = false;
});
// Inventory selection
document.querySelectorAll('.inventory-slot').forEach(slot => {
slot.addEventListener('click', () => {
document.querySelectorAll('.inventory-slot').forEach(s => {
s.classList.remove('active');
});
slot.classList.add('active');
selectedBlock = slot.dataset.block;
});
});
// Handle movement in game loop
function handleMovement() {
if (controls.isLocked) {
const delta = clock.getDelta();
const moveSpeed = 0.2;
// Forward/backward
if (keys['KeyW']) {
controls.moveForward(moveSpeed);
}
if (keys['KeyS']) {
controls.moveForward(-moveSpeed);
}
// Left/right
if (keys['KeyA']) {
controls.moveRight(-moveSpeed);
}
if (keys['KeyD']) {
controls.moveRight(moveSpeed);
}
// Apply gravity and jumping
camera.position.y += velocityY;
velocityY += gravity;
// Check if player is on ground
const playerX = Math.floor(camera.position.x);
const playerY = Math.floor(camera.position.y - 0.5);
const playerZ = Math.floor(camera.position.z);
const blockBelow = getBlock(playerX, playerY, playerZ);
if (blockBelow) {
camera.position.y = playerY + 1.5;
velocityY = 0;
isJumping = false;
}
// Prevent falling through world
if (camera.position.y < -10) {
camera.position.y = 10;
velocityY = 0;
}
// Update position display
document.getElementById('position').textContent =
`Position: (${camera.position.x.toFixed(1)}, ${camera.position.y.toFixed(1)}, ${camera.position.z.toFixed(1)})`;
}
}
// Add movement handler to animation loop
clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
handleMovement();
renderer.render(scene, camera);
// Calculate FPS
const time = performance.now();
if (time >= lastTime + 1000) {
fps = Math.round(1000 / (time - lastTime) * 100) / 100;
document.getElementById('fps').textContent = `FPS: ${fps}`;
lastTime = time;
}
}
}
// Remove the block the player is looking at
function removeTargetedBlock() {
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(Object.values(world).map(b => b.mesh));
if (intersects.length > 0) {
const block = intersects[0].object;
const position = block.position;
const x = Math.round(position.x);
const y = Math.round(position.y);
const z = Math.round(position.z);
removeBlock(x, y, z);
}
}
// Place a block in front of the player
function placeBlockInFront() {
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(Object.values(world).map(b => b.mesh));
if (intersects.length > 0) {
const point = intersects[0].point;
const normal = intersects[0].face.normal;
// Calculate position for new block
const x = Math.round(point.x + normal.x * 0.5);
const y = Math.round(point.y + normal.y * 0.5);
const z = Math.round(point.z + normal.z * 0.5);
// Don't place block inside player
const playerX = Math.floor(camera.position.x);
const playerY = Math.floor(camera.position.y);
const playerZ = Math.floor(camera.position.z);
if (Math.abs(x - playerX) > 1 || y !== playerY || Math.abs(z - playerZ) > 1) {
addBlock(x, y, z, selectedBlock);
}
}
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Update raycaster for block targeting
raycaster.setFromCamera(mouse, camera);
renderer.render(scene, camera);
// Calculate FPS
const time = performance.now();
if (time >= lastTime + 1000) {
fps = Math.round(1000 / (time - lastTime) * 100) / 100;
document.getElementById('fps').textContent = `FPS: ${fps}`;
lastTime = time;
}
}
// Start the game when the page loads
window.addEventListener('load', init);
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Bronze47/mc" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>