问题标题: c++我的世界代码

0
0
已解决
高煜婷
高煜婷
初级守护
初级守护

#include <iostream> #include <vector> #include <windows.h> #include <conio.h> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; // 窗口常量 const int MAP_W = 50; const int MAP_H = 25; // MC方块定义 enum Block { AIR = 0, GRASS, // 草方块 DIRT, // 泥土 STONE, // 石头 WOOD, // 原木 LEAF, // 树叶 WATER // 水 }; // 方块显示字符 + 控制台颜色 struct BlockInfo { char ch; int color; } blockData[] = { {' ', 7}, // 空气 {'#', 10}, // 草 绿色 {'▒', 6}, // 泥土 土黄 {'■', 8}, // 石头 灰白 {'▓', 14}, // 木头 浅黄 {'◆', 11}, // 树叶 浅绿 {'≈', 9} // 水 蓝色 }; // 玩家结构体 struct Player { int x, y; int selectBlock; bool onGround; // 是否站在地面(重力判定) } player; vector<vector<int>> world(MAP_H, vector<int>(MAP_W, AIR)); bool isDay = true; // 设置控制台文字颜色 void setColor(int c) { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(h, c); } // 关闭光标闪烁 void hideCursor() { CONSOLE_CURSOR_INFO cur = {1, FALSE}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur); } // 生成自然地形(仿MC地层:表层草→泥土→石头基岩) void generateTerrain() { srand((unsigned)time(nullptr)); // 底层全石头 for (int y = MAP_H - 4; y < MAP_H; y++) for (int x = 0; x < MAP_W; x++) world[y][x] = STONE; // 随机起伏地表 for (int x = 0; x < MAP_W; x++) { int groundH = MAP_H - 5 - rand() % 3; world[groundH][x] = GRASS; world[groundH + 1][x] = DIRT; world[groundH + 2][x] = DIRT; } // 玩家出生点 player.x = 8; player.y = MAP_H - 8; player.selectBlock = GRASS; } // 坐标碰撞检测:实心方块无法穿过 bool isSolid(int x, int y) { if (x < 0 || x >= MAP_W || y < 0 || y >= MAP_H) return true; return world[y][x] != AIR; } // 重力**(MC核心下落逻辑) void gravityUpdate() { if (!isSolid(player.x, player.y + 1)) { player.y++; player.onGround = false; } else { player.onGround = true; } } // 渲染游戏画面 void render() { system("cls"); int backColor = isDay ? 7 : 15; setColor(backColor); for (int y = 0; y < MAP_H; y++) { for (int x = 0; x < MAP_W; x++) { // 绘制玩家 if (x == player.x && y == player.y) { setColor(12); cout << "@"; setColor(backColor); } else { int bType = world[y][x]; setColor(blockData[bType].color); cout << blockData[bType].ch; setColor(backColor); } } cout << endl; } // 底部状态栏 setColor(14); cout << "\n==== MC控制台版 ====" << endl; cout << "W跳跃 | A/D左右移动 | 空格放置方块 | Z挖掘方块" << endl; cout << "1草 2泥土 3石头 4木头 5树叶 6水 | T切换昼夜 | S存档 L读档 | ESC退出" << endl; cout << "手持方块:"; switch (player.selectBlock) { case GRASS: cout << "草方块"; break; case DIRT: cout << "泥土"; break; case STONE: cout << "石头"; break; case WOOD: cout << "原木"; break; case LEAF: cout << "树叶"; break; case WATER: cout << "水"; break; } cout << " | 时间:" << (isDay ? "白天" : "夜晚") << endl; setColor(7); } // 键盘输入处理 void keyInput() { if (!_kbhit()) return; char key = _getch(); int nx = player.x; int ny = player.y; switch (tolower(key)) { // 左右移动 case 'a': nx--; break; case 'd': nx++; break; // 跳跃(只有在地面才能跳) case 'w': if (player.onGround) ny -= 3; break; // 选择方块 case '1': player.selectBlock = GRASS; return; case '2': player.selectBlock = DIRT; return; case '3': player.selectBlock = STONE; return; case '4': player.selectBlock = WOOD; return; case '5': player.selectBlock = LEAF; return; case '6': player.selectBlock = WATER; return; // 放置方块(脚下前方) case ' ': if (!isSolid(player.x, player.y + 1)) world[player.y + 1][player.x] = player.selectBlock; return; // 挖掘消除方块 case 'z': if (!isSolid(player.x, player.y + 1)) world[player.y + 1][player.x] = AIR; return; // 昼夜切换 case 't': isDay = !isDay; return; // 存档 case 's': { ofstream save("mc_save.dat"); for (auto& row : world) for (int b : row) save << b << " "; save.close(); return; } // 读档 case 'l': { ifstream load("mc_save.dat"); if (load.is_open()) { for (auto& row : world) for (int& b : row) load >> b; load.close(); } return; } // ESC退出程序 case 27: exit(0); } // 无碰撞才允许移动 if (!isSolid(nx, ny)) { player.x = nx; player.y = ny; } } int main() { hideCursor(); generateTerrain(); // 游戏主循环 while (true) { render(); gravityUpdate(); keyInput(); Sleep(40); } return 0; }


0
已采纳
陶笛龙
陶笛龙
资深守护
资深守护

这波考验你的眼睛近视度数

0
于行衍
于行衍
高级天翼
高级天翼

你能不能换一下行

复制出来是这样的

0
0
王一行
王一行
新手光能
新手光能

能发一下换过行的代码吗?

我要回答