主页
搜索
最近更新
数据统计
申请密钥
系统公告
1
/
1
请查看完所有公告
我的世界2D小游戏
最后更新于 2025-07-31 10:44:06
作者
sunmingqian27
分类
个人记录
复制 Markdown
查看原文
删除文章
更新内容
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <conio.h> #include <windows.h> using namespace std; const int WORLD_WIDTH = 40; const int WORLD_HEIGHT = 20; const int VIEW_DISTANCE = 10; enum BlockType { AIR, DIRT, STONE, BEDROCK }; class Block { private: BlockType type; public: Block(BlockType t = AIR) : type(t) {} char getChar() const { switch(type) { case DIRT: return '*'; case STONE: return '#'; case BEDROCK: return 'X'; default: return ' '; } } bool isSolid() const { return type != AIR; } BlockType getType() const { return type; } void setType(BlockType t) { type = t; } }; class World { private: vector<vector<Block>> blocks; void generateTerrain() { // 生成基础地形 for (int x = 0; x < WORLD_WIDTH; x++) { int height = WORLD_HEIGHT/2 + rand()%5 - 2; for (int y = 0; y < WORLD_HEIGHT; y++) { if (y > height) { blocks[x][y].setType(AIR); } else if (y == height) { blocks[x][y].setType(DIRT); } else if (y > height - 3) { blocks[x][y].setType(DIRT); } else { blocks[x][y].setType(STONE); } } // 基岩层 blocks[x][WORLD_HEIGHT-1].setType(BEDROCK); } } public: World() : blocks(WORLD_WIDTH, vector<Block>(WORLD_HEIGHT)) { srand(time(0)); generateTerrain(); } Block& getBlock(int x, int y) { if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) return blocks[0][0]; // 返回边界保护 return blocks[x][y]; } }; class Player { private: int x, y; BlockType inventory; public: Player(int startX, int startY) : x(startX), y(startY), inventory(DIRT) {} void move(int dx, int dy, World& world) { int newX = x + dx; int newY = y + dy; if (newX >= 0 && newX < WORLD_WIDTH && newY >= 0 && newY < WORLD_HEIGHT && !world.getBlock(newX, newY).isSolid()) { x = newX; y = newY; } } void placeBlock(World& world) { int targetX = x; int targetY = y + 1; // 下方放置 if (!world.getBlock(targetX, targetY).isSolid()) { world.getBlock(targetX, targetY).setType(inventory); } } void breakBlock(World& world) { int targetX = x; int targetY = y + 1; // 挖掘下方 if (world.getBlock(targetX, targetY).getType() != BEDROCK) { world.getBlock(targetX, targetY).setType(AIR); } } void draw(World& world) { system("cls"); // 绘制玩家周围区域 for (int dy = -VIEW_DISTANCE; dy <= VIEW_DISTANCE; dy++) { for (int dx = -VIEW_DISTANCE; dx <= VIEW_DISTANCE; dx++) { int wx = x + dx; int wy = y + dy; if (wx == x && wy == y) { cout << '@'; } else if (wx >= 0 && wx < WORLD_WIDTH && wy >= 0 && wy < WORLD_HEIGHT) { cout << world.getBlock(wx, wy).getChar(); } else { cout << ' '; } } cout << endl; } cout << "Inventory: "; switch(inventory) { case DIRT: cout << "Dirt"; break; case STONE: cout << "Stone"; break; default: cout << "Empty"; break; } cout << "\nControls: WASD - Move | E - Place | Q - Break | 1/2 - Change Block"; } }; int main() { World world; Player player(WORLD_WIDTH/2, WORLD_HEIGHT/2); while(true) { player.draw(world); if (_kbhit()) { char input = _getch(); switch(tolower(input)) { case 'w': player.move(0, -1, world); break; case 's': player.move(0, 1, world); break; case 'a': player.move(-1, 0, world); break; case 'd': player.move(1, 0, world); break; case 'e': player.placeBlock(world); break; case 'q': player.breakBlock(world); break; case '1': player.setInventory(DIRT); break; case '2': player.setInventory(STONE); break; } } Sleep(50); } return 0; }
正在渲染内容...
点赞
0
收藏
0