import React, { useState, useEffect, useRef } from 'react'; import { Sun, Moon, Heart, Apple, Package } from 'lucide-react'; export default function Minecraft2() { // Game state const [player, setPlayer] = useState({ x: 10, y: 10, direction: 'down' }); const [inventory, setInventory] = useState({ dirt: 5, grass: 3, stone: 2, wood: 4, leaves: 1 }); const [selectedBlock, setSelectedBlock] = useState('dirt'); const [health, setHealth] = useState(100); const [hunger, setHunger] = useState(100); const [isDayTime, setIsDayTime] = useState(true); const [gameTime, setGameTime] = useState(0); const [placedBlocks, setPlacedBlocks] = useState([]); const [showInventory, setShowInventory] = useState(false); const canvasRef = useRef(null); const gameLoopRef = useRef(null); // Block colors const blockColors = { dirt: '#8B4513', grass: '#567D46', stone: '#808080', wood: '#A0522D', leaves: '#228B22', }; // Handle keyboard input useEffect(() => { const handleKeyDown = (e) => { // Movement if (e.key === 'ArrowUp' || e.key === 'w') { setPlayer(prev => ({ ...prev, y: Math.max(0, prev.y - 1), direction: 'up' })); } else if (e.key === 'ArrowDown' || e.key === 's') { setPlayer(prev => ({ ...prev, y: Math.min(19, prev.y + 1), direction: 'down' })); } else if (e.key === 'ArrowLeft' || e.key === 'a') { setPlayer(prev => ({ ...prev, x: Math.max(0, prev.x - 1), direction: 'left' })); } else if (e.key === 'ArrowRight' || e.key === 'd') { setPlayer(prev => ({ ...prev, x: Math.min(19, prev.x + 1), direction: 'right' })); } // Block selection (1-5) if (e.key >= '1' && e.key <= '5') { const blockTypes = ['dirt', 'grass', 'stone', 'wood', 'leaves']; setSelectedBlock(blockTypes[parseInt(e.key) - 1]); } // Place block (space) if (e.key === ' ' && inventory[selectedBlock] > 0) { const blockPosition = { ...player }; // Check if there's already a block at this position const existingBlockIndex = placedBlocks.findIndex( block => block.x === blockPosition.x && block.y === blockPosition.y ); if (existingBlockIndex === -1) { setPlacedBlocks(prev => [...prev, { ...blockPosition, type: selectedBlock }]); setInventory(prev => ({ ...prev, [selectedBlock]: prev[selectedBlock] - 1 })); } } // Toggle inventory (e) if (e.key === 'e') { setShowInventory(prev => !prev); } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [player, inventory, selectedBlock, placedBlocks]); // Game loop useEffect(() => { const gameLoop = () => { setGameTime(prev => { const newTime = prev + 1; // Day/night cycle every 300 ticks if (newTime % 300 === 0) { setIsDayTime(prevDay => !prevDay); } // Decrease hunger every 100 ticks if (newTime % 100 === 0) { setHunger(prev => Math.max(0, prev - 5)); } // Decrease health if hunger is 0 if (newTime % 50 === 0 && hunger === 0) { setHealth(prev => Math.max(0, prev - 5)); } return newTime; }); }; gameLoopRef.current = setInterval(gameLoop, 1000); return () => clearInterval(gameLoopRef.current); }, [hunger]); // Render game useEffect(() => { const canvas = canvasRef.current; const ctx = canvas.getContext('2d'); const tileSize = 30; // Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw background based on time of day const backgroundColor = isDayTime ? '#87CEEB' : '#191970'; ctx.fillStyle = backgroundColor; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw grid ctx.strokeStyle = '#333333'; ctx.lineWidth = 0.5; for (let x = 0; x <= 20; x++) { ctx.beginPath(); ctx.moveTo(x * tileSize, 0); ctx.lineTo(x * tileSize, 20 * tileSize); ctx.stroke(); } for (let y = 0; y <= 20; y++) { ctx.beginPath(); ctx.moveTo(0, y * tileSize); ctx.lineTo(20 * tileSize, y * tileSize); ctx.stroke(); } // Draw placed blocks placedBlocks.forEach(block => { ctx.fillStyle = blockColors[block.type]; // Draw isometric-like blocks const x = block.x * tileSize; const y = block.y * tileSize; // Base square ctx.fillRect(x, y, tileSize, tileSize); // Top shading (lighter) ctx.fillStyle = adjustColor(blockColors[block.type], 30); ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + tileSize, y); ctx.lineTo(x + tileSize - 5, y + 5); ctx.lineTo(x + 5, y + 5); ctx.closePath(); ctx.fill(); // Right side shading (darker) ctx.fillStyle = adjustColor(blockColors[block.type], -30); ctx.beginPath(); ctx.moveTo(x + tileSize, y); ctx.lineTo(x + tileSize, y + tileSize); ctx.lineTo(x + tileSize - 5, y + tileSize - 5); ctx.lineTo(x + tileSize - 5, y + 5); ctx.closePath(); ctx.fill(); }); // Draw player const playerX = player.x * tileSize; const playerY = player.y * tileSize; // Player body ctx.fillStyle = '#3366CC'; ctx.fillRect(playerX + 5, playerY + 5, tileSize - 10, tileSize - 10); // Player head ctx.fillStyle = '#FFD700'; ctx.fillRect(playerX + 8, playerY + 2, tileSize - 16, tileSize - 16); // Player direction indicator ctx.fillStyle = '#FF0000'; if (player.direction === 'up') { ctx.fillRect(playerX + 12, playerY, 6, 6); } else if (player.direction === 'down') { ctx.fillRect(playerX + 12, playerY + tileSize - 6, 6, 6); } else if (player.direction === 'left') { ctx.fillRect(playerX, playerY + 12, 6, 6); } else if (player.direction === 'right') { ctx.fillRect(playerX + tileSize - 6, playerY + 12, 6, 6); } }, [player, placedBlocks, isDayTime]); // Helper function to adjust color brightness function adjustColor(color, amount) { return '#' + color.replace(/^#/, '').replace(/../g, color => ('0' + Math.min(255, Math.max(0, parseInt(color, 16) + amount)).toString(16)).substr(-2) ); } // Eat food to restore hunger const eatFood = () => { if (inventory.leaves > 0) { setHunger(prev => Math.min(100, prev + 20)); setInventory(prev => ({ ...prev, leaves: prev.leaves - 1 })); } }; // Craft wood from leaves const craftWood = () => { if (inventory.leaves >= 2) { setInventory(prev => ({ ...prev, leaves: prev.leaves - 2, wood: prev.wood + 1 })); } }; return (

Minecraft 2

Build, explore, and survive in this React-based Minecraft remake

{/* HUD */}
{isDayTime ? ( ) : ( )}
{/* Hotbar */}
{Object.entries(blockColors).map(([type, color], index) => (
setSelectedBlock(type)} > {inventory[type]} {index + 1}
))}
{/* Controls help */}

WASD/Arrows: Move

1-5: Select block

Space: Place block

E: Toggle inventory

{/* Inventory panel */} {showInventory && (

Inventory

{Object.entries(inventory).map(([type, count]) => (
{type}: {count}
))}

Crafting

)}

This is a simplified Minecraft-inspired game built with React. Use WASD or arrow keys to move, 1-5 to select blocks, and space to place blocks. Press E to open your inventory where you can craft items.

The game features a day/night cycle, hunger system, and basic crafting. Explore and build in this blocky world!

); }