html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Minecraft 样板</title>
<style>
/* 添加一些基本样式 */
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
<script>
// 获取 canvas 元素
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// 定义一些常量
const BLOCK_SIZE = 20;
const CHUNK_SIZE = 10;
const GROUND_LEVEL = 10;
const PLAYER_SIZE = 20;
const PLAYER_SPEED = 2;
// 初始化玩家位置
let playerX = CHUNK_SIZE * BLOCK_SIZE / 2;
let playerY = GROUND_LEVEL * BLOCK_SIZE - PLAYER_SIZE;
// 初始化地形数据
let chunks = {};
// 生成地形
for (let x = 0; x < canvas.width; x += CHUNK_SIZE * BLOCK_SIZE) {
let chunk = [];
for (let z = 0; z < CHUNK_SIZE; z++) {
let height = GROUND_LEVEL + Math.floor(Math.random() * 5) - 2;
for (let y = 0; y < height; y++) {
chunk.push({ x: x + z * BLOCK_SIZE, y: y * BLOCK_SIZE, type: 'grass' });
}
}
chunks[x] = chunk;
}
// 渲染地形
function renderTerrain() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let chunk in chunks) {
for (let block of chunks[chunk]) {
ctx.fillStyle = block.type === 'grass' ? 'green' : 'gray';
ctx.fillRect(block.x, block.y, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
// 渲染玩家
function renderPlayer() {
ctx.fillStyle = 'blue';
ctx.fillRect(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
}
// 碰撞检测
function checkCollision(x, y) {
for (let chunk in chunks) {
for (let block of chunks[chunk]) {
if (x + PLAYER_SIZE > block.x &&
x < block.x + BLOCK_SIZE &&
y + PLAYER_SIZE > block.y &&
y < block.y + BLOCK_SIZE) {
return true;
}
}
}
return false;
}
// 处理键盘输入
document.addEventListener('keydown', (e) => {
switch (e.key) {
case 'ArrowUp':
if (!checkCollision(playerX, playerY - PLAYER_SPEED)) {
playerY -= PLAYER_SPEED;
}
break;
case 'ArrowDown':
if (!checkCollision(playerX, playerY + PLAYER_SPEED)) {
playerY += PLAYER_SPEED;
}
break;
case 'ArrowLeft':
if (!checkCollision(playerX - PLAYER_SPEED, playerY)) {
playerX -= PLAYER_SPEED;
}
break;
case 'ArrowRight':
if (!checkCollision(playerX + PLAYER_SPEED, playerY)) {
playerX += PLAYER_SPEED;
}
break;
case ' ':
// 建造方块
let blockX = Math.floor(playerX / BLOCK_SIZE) * BLOCK_SIZE;
let blockY = Math.floor((playerY + PLAYER_SIZE) / BLOCK_SIZE) * BLOCK_SIZE;
if (!checkCollision(blockX, blockY - BLOCK_SIZE)) {
let chunkX = Math.floor(blockX / (CHUNK_SIZE * BLOCK_SIZE)) * (CHUNK_SIZE * BLOCK_SIZE);
if (!chunks[chunkX]) {
chunks[chunkX] = [];
}
chunks[chunkX].push({ x: blockX, y: blockY - BLOCK_SIZE, type: 'grass' });
}
break;
case 'Delete':
case 'Backspace':
// 销毁方块
let destroyBlockX = Math.floor(playerX / BLOCK_SIZE) * BLOCK_SIZE;
let destroyBlockY = Math.floor(playerY / BLOCK_SIZE) * BLOCK_SIZE;
let chunkX = Math.floor(destroyBlockX / (CHUNK_SIZE * BLOCK_SIZE)) * (CHUNK_SIZE * BLOCK_SIZE);
if (chunks[chunkX]) {
chunks[chunkX] = chunks[chunkX].filter((block) => {
return !(block.x === destroyBlockX && block.y === destroyBlockY);
});
}
break;
}
});
// 主循环
function loop() {
renderTerrain();
renderPlayer();
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>