Spaces:
Running
Running
File size: 7,149 Bytes
34072fd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2D Minecraft | Physics Visualizer Pro</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
canvas {
border: 1px solid #333;
background-color: #87CEEB;
display: block;
margin: 0 auto;
}
#inventory {
display: flex;
justify-content: center;
margin: 10px 0;
}
.block-item {
border: 2px solid transparent;
margin: 0 5px;
cursor: pointer;
}
.block-item.selected {
border-color: #4f46e5;
}
</style>
</head>
<body class="bg-gray-100">
<nav class="bg-indigo-600 text-white shadow-lg">
<div class="container mx-auto px-6 py-3">
<div class="flex justify-between items-center">
<div class="flex space-x-4">
<div>
<a href="#" class="flex items-center py-2 px-2">
<i data-feather="atom" class="mr-2"></i>
<span class="font-bold text-lg">Physics Visualizer Pro</span>
</a>
</div>
</div>
<div class="hidden md:flex items-center space-x-1">
<a href="index.html" class="py-2 px-3 hover:bg-indigo-700 rounded transition duration-300">Home</a>
<a href="minecraft2d.html" class="py-2 px-3 bg-indigo-700 rounded hover:bg-indigo-800 transition duration-300">2D Minecraft</a>
</div>
</div>
</div>
</nav>
<div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold text-center mb-6">2D Minecraft</h1>
<div class="max-w-3xl mx-auto bg-white p-6 rounded-lg shadow-md">
<div id="inventory">
<div class="block-item selected" data-block="dirt">
<img src="https://static.photos/agriculture/64x64/1" alt="Dirt" width="64" height="64">
</div>
<div class="block-item" data-block="stone">
<img src="https://static.photos/construction/64x64/2" alt="Stone" width="64" height="64">
</div>
<div class="block-item" data-block="wood">
<img src="https://static.photos/agriculture/64x64/3" alt="Wood" width="64" height="64">
</div>
<div class="block-item" data-block="leaf">
<img src="https://static.photos/nature/64x64/4" alt="Leaf" width="64" height="64">
</div>
</div>
<canvas id="gameCanvas" width="800" height="400"></canvas>
<div class="mt-4 text-center">
<p class="text-gray-600 mb-2">Controls: Click to place blocks. Select block type from above.</p>
<button id="clearBtn" class="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600">
Clear World
</button>
</div>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let selectedBlock = 'dirt';
// Block textures (using placeholder images)
const blockTextures = {
dirt: 'https://static.photos/agriculture/32x32/1',
stone: 'https://static.photos/construction/32x32/2',
wood: 'https://static.photos/agriculture/32x32/3',
leaf: 'https://static.photos/nature/32x32/4'
};
// Game grid
const gridSize = 32;
const gridWidth = canvas.width / gridSize;
const gridHeight = canvas.height / gridSize;
let world = Array(gridWidth).fill().map(() => Array(gridHeight).fill(null));
// Generate simple terrain
function generateTerrain() {
const groundLevel = Math.floor(gridHeight * 0.7);
for (let x = 0; x < gridWidth; x++) {
// Base ground layer
for (let y = groundLevel; y < gridHeight; y++) {
world[x][y] = 'dirt';
}
// Stone layer below ground
for (let y = groundLevel + 3; y < gridHeight; y++) {
world[x][y] = 'stone';
}
// Add some surface features
if (x % 10 === 0 && x > 0 && x < gridWidth - 5) {
// Tree
const treeHeight = 5;
for (let y = groundLevel - 1; y >= groundLevel - treeHeight; y--) {
world[x][y] = 'wood';
}
// Tree leaves
for (let dx = -2; dx <= 2; dx++) {
if (x + dx >= 0 && x + dx < gridWidth) {
world[x + dx][groundLevel - treeHeight - 1] = 'leaf';
}
}
}
}
}
// Draw the world
function drawWorld() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Sky
ctx.fillStyle = '#87CEEB';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw blocks
for (let x = 0; x < gridWidth; x++) {
for (let y = 0; y < gridHeight; y++) {
if (world[x][y]) {
const img = new Image();
img.src = blockTextures[world[x][y]];
ctx.drawImage(img, x * gridSize, y * gridSize, gridSize, gridSize);
}
}
}
}
// Handle block selection
document.querySelectorAll('.block-item').forEach(item => {
item.addEventListener('click', () => {
document.querySelectorAll('.block-item').forEach(i => i.classList.remove('selected'));
item.classList.add('selected');
selectedBlock = item.dataset.block;
});
});
// Handle canvas clicks
canvas.addEventListener('click', (e) => {
const rect = canvas.getBoundingClientRect();
const x = Math.floor((e.clientX - rect.left) / gridSize);
const y = Math.floor((e.clientY - rect.top) / gridSize);
if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight) {
world[x][y] = selectedBlock;
drawWorld();
}
});
// Clear button
document.getElementById('clearBtn').addEventListener('click', () => {
world = Array(gridWidth).fill().map(() => Array(gridHeight).fill(null));
generateTerrain();
drawWorld();
});
// Initialize game
generateTerrain();
drawWorld();
</script>
</body>
</html> |