欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > 【c++】 我的世界

【c++】 我的世界

2025/5/9 1:23:00 来源:https://blog.csdn.net/AI_int/article/details/147773835  浏览:    关键词:【c++】 我的世界

太久没更新小游戏了

给个赞和收藏吧,求求了

要游戏的请私聊我

#include <iostream>
#include <vector>// 定义世界大小
const int WORLD_WIDTH = 20;
const int WORLD_HEIGHT = 10;// 定义方块类型
enum BlockType {AIR,GRASS,DIRT,STONE
};// 定义世界
std::vector<std::vector<BlockType>> world(WORLD_HEIGHT, std::vector<BlockType>(WORLD_WIDTH, AIR));// 生成地形
void generateTerrain() {for (int x = 0; x < WORLD_WIDTH; ++x) {int groundLevel = WORLD_HEIGHT / 2;world[groundLevel][x] = GRASS;for (int y = groundLevel + 1; y < WORLD_HEIGHT; ++y) {if (y < groundLevel + 3) {world[y][x] = DIRT;} else {world[y][x] = STONE;}}}
}// 打印世界
void printWorld(int playerX, int playerY) {for (int y = 0; y < WORLD_HEIGHT; ++y) {for (int x = 0; x < WORLD_WIDTH; ++x) {if (x == playerX && y == playerY) {std::cout << 'P';} else {switch (world[y][x]) {case AIR:std::cout << ' ';break;case GRASS:std::cout << 'G';break;case DIRT:std::cout << 'D';break;case STONE:std::cout << 'S';break;}}}std::cout << std::endl;}
}int main() {generateTerrain();int playerX = WORLD_WIDTH / 2;int playerY = WORLD_HEIGHT / 2 - 1;while (true) {system("cls"); // 清屏,在 Linux 系统中使用 system("clear");printWorld(playerX, playerY);std::cout << "使用 w(上), s(下), a(左), d(右) 移动,q 退出: ";char input;std::cin >> input;if (input == 'q') {break;}int newX = playerX;int newY = playerY;switch (input) {case 'w':newY--;break;case 's':newY++;break;case 'a':newX--;break;case 'd':newX++;break;}// 检查新位置是否合法if (newX >= 0 && newX < WORLD_WIDTH && newY >= 0 && newY < WORLD_HEIGHT) {playerX = newX;playerY = newY;}}return 0;
}    

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词