#include <iostream> #include <vector> #include <conio.h> #include <windows.h> #include <ctime> #include <fstream> using namespace std; // 地图常量 const int MAP_WIDTH = 40; const int MAP_HEIGHT = 20; // 方块类型 enum BlockType { AIR = 0, GRASS, STONE, WOOD, WATER }; // 方块字符显示 char BlockChar[] = {' ', '#', '■', '▓', '≈'}; // 颜色 void SetColor(int color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, color); } // 全局地图 vector<vector<int>> world(MAP_HEIGHT, vector<int>(MAP_WIDTH, AIR)); // 玩家数据 struct Player { int x, y; int selectBlock; // 当前手持方块 }player; bool dayTime = true; // 昼夜 // 初始化地形(底层石头,表层草地) void InitWorld() { for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { if (y >= MAP_HEIGHT - 3) world[y][x] = STONE; if (y == MAP_HEIGHT - 4) world[y][x] = GRASS; } } // 初始玩家位置 player.x = 5; player.y = MAP_HEIGHT - 6; player.selectBlock = GRASS; } // 渲染整个游戏画面 void Render() { system("cls"); // 昼夜颜色 if (dayTime) SetColor(7); else SetColor(8); for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { // 玩家位置 if (x == player.x && y == player.y) { SetColor(12); cout << "@"; SetColor(dayTime ? 7 : 8); } else { // 不同方块不同颜色 switch (world[y][x]) { case GRASS: SetColor(10); break; case STONE: SetColor(7); break; case WOOD: SetColor(6); break; case WATER: SetColor(9); break; default: break; } cout << BlockChar[world[y][x]]; SetColor(dayTime ? 7 : 8); } } cout << endl; } // 底部状态栏 SetColor(14); cout << "【WASD移动 | 左键放置方块 | 右键清除方块 | 1草 2石头 3木头 4水 | T切换昼夜 | S存档 L读档 | ESC退出】" << endl; cout << "当前选中方块:"; switch (player.selectBlock) { case GRASS: cout << "草地"; break; case STONE: cout << "石头"; break; case WOOD: cout << "木头"; break; case WATER: cout << "水"; break; } cout << " | 时间:" << (dayTime ? "白天" : "黑夜") << endl; SetColor(7); } // 碰撞检测:不能走进实体方块 bool CheckCollision(int x, int y) { if (x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT) return true; return world[y][x] != AIR; } // 键盘逻辑 void KeyControl() { if (!_kbhit()) return; char key = _getch(); int nx = player.x, ny = player.y; switch (tolower(key)) { case 'w': ny--; break; case 's': ny++; break; case 'a': nx--; break; case 'd': nx++; break; case '1': player.selectBlock = GRASS; return; case '2': player.selectBlock = STONE; return; case '3': player.selectBlock = WOOD; return; case '4': player.selectBlock = WATER; return; case 't': dayTime = !dayTime; return; case 27: exit(0); // ESC退出 // 放置方块(空格) case ' ': if (!CheckCollision(player.x, player.y - 1)) world[player.y - 1][player.x] = player.selectBlock; return; // 销毁方块(X) case 'x': if (!CheckCollision(player.x, player.y - 1)) world[player.y - 1][player.x] = AIR; return; // 存档 S case 's': { ofstream f("world_save.dat"); for (auto &row : world) for (int b : row) f << b << " "; f.close(); return; } // 读档 L case 'l': { ifstream f("world_save.dat"); if (f.is_open()) { for (auto &row : world) for (int &b : row) f >> b; f.close(); } return; } } // 移动无碰撞才更新坐标 if (!CheckCollision(nx, ny)) { player.x = nx; player.y = ny; } } int main() { // 关闭光标闪烁 CONSOLE_CURSOR_INFO cursor = {1, FALSE}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); InitWorld(); while (true) { Render(); KeyControl(); Sleep(30); } return 0; }

