东西
2026-11-05 16:29:55
发布于:浙江
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
// --- 常量定义 ---
const int WORLD_WIDTH = 60;
const int WORLD_HEIGHT = 20;
const int VIEW_DISTANCE_X = 15; // 视野宽度半径
const int VIEW_DISTANCE_Y = 8; // 视野高度半径
// --- 方块类型枚举 ---
enum BlockType {
AIR = 0,
GRASS = 1,
DIRT = 2,
STONE = 3,
WOOD = 4,
LEAVES = 5,
BEDROCK = 99
};
// --- 全局变量 ---
int world[WORLD_HEIGHT][WORLD_WIDTH];
int playerX, playerY;
int selectedBlock = DIRT; // 当前手持方块
bool gameRunning = true;
// --- 辅助函数:设置控制台光标位置 ---
void setCursor(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// --- 辅助函数:隐藏光标 ---
void hideCursor() {
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.dwSize = 100;
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
// --- 世界生成 ---
void generateWorld() {
srand(time(0));
for (int x = 0; x < WORLD_WIDTH; ++x) {
// 简单地形生成:基础高度 + 随机波动
int surfaceHeight = WORLD_HEIGHT - 5 - (rand() % 3);
for (int y = 0; y < WORLD_HEIGHT; ++y) {
if (y == WORLD_HEIGHT - 1) {
world[y][x] = BEDROCK; // 基岩层
} else if (y > surfaceHeight + 3) {
world[y][x] = STONE; // 深层石头
} else if (y > surfaceHeight) {
world[y][x] = DIRT; // 泥土层
} else if (y == surfaceHeight) {
world[y][x] = GRASS; // 草方块
} else {
world[y][x] = AIR; // 空气
}
}
// 随机生成树木 (简单逻辑:1/10概率在草地上生成树)
if (world[surfaceHeight][x] == GRASS && rand() % 10 == 0 && x > 2 && x < WORLD_WIDTH - 2) {
int treeHeight = 3 + rand() % 2;
for (int h = 1; h <= treeHeight; ++h) {
if (surfaceHeight - h >= 0) world[surfaceHeight - h][x] = WOOD;
}
// 树叶
if (surfaceHeight - treeHeight - 1 >= 0) {
world[surfaceHeight - treeHeight - 1][x] = LEAVES;
if(x>0) world[surfaceHeight - treeHeight - 1][x-1] = LEAVES;
if(x<WORLD_WIDTH-1) world[surfaceHeight - treeHeight - 1][x+1] = LEAVES;
}
}
}
// 初始化玩家位置 (放在地表上方)
playerX = WORLD_WIDTH / 2;
playerY = 0;
// 寻找玩家脚下的地面
while (playerY < WORLD_HEIGHT && world[playerY][playerX] == AIR) {
playerY++;
}
playerY--; // 站在地面上
}
// --- 渲染引擎 ---
void render() {
system("cls"); // 清屏
// 计算视野范围
int startX = std::max(0, playerX - VIEW_DISTANCE_X);
int endX = std::min(WORLD_WIDTH - 1, playerX + VIEW_DISTANCE_X);
int startY = std::max(0, playerY - VIEW_DISTANCE_Y);
int endY = std::min(WORLD_HEIGHT - 1, playerY + VIEW_DISTANCE_Y);
// 绘制顶部信息栏
std::cout << "=== Console Minecraft ===" << std::endl;
std::cout << "Pos: (" << playerX << ", " << playerY << ") | Block: ";
switch(selectedBlock) {
case DIRT: std::cout << "Dirt [1]"; break;
case STONE: std::cout << "Stone [2]"; break;
case WOOD: std::cout << "Wood [3]"; break;
case GRASS: std::cout << "Grass [4]"; break;
default: std::cout << "Unknown";
}
std::cout << std::endl;
std::cout << "Controls: WASD Move, Space Jump, E Place, Q Break, 1-4 Select Block, Esc Quit" << std::endl;
std::cout << "-------------------------" << std::endl;
// 绘制世界
for (int y = startY; y <= endY; ++y) {
for (int x = startX; x <= endX; ++x) {
// 计算相对于屏幕的坐标
int screenX = x - startX;
int screenY = y - startY + 2; // +2 偏移量留给标题
if (x == playerX && y == playerY) {
std::cout << "\033[32m@\033[0m"; // 绿色玩家
} else {
switch (world[y][x]) {
case AIR: std::cout << " "; break;
case GRASS: std::cout << "\033[32m#\033[0m"; break; // 绿色
case DIRT: std::cout << "\033[33m.\033[0m"; break; // 黄色
case STONE: std::cout << "\033[37m+\033[0m"; break; // 白色
case WOOD: std::cout << "\033[33m|\033[0m"; break; // 棕色模拟
case LEAVES: std::cout << "\033[32m%\033[0m"; break;// 绿色
case BEDROCK: std::cout << "\033[30mX\033[0m"; break;// 黑色
default: std::cout << "?";
}
}
}
std::cout << std::endl;
}
}
// --- 物理与碰撞检测 ---
bool isSolid(int x, int y) {
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) return true; // 边界视为固体
return world[y][x] != AIR;
}
void updatePhysics() {
// 简单的重力模拟
if (!isSolid(playerX, playerY + 1)) {
playerY++;
}
}
// --- 游戏主循环 ---
void gameLoop() {
generateWorld();
hideCursor();
while (gameRunning) {
render();
// 非阻塞输入检查
if (_kbhit()) {
char key = _getch();
// 移动逻辑
if (key == 'a' || key == 'A') {
if (!isSolid(playerX - 1, playerY)) playerX--;
} else if (key == 'd' || key == 'D') {
if (!isSolid(playerX + 1, playerY)) playerX++;
} else if (key == 'w' || key == 'W') {
// 简单跳跃:如果脚下是固体且头顶是空气
if (isSolid(playerX, playerY + 1) && !isSolid(playerX, playerY - 1)) {
playerY -= 2;
}
} else if (key == 's' || key == 'S') {
// 下蹲/下降(如果下方有空间)
if (!isSolid(playerX, playerY + 1)) playerY++;
}
// 交互逻辑
else if (key == 'e' || key == 'E') {
// 放置方块:尝试放置在玩家右侧或前方(简化为右侧)
int targetX = playerX + 1;
int targetY = playerY;
if (targetX >= 0 && targetX < WORLD_WIDTH && targetY >= 0 && targetY < WORLD_HEIGHT) {
if (world[targetY][targetX] == AIR) {
world[targetY][targetX] = selectedBlock;
}
}
} else if (key == 'q' || key == 'Q') {
// 破坏方块:破坏玩家右侧
int targetX = playerX + 1;
int targetY = playerY;
if (targetX >= 0 && targetX < WORLD_WIDTH && targetY >= 0 && targetY < WORLD_HEIGHT) {
if (world[targetY][targetX] != BEDROCK) {
world[targetY][targetX] = AIR;
}
}
}
// 物品栏选择
else if (key == '1') selectedBlock = DIRT;
else if (key == '2') selectedBlock = STONE;
else if (key == '3') selectedBlock = WOOD;
else if (key == '4') selectedBlock = GRASS;
// 退出
else if (key == 27) { // ESC
gameRunning = false;
}
}
updatePhysics();
Sleep(100); // 控制帧率,避免CPU占用过高
}
}
int main() {
// 设置控制台编码以支持颜色(可选,Windows 10+ 支持 ANSI)
system("chcp 65001 > nul");
stdcout << "Starting Console Minecraft..." << stdendl;
Sleep(1000);
gameLoop();
std::cout << "Game Over. Thanks for playing!" << std::endl;
return 0;
}
这里空空如也


















有帮助,赞一个