我来为你创建一个基础版的 Minecraft 游戏样板。这个版本使用纯 HTML5 Canvas 和 JavaScript 实现,采用等距视角(isometric view)来模拟 3D 效果。
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基础版 Minecraft</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #87CEEB;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: Arial, sans-serif;
}
#gameContainer {
position: relative;
}
canvas {
border: 2px solid #333;
cursor: crosshair;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background-color: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
font-size: 14px;
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas"></canvas>
<div id="info">
<div>WASD - 移动</div>
<div>空格 - 跳跃</div>
<div>左键 - 销毁方块</div>
<div>右键 - 放置方块</div>
<div>1-3 - 选择方块类型</div>
</div>
</div>
<script>
// 游戏配置
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
// 防止右键菜单
canvas.addEventListener('contextmenu', e => e.preventDefault());
// 游戏常量
const BLOCK_SIZE = 20;
const WORLD_WIDTH = 20;
const WORLD_HEIGHT = 10;
const WORLD_DEPTH = 20;
// 方块类型
const BLOCK_TYPES = {
AIR: 0,
GRASS: 1,
DIRT: 2,
STONE: 3
};
const BLOCK_COLORS = {
[BLOCK_TYPES.GRASS]: { top: '#7CFC00', side: '#228B22' },
[BLOCK_TYPES.DIRT]: { top: '#8B4513', side: '#654321' },
[BLOCK_TYPES.STONE]: { top: '#808080', side: '#696969' }
};
// 游戏状态
class Game {
constructor() {
this.world = this.generateWorld();
this.player = {
x: WORLD_WIDTH / 2,
y: WORLD_HEIGHT - 3,
z: WORLD_DEPTH / 2,
vx: 0,
vy: 0,
vz: 0,
onGround: false,
selectedBlock: BLOCK_TYPES.GRASS
};
this.camera = {
x: 400,
y: 300
};
this.keys = {};
this.mouse = { x: 0, y: 0 };
this.setupEventListeners();
}
generateWorld() {
const world = [];
for (let x = 0; x < WORLD_WIDTH; x++) {
world[x] = [];
for (let y = 0; y < WORLD_HEIGHT; y++) {
world[x][y] = [];
for (let z = 0; z < WORLD_DEPTH; z++) {
// 简单地形生成
const height = Math.floor(WORLD_HEIGHT / 2 + Math.sin(x * 0.3) * 2 + Math.cos(z * 0.3) * 2);
if (y < height - 2) {
world[x][y][z] = BLOCK_TYPES.STONE;
} else if (y < height - 1) {
world[x][y][z] = BLOCK_TYPES.DIRT;
} else if (y < height) {
world[x][y][z] = BLOCK_TYPES.GRASS;
} else {
world[x][y][z] = BLOCK_TYPES.AIR;
}
}
}
}
return world;
}
setupEventListeners() {
// 键盘控制
document.addEventListener('keydown', (e) => {
this.keys[e.key.toLowerCase()] = true;
// 方块选择
if (e.key >= '1' && e.key <= '3') {
this.player.selectedBlock = parseInt(e.key);
}
});
document.addEventListener('keyup', (e) => {
this.keys[e.key.toLowerCase()] = false;
});
// 鼠标控制
canvas.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
this.mouse.x = e.clientX - rect.left;
this.mouse.y = e.clientY - rect.top;
});
canvas.addEventListener('mousedown', (e) => {
const block = this.getBlockAtMouse();
if (block) {
if (e.button === 0) { // 左键销毁
this.destroyBlock(block.x, block.y, block.z);
} else if (e.button === 2) { // 右键放置
this.placeBlock(block.x, block.y, block.z);
}
}
});
}
worldToScreen(x, y, z) {
const isoX = (x - z) * BLOCK_SIZE + this.camera.x;
const isoY = (x + z) * BLOCK_SIZE * 0.5 - y * BLOCK_SIZE + this.camera.y;
return { x: isoX, y: isoY };
}
screenToWorld(screenX, screenY) {
// 简化的屏幕到世界坐标转换
const relX = screenX - this.camera.x;
const relY = screenY - this.camera.y;
// 这是一个近似计算,用于鼠标交互
for (let y = WORLD_HEIGHT - 1; y >= 0; y--) {
for (let x = 0; x < WORLD_WIDTH; x++) {
for (let z = 0; z < WORLD_DEPTH; z++) {
if (this.world[x][y][z] !== BLOCK_TYPES.AIR) {
const pos = this.worldToScreen(x, y, z);
if (Math.abs(pos.x - screenX) < BLOCK_SIZE &&
Math.abs(pos.y - screenY) < BLOCK_SIZE) {
return { x, y, z };
}
}
}
}
}
return null;
}
getBlockAtMouse() {
return this.screenToWorld(this.mouse.x, this.mouse.y);
}
isBlockSolid(x, y, z) {
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT || z < 0 || z >= WORLD_DEPTH) {
return true;
}
return this.world[Math.floor(x)][Math.floor(y)][Math.floor(z)] !== BLOCK_TYPES.AIR;
}
destroyBlock(x, y, z) {
if (x >= 0 && x < WORLD_WIDTH && y >= 0 && y < WORLD_HEIGHT && z >= 0 && z < WORLD_DEPTH) {
this.world[x][y][z] = BLOCK_TYPES.AIR;
}
}
placeBlock(x, y, z) {
// 在方块上方放置新方块
if (y + 1 < WORLD_HEIGHT && !this.isBlockSolid(x, y + 1, z)) {
this.world[x][y + 1][z] = this.player.selectedBlock;
}
}
update() {
// 玩家移动
const speed = 0.1;
const jumpSpeed = 0.2;
if (this.keys['a']) this.player.vx = -speed;
else if (this.keys['d']) this.player.vx = speed;
else this.player.vx *= 0.8;
if (this.keys['w']) this.player.vz = -speed;
else if (this.keys['s']) this.player.vz = speed;
else this.player.vz *= 0.8;
if (this.keys[' '] && this.player.onGround) {
this.player.vy = jumpSpeed;
}
// 重力
this.player.vy -= 0.01;
// 碰撞检测和位置更新
const newX = this.player.x + this.player.vx;
const newY = this.player.y + this.player.vy;
const newZ = this.player.z + this.player.vz;
// X轴碰撞
if (!this.isBlockSolid(newX, this.player.y, this.player.z)) {
this.player.x = newX;
}
// Z轴碰撞
if (!this.isBlockSolid(this.player.x, this.player.y, newZ)) {
this.player.z = newZ;
}
// Y轴碰撞
if (!this.isBlockSolid(this.player.x, newY, this.player.z)) {
this.player.y = newY;
this.player.onGround = false;
} else {
if (this.player.vy < 0) {
this.player.onGround = true;
}
this.player.vy = 0;
}
// 边界检查
this.player.x = Math.max(0, Math.min(WORLD_WIDTH - 1, this.player.x));
this.player.z = Math.max(0, Math.min(WORLD_DEPTH - 1, this.player.z));
this.player.y = Math.max(0, Math.min(WORLD_HEIGHT - 1, this.player.y));
}
drawBlock(x, y, z, type) {
if (type === BLOCK_TYPES.AIR) return;
const pos = this.worldToScreen(x, y, z);
const colors = BLOCK_COLORS[type];
// 绘制方块的三个可见面
// 顶面
ctx.fillStyle = colors.top;
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(pos.x + BLOCK_SIZE, pos.y - BLOCK_SIZE * 0.5);
ctx.lineTo(pos.x, pos.y - BLOCK_SIZE);
ctx.lineTo(pos.x - BLOCK_SIZE, pos.y - BLOCK_SIZE * 0.5);
ctx.closePath();
ctx.fill();
// 左侧面
ctx.fillStyle = colors.side;
ctx.beginPath();
ctx.moveTo(pos.x - BLOCK_SIZE, pos.y - BLOCK_SIZE * 0.5);
ctx.lineTo(pos.x - BLOCK_SIZE, pos.y + BLOCK_SIZE * 0.5);
ctx.lineTo(pos.x, pos.y + BLOCK_SIZE);
ctx.lineTo(pos.x, pos.y);
ctx.closePath();
ctx.fill();
// 右侧面
ctx.fillStyle = colors.side;
ctx.filter = 'brightness(0.8)';
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(pos.x, pos.y + BLOCK_SIZE);
ctx.lineTo(pos.x + BLOCK_SIZE, pos.y + BLOCK_SIZE * 0.5);
ctx.lineTo(pos.x + BLOCK_SIZE, pos.y - BLOCK_SIZE * 0.5);
ctx.closePath();
ctx.fill();
ctx.filter = 'none';
}
render() {
// 清空画布
ctx.fillStyle = '#87CEEB';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制世界
for (let y = 0; y < WORLD_HEIGHT; y++) {
for (let x = WORLD_WIDTH - 1; x >= 0; x--) {
for (let z = 0; z < WORLD_DEPTH; z++) {
if (this.world[x][y][z] !== BLOCK_TYPES.AIR) {
this.drawBlock(x, y, z, this.world[x][y][z]);
}
}
}
}
// 绘制玩家
const playerPos = this.worldToScreen(this.player.x, this.player.y, this.player.z);
ctx.fillStyle = '#FF0000';
ctx.fillRect(playerPos.x - 5, playerPos.y - 20, 10, 20);
// 绘制选中的方块类型
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(10, canvas.height - 40, 150, 30);
ctx.fillStyle = 'white';
ctx.font = '16px Arial';
ctx.fillText(`选中: ${['空气', '草方块', '泥土', '石头'][this.player.selectedBlock]}`, 20, canvas.height - 20);
}
run() {
this.update();
this.render();
requestAnimationFrame(() => this.run());
}
}
// 启动游戏
const game = new Game();
game.run();
</script>
</body>
</html>