MINECRAFT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BlockCraft - Survival World</title>
<style>
*{box-sizing:border-box}
body{margin:0;overflow:hidden;background:#78b9e8;font-family:Arial;color:white}
canvas{display:block;width:100vw;height:100vh;image-rendering:pixelated}
#hud{position:absolute;top:10px;left:10px;background:#0009;padding:10px;border:2px solid #aaa;min-width:180px}
#bars{display:flex;gap:4px;margin:7px 0}
.bar{height:12px;background:#444;flex:1}
#health{background:#e43c3c;width:100%}
#hunger{background:#d9a34a;width:100%}
#hotbar{position:absolute;bottom:15px;left:50%;transform:translateX(-50%);display:flex;gap:4px}
.slot{width:52px;height:52px;background:#555b;border:3px solid #999;display:flex;align-items:center;justify-content:center;flex-direction:column;font-size:11px;text-shadow:1px 1px black}
.slot.selected{border:3px solid white;background:#999b}
#message{position:absolute;top:20%;left:50%;transform:translateX(-50%);background:#000b;padding:12px;display:none}
#crosshair{position:absolute;left:50%;top:50%;font-size:25px;transform:translate(-50%,-50%);pointer-events:none}
#menu,#inventory,#crafting,#dead{position:absolute;inset:0;background:#000b;display:none;align-items:center;justify-content:center}
.panel{background:#333e;padding:24px;border:4px solid #aaa;max-width:500px;width:90%;text-align:center;max-height:85vh;overflow:auto}
button{background:#777;color:white;border:3px solid #aaa;padding:10px;margin:4px;font-weight:bold;cursor:pointer}
button:hover{background:#999}
#inventoryGrid{display:grid;grid-template-columns:repeat(4,1fr);gap:5px;margin:12px 0}
.invslot{background:#555;padding:12px 3px;min-height:50px;border:2px solid #999;font-size:12px}
.recipe{background:#555;padding:8px;margin:5px;border:2px solid #aaa}
#help{position:absolute;right:10px;top:10px;background:#0008;padding:10px;font-size:12px}
</style>
</head>
<body>
<canvas id="world"></canvas>
<div id="crosshair">+</div>
<div id="hud">
<b>BLOCKCRAFT</b>
<div id="bars">
<div class="bar"><div id="health" style="height:100%"></div></div>
<div class="bar"><div id="hunger" style="height:100%"></div></div>
</div>
<div>❤️ <span id="hptext">100</span></div>
<div>🍗 <span id="foodtext">100</span></div>
<div>Day: <span id="day">1</span></div>
<div>Time: <span id="time">Morning</span></div>
</div>
<div id="help">
WASD: Move<br>
Mouse: Look<br>
Left click: Mine / Attack<br>
Right click: Place block<br>
1-9: Select item<br>
E: Inventory<br>
C: Crafting<br>
ESC: Release mouse
</div>
<div id="message"></div>
<div id="hotbar"></div>
<div id="menu">
<div class="panel">
<h1>BLOCKCRAFT</h1>
<h3>Survival World</h3>
<p>Explore, mine, craft, build, and survive.</p>
<button onclick="newGame()">NEW WORLD</button>
<button onclick="loadGame()">LOAD WORLD</button>
<button onclick="closeMenu()">RESUME</button>
</div>
</div>
<div id="inventory">
<div class="panel">
<h2>INVENTORY</h2>
<div id="inventoryGrid"></div>
<p>Select a hotbar slot with number keys 1-9.</p>
<button onclick="closeInventory()">CLOSE</button>
</div>
</div>
<div id="crafting">
<div class="panel">
<h2>CRAFTING</h2>
<div id="recipes"></div>
<button onclick="closeCrafting()">CLOSE</button>
</div>
</div>
<div id="dead">
<div class="panel">
<h1>You Died!</h1>
<p>Your survival journey has ended.</p>
<button onclick="respawn()">RESPAWN</button>
</div>
</div>
<script>
"use strict";
const canvas=document.getElementById("world");
const ctx=canvas.getContext("2d");
let W,H;
function resize(){
W=canvas.width=innerWidth;
H=canvas.height=innerHeight;
}
resize();
window.addEventListener("resize",resize);
const SIZE=18,HEIGHT=12,DEPTH=18;
let blocks={},inventory={},mobs=[],keys={};
let px=9,py=5,pz=9,yaw=0,pitch=0;
let selected=0,hp=100,food=100,day=1,timeOfDay=0;
let running=false,paused=false,dead=false;
let miningTarget=null,miningProgress=0,lastHunger=0;
let lastTime=performance.now();
const names={
grass:"Grass",dirt:"Dirt",stone:"Stone",wood:"Wood",
leaves:"Leaves",sand:"Sand",coal:"Coal Ore",
iron:"Iron Ore",diamond:"Diamond Ore",
planks:"Wood Planks",stick:"Stick",
crafting:"Crafting Table",furnace:"Furnace",
woodPick:"Wooden Pickaxe",stonePick:"Stone Pickaxe",
woodAxe:"Wooden Axe",stoneAxe:"Stone Axe",
woodSword:"Wooden Sword",stoneSword:"Stone Sword",
apple:"Apple"
};
const colors={
grass:"#58a83d",dirt:"#8b5731",stone:"#888888",
wood:"#8b572f",leaves:"#287b32",sand:"#d9c47d",
coal:"#454545",iron:"#c18b73",diamond:"#36e5e8",
planks:"#b17a43",stick:"#a77b46",
crafting:"#80502c",furnace:"#555",
woodPick:"#bd8c54",stonePick:"#888",
woodAxe:"#bd8c54",stoneAxe:"#888",
woodSword:"#bd8c54",stoneSword:"#888",apple:"#d83232"
};
function key(x,y,z){return x+","+y+","+z}
function getBlock(x,y,z){return blocks[key(x,y,z)]||null}
function setBlock(x,y,z,b){
if(x<0||x>=SIZE||z<0||z>=DEPTH||y<0||y>=HEIGHT)return;
if(b)blocks[key(x,y,z)]=b;
else delete blocks[key(x,y,z)];
}
function addItem(name,count=1){
inventory[name]=(inventory[name]||0)+count;
updateUI();
}
function takeItem(name,count=1){
if((inventory[name]||0)<count)return false;
inventory[name]-=count;
if(inventory[name]<=0)delete inventory[name];
updateUI();
return true;
}
function makeTerrain(){
blocks={};
inventory={};
mobs=[];
for(let x=0;x<SIZE;x++){
for(let z=0;z<DEPTH;z++){
let h=3+Math.floor(Math.sin(x*.5)*1.2+Math.cos(z*.4)*1.2);
for(let y=0;y<=h;y++){
let b=y===h?"grass":y>=h-2?"dirt":"stone";
setBlock(x,y,z,b);
}
for(let y=0;y<h-2;y++){
if(Math.random()<.045)setBlock(x,y,z,"coal");
if(Math.random()<.025)setBlock(x,y,z,"iron");
if(y<2&&Math.random()<.012)setBlock(x,y,z,"diamond");
}
}
}
// Trees
for(let i=0;i<12;i++){
let x=2+Math.floor(Math.random()*14);
let z=2+Math.floor(Math.random()*14);
let y=topY(x,z)+1;
for(let j=0;j<4;j++)setBlock(x,y+j,z,"wood");
for(let dx=-2;dx<=2;dx++){
for(let dz=-2;dz<=2;dz++){
for(let dy=2;dy<=4;dy++){
if(Math.abs(dx)+Math.abs(dz)+Math.abs(dy-3)<5&&
!getBlock(x+dx,y+dy-1,z+dz)){
setBlock(x+dx,y+dy-1,z+dz,"leaves");
}
}
}
}
}
px=9;pz=9;py=topY(9,9)+1;
addItem("apple",3);
addItem("woodPick",1);
}
function topY(x,z){
for(let y=HEIGHT-1;y>=0;y--){
if(getBlock(x,y,z))return y;
}
return 0;
}
function newGame(){
makeTerrain();
hp=100;food=100;day=1;timeOfDay=0;
document.getElementById("menu").style.display="none";
running=true;paused=false;dead=false;
updateUI();
say("Welcome to your new world!");
}
function updateUI(){
document.getElementById("hptext").textContent=Math.ceil(hp);
document.getElementById("foodtext").textContent=Math.ceil(food);
document.getElementById("health").style.width=hp+"%";
document.getElementById("hunger").style.width=food+"%";
document.getElementById("day").textContent=day;
document.getElementById("time").textContent=
timeOfDay<.25?"Morning":timeOfDay<.5?"Afternoon":
timeOfDay<.75?"Evening":"Night";
let hot=document.getElementById("hotbar");
hot.innerHTML="";
let list=Object.keys(inventory);
for(let i=0;i<9;i++){
let name=list[i];
let div=document.createElement("div");
div.className="slot"+(selected===i?" selected":"");
div.innerHTML=name?names[name]+"<br>"+inventory[name]:String(i+1);
hot.appendChild(div);
}
}
function say(t){
let m=document.getElementById("message");
m.textContent=t;m.style.display="block";
setTimeout(()=>m.style.display="none",1800);
}
function project(x,y,z){
let dx=x-px,dy=y-py,dz=z-pz;
let rx=dx*Math.cos(yaw)-dz*Math.sin(yaw);
let rz=dx*Math.sin(yaw)+dz*Math.cos(yaw);
let ry=dy*Math.cos(pitch)-rz*Math.sin(pitch);
rz=dy*Math.sin(pitch)+rz*Math.cos(pitch);
if(rz<.2)return null;
let scale=Math.min(W,H)*.85/rz;
return {x:W/2+rx*scale,y:H/2-ry*scale,scale:scale,depth:rz};
}
function drawBlock(x,y,z,type){
let p=project(x+.5,y+.5,z+.5);
if(!p)return;
let s=Math.min(130,p.scale*.48);
if(s<2)return;
let top=project(x+.5,y+1,z+.5);
let left=project(x,y+.5,z+.5);
let right=project(x+1,y+.5,z+.5);
if(!top||!left||!right)return;
let half=s*.5;
let side=s*.9;
ctx.fillStyle=colors[type]||"#777";
ctx.fillRect(p.x-half,p.y-half,s,s);
// Block shading
ctx.fillStyle="rgba(0,0,0,.15)";
ctx.fillRect(p.x-half,p.y,s,half);
ctx.strokeStyle="#0004";
ctx.lineWidth=Math.max(1,s*.025);
ctx.strokeRect(p.x-half,p.y-half,s,s);
// Pixel texture
if(s>12){
ctx.fillStyle="#ffffff18";
for(let i=0;i<4;i++){
ctx.fillRect(p.x-half+Math.random()*s,p.y-half+Math.random()*s,2,2);
}
}
}
function drawWorld(){
let sky=ctx.createLinearGradient(0,0,0,H);
let night=timeOfDay>.72||timeOfDay<.15;
sky.addColorStop(0,night?"#101a39":"#61a9d8");
sky.addColorStop(1,night?"#33384e":"#c6e3ec");
ctx.fillStyle=sky;
ctx.fillRect(0,0,W,H);
// Draw far blocks first
let list=[];
for(let k in blocks){
let a=k.split(",").map(Number);
let p=project(a[0],a[1],a[2]);
if(p&&p.depth<35)list.push({x:a[0],y:a[1],z:a[2],b:blocks[k],d:p.depth});
}
list.sort((a,b)=>b.d-a.d);
list.forEach(v=>drawBlock(v.x,v.y,v.z,v.b));
// Target marker
let target=rayTarget();
if(target){
let p=project(target.x+.5,target.y+.5,target.z+.5);
if(p){
let s=Math.min(130,p.scale*.48);
ctx.strokeStyle="white";ctx.lineWidth=3;
ctx.strokeRect(p.x-s*.55,p.y-s*.55,s*1.1,s*1.1);
}
}
drawMobs();
drawHand();
}
function drawMobs(){
mobs.forEach(m=>{
if(!m.alive)return;
let p=project(m.x,m.y,m.z);
if(!p)return;
let s=Math.min(70,p.scale*.35);
ctx.fillStyle=m.type==="zombie"?"#277b42":"#8b3333";
ctx.fillRect(p.x-s*.5,p.y-s,s,s*1.5);
ctx.fillStyle="#d5c4a1";
ctx.fillRect(p.x-s*.35,p.y-s*.9,s*.2,s*.15);
ctx.fillRect(p.x+s*.15,p.y-s*.9,s*.2,s*.15);
ctx.fillStyle="#222";
ctx.fillRect(p.x-s*.5,p.y-s*1.2,s,s*.12);
ctx.fillStyle="#e33";
ctx.fillRect(p.x-s*.5,p.y-s*1.2,s*(m.hp/50),s*.12);
});
}
function drawHand(){
ctx.fillStyle="#8b603f";
ctx.fillRect(W/2+55,H-105,65,110);
ctx.fillStyle="#d5b18c";
ctx.fillRect(W/2+65,H-80,45,50);
let item=Object.keys(inventory)[selected];
if(item&&item.includes("Pick")){
ctx.fillStyle="#8b633f";
ctx.fillRect(W/2+20,H-150,12,100);
ctx.fillStyle=colors[item];
ctx.fillRect(W/2-5,H-155,70,13);
}
}
function rayTarget(){
let fx=-Math.sin(yaw)*Math.cos(pitch);
let fy=Math.sin(pitch);
let fz=-Math.cos(yaw)*Math.cos(pitch);
for(let d=.5;d<7;d+=.15){
let x=Math.floor(px+fx*d);
let y=Math.floor(py+fy*d);
let z=Math.floor(pz+fz*d);
let b=getBlock(x,y,z);
if(b)return {x,y,z,b};
}
return null;
}
function mine(){
if(!running||paused||dead)return;
let target=rayTarget();
if(!target){say("No block in reach!");return}
let item=Object.keys(inventory)[selected];
let required=target.b==="stone"||target.b==="coal"||target.b==="iron"||target.b==="diamond";
if(required&&item!=="woodPick"&&item!=="stonePick"){
say("You need a pickaxe to mine this!");
return;
}
let block=target.b;
setBlock(target.x,target.y,target.z,null);
let drop=block==="grass"?"dirt":block;
if(block!=="leaves")addItem(drop);
else if(Math.random()<.2)addItem("apple");
say("Mined "+names[block]);
}
function place(){
if(!running||paused||dead)return;
let item=Object.keys(inventory)[selected];
if(!item||!["dirt","stone","wood","planks","sand","crafting"].includes(item)){
say("Select a placeable block!");
return;
}
let target=rayTarget();
if(!target){say("Look at a block first!");return}
let fx=-Math.sin(yaw),fz=-Math.cos(yaw);
let x=target.x+(Math.abs(fx)>Math.abs(fz)?Math.sign(fx):0);
let z=target.z+(Math.abs(fz)>=Math.abs(fx)?Math.sign(fz):0);
let y=target.y;
if(getBlock(x,y,z)){say("Space occupied!");return}
if(x===Math.floor(px)&&z===Math.floor(pz)&&y===Math.floor(py)){
say("You cannot place a block inside yourself!");return;
}
setBlock(x,y,z,item);
takeItem(item);
}
canvas.addEventListener("mousedown",e=>{
if(!running||paused||dead)return;
if(e.button===0)mine();
if(e.button===2)place();
});
canvas.addEventListener("contextmenu",e=>e.preventDefault());
function move(dt){
let f=0,s=0;
if(keys.KeyW)f++;
if(keys.KeyS)f--;
if(keys.KeyA)s--;
if(keys.KeyD)s++;
let speed=4*dt;
px+=(-Math.sin(yaw)*f+Math.cos(yaw)*s)*speed;
pz+=(-Math.cos(yaw)*f-Math.sin(yaw)*s)*speed;
px=Math.max(0,Math.min(SIZE-.1,px));
pz=Math.max(0,Math.min(DEPTH-.1,pz));
let ground=topY(Math.floor(px),Math.floor(pz));
if(py>ground+1)py-=dt*8;
else py=ground+1;
if(keys.Space&&py<=ground+1.1)py+=dt*5;
}
function spawnMobs(){
if(mobs.length>12)return;
let x=Math.floor(Math.random()*SIZE);
let z=Math.floor(Math.random()*DEPTH);
mobs.push({x,y:topY(x,z)+1,z,hp:50,alive:true,type:"zombie",cooldown:0});
}
function updateMobs(dt){
mobs.forEach(m=>{
if(!m.alive)return;
let dx=px-m.x,dz=pz-m.z,d=Math.hypot(dx,dz);
if(d<12&&d>1.1){
m.x+=dx/d*dt*1.5;
m.z+=dz/d*dt*1.5;
}
m.cooldown-=dt;
if(d<1.5&&m.cooldown<=0){
hp-=10;m.cooldown=1;
updateUI();
if(hp<=0){
dead=true;
document.getElementById("dead").style.display="flex";
}
}
});
}
function craft(name){
const recipes={
planks:{wood:1},
stick:{planks:2},
crafting:{planks:4},
woodPick:{planks:3,stick:2},
woodAxe:{planks:3,stick:2},
woodSword:{planks:2,stick:1},
stonePick:{stone:3,stick:2},
stoneAxe:{stone:3,stick:2},
stoneSword:{stone:2,stick:1},
furnace:{stone:8}
};
let recipe=recipes[name];
if(!recipe)return;
for(let material in recipe){
if((inventory[material]||0)<recipe[material]){
say("Not enough "+names[material]);
return;
}
}
for(let material in recipe)takeItem(material,recipe[material]);
addItem(name);
say("Crafted "+names[name]);
openCrafting();
}
function openInventory(){
paused=true;
document.getElementById("inventory").style.display="flex";
let grid=document.getElementById("inventoryGrid");
grid.innerHTML="";
for(let item in inventory){
let div=document.createElement("div");
div.className="invslot";
div.textContent=names[item]+" x"+inventory[item];
grid.appendChild(div);
}
}
function closeInventory(){
document.getElementById("inventory").style.display="none";
paused=false;
}
function openCrafting(){
paused=true;
document.getElementById("crafting").style.display="flex";
let recipes=document.getElementById("recipes");
recipes.innerHTML="";
let options=["planks","stick","crafting","woodPick","woodAxe","woodSword","stonePick","stoneAxe","stoneSword","furnace"];
options.forEach(n=>{
let b=document.createElement("button");
b.className="recipe";
b.textContent="Craft "+names[n];
b.onclick=()=>craft(n);
recipes.appendChild(b);
});
}
function closeCrafting(){
document.getElementById("crafting").style.display="none";
paused=false;
}
function closeMenu(){
document.getElementById("menu").style.display="none";
paused=false;
}
function saveGame(){
try{
localStorage.setItem("blockcraftSave",JSON.stringify({
blocks,inventory,px,py,pz,yaw,hp,food,day,timeOfDay,mobs
}));
say("World saved!");
}catch(e){say("Could not save world.");}
}
function loadGame(){
try{
let d=JSON.parse(localStorage.getItem("blockcraftSave"));
if(!d){say("No saved world!");return}
blocks=d.blocks;inventory=d.inventory;
px=d.px;py=d.py;pz=d.pz;yaw=d.yaw;
hp=d.hp;food=d.food;day=d.day;timeOfDay=d.timeOfDay;
mobs=d.mobs||[];
running=true;paused=false;
document.getElementById("menu").style.display="none";
updateUI();say("World loaded!");
}catch(e){say("Save could not be loaded.");}
}
function respawn(){
hp=100;food=100;px=9;pz=9;py=topY(9,9)+1;
dead=false;
document.getElementById("dead").style.display="none";
updateUI();
}
document.addEventListener("keydown",e=>{
keys[e.code]=true;
if(e.code==="KeyE"&&!e.repeat)openInventory();
if(e.code==="KeyC"&&!e.repeat)openCrafting();
if(e.code==="Escape"){
if(document.pointerLockElement)document.exitPointerLock();
paused=true;
document.getElementById("menu").style.display="flex";
}
if(e.code.startsWith("Digit")){
let n=Number(e.code.slice(5))-1;
if(n>=0&&n<9){selected=n;updateUI()}
}
});
document.addEventListener("keyup",e=>keys[e.code]=false);
canvas.addEventListener("click",()=>{
if(running&&!paused&&!dead)canvas.requestPointerLock?.();
});
document.addEventListener("mousemove",e=>{
if(document.pointerLockElement===canvas){
yaw+=e.movementX*.002;
pitch=Math.max(-1,Math.min(1,pitch-e.movementY*.002));
}
});
function loop(now){
let dt=Math.min((now-lastTime)/1000,.05);
lastTime=now;
if(running&&!paused&&!dead){
move(dt);
timeOfDay=(timeOfDay+dt/180)%1;
if(timeOfDay<.001)day++;
if(timeOfDay>.7&&Math.random()<dt*.15)spawnMobs();
updateMobs(dt);
lastHunger+=dt;
if(lastHunger>8){
lastHunger=0;
food=Math.max(0,food-1);
if(food===0)hp=Math.max(0,hp-2);
if(hp<=0){
dead=true;
document.getElementById("dead").style.display="flex";
}
updateUI();
}
}
drawWorld();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
document.getElementById("menu").style.display="flex";
updateUI();
</script>
</body>
</html>
Featured
Advertisement
Category
Action