更新日志
2025/6/14
制作基本玩法如下:
| 操作 | 移动 | 跳跃 | 切换物品栏 | 破坏方块 | 放置方块 | 退出 |
|---|---|---|---|---|---|---|
| 按键 | ←/→ | 空格 | 1~9 | D(delete) | P(put) | Q(quit) |
2025/6/13~2025/6/20
1.增加世界范围至1000*100
大范围世界可以更好的探索
2.修改放置和破坏逻辑
自由选择放置/破坏
3.添加更多信息
如血量、饱食度
4.添加新方块
现在方块类型如下:
| 空气 | 草方块 | 泥土 | 石头 |
|---|---|---|---|
| 煤炭矿 | 铁矿 | 钻石矿 | 原木 |
| 叶子 | 沙子 | 水 | 基岩 |
BUG反馈
代码
// 字字落实
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cstdlib>
#include <ctime>
#include <random>
#include <cmath>
#include <conio.h>
#include <windows.h>
#include <algorithm>
#include <sstream>
using namespace std;
// 控制台颜色常量
const int BLACK = 0;
const int BLUE = 1;
const int GREEN = 2;
const int CYAN = 3;
const int RED = 4;
const int MAGENTA = 5;
const int BROWN = 6; // 棕色
const int LIGHTGRAY = 7;
const int DARKGRAY = 8;
const int LIGHTBLUE = 9;
const int LIGHTGREEN = 10;
const int LIGHTCYAN = 11;
const int LIGHTRED = 12;
const int LIGHTMAGENTA = 13;
const int YELLOW = 14; // 黄色
const int WHITE = 15;
// 确保 ENABLE_VIRTUAL_TERMINAL_PROCESSING 被定义
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
// ANSI转义序列映射
void setColor(stringstream& ss, int textColor, int bgColor) {
static const map<int, int> colorMap = {
{BLACK, 30}, {RED, 31}, {GREEN, 32}, {YELLOW, 33},
{BLUE, 34}, {MAGENTA, 35}, {CYAN, 36}, {WHITE, 37},
{DARKGRAY, 90}, {LIGHTRED, 91}, {LIGHTGREEN, 92}, {YELLOW, 93},
{LIGHTBLUE, 94}, {LIGHTMAGENTA, 95}, {LIGHTCYAN, 96}, {LIGHTGRAY, 97},
{BROWN, 33} // 棕色
};
auto textIt = colorMap.find(textColor);
auto bgIt = colorMap.find(bgColor);
if (textIt != colorMap.end() && bgIt != colorMap.end()) {
ss << "\033[" << textIt->second << ";" << (bgIt->second + 10) << "m";
}
else {
ss << "\033[0m";
}
}
// 重置为默认颜色
void resetColor(stringstream& ss) {
ss << "\033[0m";
}
enum class BlockType {
AIR, // 空气
GRASS, // 草方块
DIRT, // 泥土
STONE, // 石头
COAL_ORE, // 煤炭矿
IRON_ORE, // 铁矿
DIAMOND_ORE, // 钻石矿
WOOD, // 原木
LEAF, // 叶子
SAND, // 沙子
WATER, // 水
BEDROCK, // 基岩
COAL, // 煤炭
IRON, // 铁
DIAMOND, // 钻石
NONE // 无物品
};
// 物品栏槽位
struct InventorySlot {
BlockType type;
int count;
};
struct BlockDisplay {
string line1; // 第一行内容
string line2; // 第二行内容
int bgColor; // 背景颜色
string name; // 方块名称
};
BlockDisplay getBlockDisplay(BlockType type) {
static const map<BlockType, BlockDisplay> displayMap = {
{BlockType::AIR, {" ", " ", BLACK, "空气"}},
{BlockType::GRASS, {"草方", "块 ", GREEN, "草块"}},
{BlockType::DIRT, {"泥土", " ", BROWN, "泥土"}},
{BlockType::STONE, {"石头", " ", DARKGRAY, "石头"}},
{BlockType::COAL_ORE, {"煤炭", "矿 ", DARKGRAY, "煤炭矿"}},
{BlockType::IRON_ORE, {"铁矿", " ", DARKGRAY, "铁矿"}},
{BlockType::DIAMOND_ORE, {"钻石", "矿 ", LIGHTBLUE, "钻石矿"}}, // 浅蓝色背景
{BlockType::WOOD, {"原木", " ", BROWN, "原木"}},
{BlockType::LEAF, {"叶子", " ", LIGHTGREEN, "叶子"}},
{BlockType::SAND, {"沙子", " ", YELLOW, "沙子"}},
{BlockType::WATER, {"水 ", " ", BLUE, "水"}},
{BlockType::BEDROCK, {"基岩", " ", DARKGRAY, "基岩"}},
{BlockType::COAL, {"煤炭", " ", DARKGRAY, "煤炭"}},
{BlockType::IRON, {"铁 ", " ", DARKGRAY, "铁"}},
{BlockType::DIAMOND, {"钻石", " ", LIGHTBLUE, "钻石"}}, // 浅蓝色背景
{BlockType::NONE, {" ", " ", BLACK, "无"}}
};
return displayMap.at(type);
}
// 检查方块是否可穿过(空气或水)
bool isPassable(BlockType type) {
return type == BlockType::AIR || type == BlockType::WATER || type == BlockType::LEAF;
}
// 检查方块是否实体(不可穿过)
bool isSolid(BlockType type) {
return !isPassable(type);
}
// 检查方块是否可以放置
bool canPlace(BlockType type) {
return type != BlockType::AIR && type != BlockType::WATER &&
type != BlockType::BEDROCK && type != BlockType::COAL &&
type != BlockType::IRON && type != BlockType::DIAMOND &&
type != BlockType::NONE;
}
// 检查方块是否可以破坏
bool canBreak(BlockType type) {
return type != BlockType::AIR && type != BlockType::BEDROCK;
}
// 获取方块破坏后的掉落物
BlockType getDropItem(BlockType type) {
switch (type) {
case BlockType::COAL_ORE: return BlockType::COAL;
case BlockType::IRON_ORE: return BlockType::IRON;
case BlockType::DIAMOND_ORE: return BlockType::DIAMOND;
default: return type;
}
}
// 随机游走隧道生成函数
void generateRandomTunnel(vector<vector<BlockType>>& world, int startX, int startY,
int length, int width, uniform_real_distribution<>& dis, mt19937& gen,
int WIDTH, int HEIGHT, int BEDROCK_LAYER) {
int x = startX;
int y = startY;
for (int i = 0; i < length; i++) {
// 确保位置在边界内
x = max(0, min(WIDTH - 1, x));
y = max(0, min(HEIGHT - 1, y));
// 清除隧道区域(圆形)
int radius = width / 2;
for (int dx = -radius; dx <= radius; dx++) {
for (int dy = -radius; dy <= radius; dy++) {
if (dx*dx + dy*dy <= radius*radius + 1) { // +1 使形状更自然
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < WIDTH && ny >= 0 && ny < HEIGHT) {
// 确保不会破坏基岩及其上方的石头层
// 允许在钻石矿生成层(y>85)生成矿洞
if (ny < HEIGHT - BEDROCK_LAYER || ny >= HEIGHT - 15) {
if (world[ny][nx] != BlockType::BEDROCK) {
world[ny][nx] = BlockType::AIR;
}
}
}
}
}
}
// 随机选择下一步方向(70%水平,30%垂直)
double direction = dis(gen);
if (direction < 0.7) { // 水平移动
if (dis(gen) < 0.5) x++; // 右
else x--; // 左
} else { // 垂直移动
if (dis(gen) < 0.5) y++; // 下
else y--; // 上
}
// 10%概率增加宽度变化
if (dis(gen) < 0.1) {
width = max(1, min(6, width + (dis(gen) < 0.5 ? 1 : -1)));
}
}
}
// 生成1000×100的随机地形(带矿洞)
vector<vector<BlockType>> generateWorld() {
const int WIDTH = 1000;
const int HEIGHT = 100;
const int BEDROCK_LAYER = 5; // 基岩层厚度
const int DIAMOND_MIN_Y = HEIGHT - 15; // 钻石矿最小生成高度 (y>85)
// 设置随机数生成器
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> dis(0.0, 1.0);
// 创建世界网格(初始为空气)
vector<vector<BlockType>> world(HEIGHT, vector<BlockType>(WIDTH, BlockType::AIR));
// 生成高度图(使用简单噪声)
vector<int> groundHeight(WIDTH);
for (int x = 0; x < WIDTH; x++) {
// 基础高度(平原地面在50高度)
int baseHeight = 50;
// 添加轻微噪声(±3格)
double noise = sin(x * 0.02) * 1.5 + cos(x * 0.05) * 1.2;
groundHeight[x] = baseHeight + static_cast<int>(noise);
// 确保高度在合理范围内
groundHeight[x] = max(10, min(HEIGHT - 10, groundHeight[x]));
}
// 填充世界(从顶部到底部)
for (int x = 0; x < WIDTH; x++) {
// 基岩层(世界底部)
for (int y = HEIGHT - BEDROCK_LAYER; y < HEIGHT; y++) {
if (y == HEIGHT - 1) {
world[y][x] = BlockType::BEDROCK;
} else {
world[y][x] = BlockType::STONE; // 基岩上方的石头层
}
}
// 从地表向下填充
for (int y = groundHeight[x]; y < HEIGHT - BEDROCK_LAYER; y++) {
// 地表层 - 草方块
if (y == groundHeight[x]) {
world[y][x] = BlockType::GRASS;
}
// 浅层(泥土)
else if (y <= groundHeight[x] + 3) {
world[y][x] = BlockType::DIRT;
}
// 深层(石头和矿石)
else {
// 深层矿石生成(煤炭、铁矿、钻石)
double r = dis(gen);
if (r < 0.02) { // 2%的概率生成煤炭矿
world[y][x] = BlockType::COAL_ORE;
} else if (r < 0.005) { // 0.5%的概率生成铁矿
world[y][x] = BlockType::IRON_ORE;
} else if (r < 0.0005 && y >= DIAMOND_MIN_Y) { // 0.05%的概率生成钻石矿,且只在地下15层以内
world[y][x] = BlockType::DIAMOND_ORE;
} else {
world[y][x] = BlockType::STONE;
}
}
}
}
// 使用随机游走算法生成矿洞
for (int i = 0; i < 60; i++) { // 增加矿洞数量到60个
// 随机选择起始点(在地表下)
int startX = static_cast<int>(dis(gen) * (WIDTH - 20)) + 10;
// 确保矿洞不会在接近基岩的地方生成(至少离基岩层10格以上)
int minY = groundHeight[startX] + 5;
int maxY = HEIGHT - BEDROCK_LAYER - 10; // 离基岩层至少10格
if (maxY < minY) continue; // 如果地表高度太高,跳过
int startY = minY + static_cast<int>(dis(gen) * (maxY - minY));
// 矿洞参数
int tunnelLength = 80 + static_cast<int>(dis(gen) * 120); // 80-200步长
int tunnelWidth = 2 + static_cast<int>(dis(gen) * 3); // 2-5格宽
int branchChance = 20; // 20%概率产生分支
int maxBranches = 3; // 最多3个分支
// 主矿洞生成(允许在钻石层生成)
generateRandomTunnel(world, startX, startY, tunnelLength, tunnelWidth, dis, gen, WIDTH, HEIGHT, BEDROCK_LAYER);
// 生成分支
int branches = 0;
for (int j = 0; j < tunnelLength && branches < maxBranches; j++) {
if (static_cast<int>(dis(gen) * 100) < branchChance) {
int branchX = startX;
int branchY = startY;
int branchLength = 30 + static_cast<int>(dis(gen) * 70); // 30-100步长
int branchWidth = 1 + static_cast<int>(dis(gen) * 2); // 1-3格宽
// 随机选择分支方向(上下左右)
int dir = static_cast<int>(dis(gen) * 4);
switch (dir) {
case 0: branchX += 5; break; // 右
case 1: branchX -= 5; break; // 左
case 2: branchY += 5; break; // 下
case 3: branchY -= 5; break; // 上
}
// 确保分支不会在基岩附近生成
if (branchY < HEIGHT - BEDROCK_LAYER - 5) {
generateRandomTunnel(world, branchX, branchY, branchLength, branchWidth, dis, gen, WIDTH, HEIGHT, BEDROCK_LAYER);
branches++;
}
}
}
}
// 添加小型矿脉(包括钻石)
for (int i = 0; i < 100; i++) {
int veinX = static_cast<int>(dis(gen) * (WIDTH - 20)) + 10;
int veinY = groundHeight[veinX] + 10 + static_cast<int>(dis(gen) * 30); // 地表下10-40层
// 确保矿脉不会在基岩附近生成
if (veinY >= HEIGHT - BEDROCK_LAYER - 5) continue;
// 矿脉类型(煤炭、铁矿或钻石)
BlockType veinType;
double r = dis(gen);
if (r < 0.6) {
veinType = BlockType::COAL_ORE; // 60%煤炭
} else if (r < 0.9) {
veinType = BlockType::IRON_ORE; // 30%铁矿
} else {
veinType = BlockType::DIAMOND_ORE; // 10%钻石
}
int veinSize = 5 + static_cast<int>(dis(gen) * 10); // 5-15格大小
for (int dx = -veinSize; dx <= veinSize; dx++) {
for (int dy = -veinSize; dy <= veinSize; dy++) {
int xPos = veinX + dx;
int yPos = veinY + dy;
if (xPos >= 0 && xPos < WIDTH && yPos >= 0 && yPos < HEIGHT) {
// 矿脉形状(随机斑点)
if (abs(dx) + abs(dy) < veinSize && dis(gen) < 0.7) {
// 确保不会替换基岩层
if (world[yPos][xPos] != BlockType::BEDROCK && yPos < HEIGHT - BEDROCK_LAYER) {
world[yPos][xPos] = veinType;
}
}
}
}
}
}
// 添加湖泊(在地表)
for (int i = 0; i < 20; i++) { // 增加湖泊数量
int lakeX = static_cast<int>(dis(gen) * (WIDTH - 10)) + 5;
int lakeY = groundHeight[lakeX]; // 地表位置
int lakeWidth = 5 + static_cast<int>(dis(gen) * 8);
int lakeDepth = 2 + static_cast<int>(dis(gen) * 3);
for (int dx = -lakeWidth/2; dx < lakeWidth/2; dx++) {
int xPos = lakeX + dx;
if (xPos < 0 || xPos >= WIDTH) continue;
for (int dy = 0; dy < lakeDepth; dy++) {
int yPos = lakeY + dy;
if (yPos < HEIGHT && yPos >= 0) {
world[yPos][xPos] = BlockType::WATER;
}
}
}
}
// 在地表生成树木(草方块上)
for (int x = 0; x < WIDTH; x++) {
if (dis(gen) < 0.05 && world[groundHeight[x]][x] == BlockType::GRASS) {
int treeHeight = 3 + static_cast<int>(dis(gen) * 3); // 3-6格高
// 生成树干(从地表向上)
for (int i = 1; i <= treeHeight && (groundHeight[x] - i) >= 0; i++) {
world[groundHeight[x] - i][x] = BlockType::WOOD;
}
// 生成树叶(树干顶部)
int canopyY = groundHeight[x] - treeHeight;
if (canopyY >= 0) {
// 树叶层(树冠)
int leafRadius = 2;
for (int dx = -leafRadius; dx <= leafRadius; dx++) {
for (int dy = -leafRadius; dy <= 0; dy++) {
int nx = x + dx;
int ny = canopyY + dy;
if (nx >= 0 && nx < WIDTH && ny >= 0 && ny < HEIGHT) {
if (world[ny][nx] == BlockType::AIR) {
world[ny][nx] = BlockType::LEAF;
}
}
}
}
// 树顶
if (canopyY - 1 >= 0) {
world[canopyY - 1][x] = BlockType::LEAF;
}
}
}
}
// 修复y=95层问题:确保基岩层上方的石头层不被破坏
for (int x = 0; x < WIDTH; x++) {
for (int y = HEIGHT - BEDROCK_LAYER; y < HEIGHT - 1; y++) { // y=95到98
// 如果这些位置被破坏成了空气,恢复为石头
if (world[y][x] == BlockType::AIR) {
world[y][x] = BlockType::STONE;
}
}
}
// 在深层添加额外的钻石矿
for (int i = 0; i < 500; i++) { // 增加钻石矿生成次数
int diamondX = static_cast<int>(dis(gen) * WIDTH);
int diamondY = DIAMOND_MIN_Y + static_cast<int>(dis(gen) * (HEIGHT - BEDROCK_LAYER - DIAMOND_MIN_Y - 1));
if (diamondY >= DIAMOND_MIN_Y && diamondY < HEIGHT - BEDROCK_LAYER) {
// 确保位置是石头
if (world[diamondY][diamondX] == BlockType::STONE) {
world[diamondY][diamondX] = BlockType::DIAMOND_ORE;
}
}
}
return world;
}
// 玩家结构
struct Player {
int x; // X坐标(世界坐标,-499~500)
int y; // Y坐标(世界坐标,0~99)
float velocity; // 垂直速度
bool inWater; // 是否在水中
bool onGround; // 是否站在地面上
bool canJump; // 是否可以跳跃
vector<InventorySlot> inventory; // 物品栏
int selectedSlot; // 当前选中的物品栏槽位
int health; // 血量(0-20)
int hunger; // 饱食度(0-20)
};
// 初始化玩家
void initPlayer(Player& player) {
player.x = 0; // 世界中心
player.y = 45; // 地表上方
player.velocity = 0.0f;
player.inWater = false;
player.onGround = false;
player.canJump = true;
player.health = 20;
player.hunger = 20;
player.inventory = vector<InventorySlot>(9, {BlockType::NONE, 0});
player.selectedSlot = 0;
}
// 添加物品到物品栏
void addToInventory(Player& player, BlockType type) {
// 无法获得空气、水和基岩
if (type == BlockType::AIR || type == BlockType::WATER || type == BlockType::BEDROCK) {
return;
}
// 检查物品栏是否已有该物品
for (auto& slot : player.inventory) {
if (slot.type == type) {
slot.count++;
return;
}
}
// 找到第一个空槽位
for (auto& slot : player.inventory) {
if (slot.type == BlockType::NONE) {
slot.type = type;
slot.count = 1;
return;
}
}
}
// 使用当前选中的物品
bool useSelectedItem(Player& player) {
InventorySlot& slot = player.inventory[player.selectedSlot];
if (slot.type == BlockType::NONE || slot.count <= 0) {
return false;
}
// 减少数量
slot.count--;
// 如果数量为0,清空槽位
if (slot.count == 0) {
slot.type = BlockType::NONE;
}
return true;
}
// 生成血量和饱食度字符串
string getHealthBar(int value) {
string bar;
// 每2点一个单位
for (int i = 0; i < 10; i++) {
int points = value - i * 2;
if (points >= 2) {
bar += '#'; // 满
} else if (points >= 1) {
bar += '='; // 半
} else {
bar += '-'; // 空
}
}
return bar;
}
// 生成饱食度字符串(反向)
string getHungerBar(int value) {
string bar;
// 每2点一个单位,反向显示
for (int i = 9; i >= 0; i--) {
int points = value - i * 2;
if (points >= 2) {
bar += '#'; // 满
} else if (points >= 1) {
bar += '='; // 半
} else {
bar += '-'; // 空
}
}
return bar;
}
// 渲染世界(包含玩家和状态信息)
void renderWorld(stringstream& ss, const vector<vector<BlockType>>& world, const Player& player,
int selectedX = -1, int selectedY = -1) {
const int VIEW_WIDTH = 23;
const int VIEW_HEIGHT = 7;
// 计算视图范围(以玩家为中心)
int startX = player.x - VIEW_WIDTH / 2;
int startY = player.y - VIEW_HEIGHT / 2;
// 确保视图在边界内
startX = max(-499, min(500 - VIEW_WIDTH, startX));
startY = max(0, min(100 - VIEW_HEIGHT, startY));
// 顶部边框
resetColor(ss);
for (int x = 0; x < VIEW_WIDTH; x++) {
ss << "+----";
}
ss << "+\n";
// 渲染视图范围内的方块
for (int viewY = 0; viewY < VIEW_HEIGHT; viewY++) {
int worldY = startY + viewY;
// 第一行内容
resetColor(ss);
ss << "|";
for (int viewX = 0; viewX < VIEW_WIDTH; viewX++) {
int worldX = startX + viewX;
BlockType block = BlockType::AIR;
// 检查是否在世界范围内
if (worldY >= 0 && worldY < 100 && worldX >= -499 && worldX < 501) {
// 转换为数组索引
int arrayX = worldX + 499;
int arrayY = worldY;
block = world[arrayY][arrayX];
}
// 检查是否是玩家位置
if (worldX == player.x && worldY == player.y) {
setColor(ss, WHITE, RED); // 玩家红色背景
if (player.inWater) {
ss << "含水";
} else {
ss << "玩家";
}
}
// 检查是否是选中的方块
else if (worldX == selectedX && worldY == selectedY) {
setColor(ss, BLACK, WHITE); // 白底黑字
BlockDisplay display = getBlockDisplay(block);
ss << display.line1;
}
else {
BlockDisplay display = getBlockDisplay(block);
setColor(ss, WHITE, display.bgColor); // 方块背景色
ss << display.line1;
}
resetColor(ss);
ss << "|";
}
ss << "\n";
// 第二行内容
resetColor(ss);
ss << "|";
for (int viewX = 0; viewX < VIEW_WIDTH; viewX++) {
int worldX = startX + viewX;
BlockType block = BlockType::AIR;
// 检查是否在世界范围内
if (worldY >= 0 && worldY < 100 && worldX >= -499 && worldX < 501) {
// 转换为数组索引
int arrayX = worldX + 499;
int arrayY = worldY;
block = world[arrayY][arrayX];
}
// 检查是否是玩家位置
if (worldX == player.x && worldY == player.y) {
setColor(ss, WHITE, RED); // 玩家红色背景
if (player.inWater) {
ss << "玩家";
} else {
ss << " ";
}
}
// 检查是否是选中的方块
else if (worldX == selectedX && worldY == selectedY) {
setColor(ss, BLACK, WHITE); // 白底黑字
BlockDisplay display = getBlockDisplay(block);
ss << display.line2;
}
else {
BlockDisplay display = getBlockDisplay(block);
setColor(ss, WHITE, display.bgColor); // 方块背景色
ss << display.line2;
}
resetColor(ss);
ss << "|";
}
ss << "\n";
// 底部边框
resetColor(ss);
for (int x = 0; x < VIEW_WIDTH; x++) {
ss << "+----";
}
ss << "+\n";
}
// 显示血量和饱食度(使用-、=、#表示)
ss << "\n";
setColor(ss, LIGHTRED, BLACK);
ss << getHealthBar(player.health);
resetColor(ss);
ss << " "; // 4个空格分隔
setColor(ss, YELLOW, BLACK);
ss << getHungerBar(player.hunger);
resetColor(ss);
ss << "\n";
// 显示物品栏
ss << "物品栏: \n";
for (int i = 0; i < 9; i++) {
if (i == player.selectedSlot) {
setColor(ss, BLACK, LIGHTGRAY); // 选中的槽位:灰色背景
} else {
resetColor(ss); // 其他槽位:黑色背景
}
const InventorySlot& slot = player.inventory[i];
if (slot.type == BlockType::NONE) {
ss << "[ :0]";
} else {
BlockDisplay itemDisplay = getBlockDisplay(slot.type);
string name = itemDisplay.name;
// 调整名称长度以确保对齐
if (name.length() == 2) name += " ";
else if (name.length() == 3) name += " ";
ss << "[" << name << ":" << slot.count << "]";
}
// 添加空格分隔
if (i < 8) ss << " ";
}
resetColor(ss);
ss << "\n";
// 显示坐标
ss << "坐标: X=" << player.x << " Y=" << player.y << "\n";
}
int main() {
// 启用ANSI转义序列支持
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
// 设置随机种子
srand(static_cast<unsigned int>(time(nullptr)));
// 生成1000×100的世界
cout << "正在生成世界..." << endl;
vector<vector<BlockType>> world = generateWorld();
cout << "世界生成完成!" << endl;
// 初始化玩家
Player player;
initPlayer(player);
// 方块选择模式变量
bool inSelectionMode = false;
bool isDiggingMode = false;
int selectedX = player.x;
int selectedY = player.y + 1; // 默认选择玩家下方方块
// 游戏主循环
bool needsRedraw = true; // 初始需要绘制
while (true) {
bool playerMoved = false; // 标记玩家是否移动
bool playerJumped = false; // 标记玩家是否跳跃
bool blockChanged = false; // 标记方块是否被破坏或放置
// 处理输入
if (_kbhit()) {
char key = _getch();
// 如果在选择模式
if (inSelectionMode) {
// 方向键移动选择框
if (key == 75 && selectedX > -499) { // 左箭头
selectedX--;
needsRedraw = true;
}
else if (key == 77 && selectedX < 500) { // 右箭头
selectedX++;
needsRedraw = true;
}
else if (key == 72 && selectedY > 0) { // 上箭头
selectedY--;
needsRedraw = true;
}
else if (key == 80 && selectedY < 99) { // 下箭头
selectedY++;
needsRedraw = true;
}
// 空格确认操作
else if (key == 32) { // 空格
// 确保在世界范围内
if (selectedY >= 0 && selectedY < 100 && selectedX >= -499 && selectedX < 501) {
// 转换为数组索引
int arrayX = selectedX + 499;
int arrayY = selectedY;
// 挖掘操作
if (isDiggingMode) {
// 检查是否可以破坏(基岩、水、空气不能挖掘)
BlockType targetBlock = world[arrayY][arrayX];
if (targetBlock != BlockType::BEDROCK &&
targetBlock != BlockType::WATER &&
targetBlock != BlockType::AIR) {
// 获取掉落物
BlockType drop = getDropItem(targetBlock);
// 添加到物品栏
addToInventory(player, drop);
// 破坏方块(变为空气)
world[arrayY][arrayX] = BlockType::AIR;
blockChanged = true;
}
}
// 放置操作
else {
// 获取当前选中的物品
BlockType selected = player.inventory[player.selectedSlot].type;
// 检查是否可以放置
if (canPlace(selected)) {
// 检查目标位置是否可以放置(非水方块或玩家位置不能放置)
if (world[arrayY][arrayX] == BlockType::AIR &&
(selectedX != player.x || selectedY != player.y)) {
// 使用物品
if (useSelectedItem(player)) {
// 放置方块
world[arrayY][arrayX] = selected;
blockChanged = true;
}
}
}
}
}
inSelectionMode = false;
needsRedraw = true;
}
// ESC退出选择模式
else if (key == 27) { // ESC
inSelectionMode = false;
needsRedraw = true;
}
}
// 不在选择模式
else {
// 左移
if (key == 75 && player.x > -499) { // 左箭头
// 检查左侧方块是否可穿过
int arrayX = (player.x - 1) + 499;
int arrayY = player.y;
if (arrayX >= 0 && arrayX < 1000 && arrayY >= 0 && arrayY < 100) {
if (isPassable(world[arrayY][arrayX])) {
player.x--;
playerMoved = true;
}
}
}
// 右移
else if (key == 77 && player.x < 500) { // 右箭头
// 检查右侧方块是否可穿过
int arrayX = (player.x + 1) + 499;
int arrayY = player.y;
if (arrayX >= 0 && arrayX < 1000 && arrayY >= 0 && arrayY < 100) {
if (isPassable(world[arrayY][arrayX])) {
player.x++;
playerMoved = true;
}
}
}
// 跳跃
else if (key == 32) { // 空格
// 只有在地面上或水中才能跳跃
if (player.canJump && (player.onGround || player.inWater)) {
player.velocity = player.inWater ? -1.5f : -2.0f; // 水中跳跃较低
player.onGround = false;
player.canJump = false; // 防止连续跳跃
playerJumped = true;
}
}
// 物品栏选择 (1-9)
else if (key >= '1' && key <= '9') {
int slot = key - '1'; // 转换为0-8的索引
if (slot >= 0 && slot < 9) {
player.selectedSlot = slot;
needsRedraw = true;
}
}
// 破坏方块 (D键)
else if (key == 'd' || key == 'D') {
inSelectionMode = true;
isDiggingMode = true;
selectedX = player.x;
selectedY = player.y + 1; // 默认选择玩家下方方块
needsRedraw = true;
}
// 放置方块 (P键)
else if (key == 'p' || key == 'P') {
inSelectionMode = true;
isDiggingMode = false;
selectedX = player.x;
selectedY = player.y + 1; // 默认选择玩家下方方块
needsRedraw = true;
}
// 退出
else if (key == 'q' || key == 'Q') {
break;
}
}
}
// 物理更新(只在非选择模式下)
if (!inSelectionMode) {
player.velocity += 0.1f; // 重力加速度
int newY = player.y + static_cast<int>(player.velocity);
// 检查是否在水中
bool wasInWater = player.inWater;
int arrayX = player.x + 499;
int arrayY = player.y;
if (arrayX >= 0 && arrayX < 1000 && arrayY >= 0 && arrayY < 100) {
player.inWater = (world[arrayY][arrayX] == BlockType::WATER);
} else {
player.inWater = false;
}
// 如果在水里,减缓下落速度
if (player.inWater) {
player.velocity *= 0.8f;
player.canJump = true; // 在水中时可以重置跳跃能力
}
// 碰撞检测
bool wasOnGround = player.onGround;
player.onGround = false;
if (newY != player.y) {
// 检查下方方块是否可穿过
int belowX = player.x + 499;
int belowY = newY;
if (belowY < 100 && belowY >= 0 && belowX >= 0 && belowX < 1000) {
if (isPassable(world[belowY][belowX])) {
// 可以下落
player.y = newY;
} else {
// 碰到实体方块
if (player.velocity > 0) {
// 落到方块上
player.y = newY - 1;
player.onGround = true;
player.canJump = true; // 落地后可以再次跳跃
} else {
// 碰到天花板
player.y = newY + 1;
}
player.velocity = 0.0f;
}
} else {
// 边界处理
if (newY < 0) {
player.y = 0;
player.velocity = 0.0f;
} else if (newY >= 100) {
player.y = 99;
player.velocity = 0.0f;
player.onGround = true;
player.canJump = true;
}
}
}
// 边界检查
player.x = max(-499, min(500, player.x));
player.y = max(0, min(99, player.y));
// 更新地面状态(防止波动)
int groundCheckX = player.x + 499;
int groundCheckY = player.y + 1;
if (groundCheckY < 100 && groundCheckY >= 0 && groundCheckX >= 0 && groundCheckX < 1000) {
player.onGround = isSolid(world[groundCheckY][groundCheckX]);
}
// 在水中时自动重置跳跃能力
if (player.inWater) {
player.canJump = true;
}
// 检查是否需要重绘
bool positionChanged = (player.y != newY);
bool stateChanged = (player.inWater != wasInWater) ||
(player.onGround != wasOnGround) ||
playerJumped;
if (playerMoved || positionChanged || stateChanged || blockChanged) {
needsRedraw = true;
}
}
// 构建整个屏幕并输出
if (needsRedraw) {
stringstream ss;
// 显示模式提示
if (inSelectionMode) {
if (isDiggingMode) {
ss << "挖掘模式: 用方向键选择方块,空格挖掘,ESC取消\n";
} else {
ss << "放置模式: 用方向键选择位置,空格放置,ESC取消\n";
}
} else {
// 添加空行以保持位置一致
ss << "\n";
}
// 渲染世界(在选择模式下传递选中位置)
if (inSelectionMode) {
renderWorld(ss, world, player, selectedX, selectedY);
} else {
renderWorld(ss, world, player);
}
// 清屏并输出整个帧
system("cls");
cout << ss.str();
needsRedraw = false;
}
// 控制帧率
Sleep(50);
}
return 0;
}