主页
搜索
最近更新
数据统计
申请密钥
系统公告
1
/
1
请查看完所有公告
今天我们来打我的世界(C++版)
最后更新于 2025-07-31 10:42:56
作者
sunmingqian27
分类
个人记录
复制 Markdown
查看原文
删除文章
更新内容
#include <iostream>//用C++写一个简易版我的世界的代码,只用C++内部库。要有较多的功能,不要那么卡 #include <conio.h> #include <windows.h> #include <cstdlib> #include <ctime> #include <vector> #include <map> using namespace std; // 游戏常量 const int WIDTH = 60; const int HEIGHT = 30; const int VIEW_RADIUS = 8; const int INVENTORY_SIZE = 5; // 方块类型 enum BlockType { AIR, GRASS, STONE, WATER, SAND, WOOD, LEAVES, NUM_BLOCKS }; // 方块属性 struct Block { char symbol; string color; bool solid; bool fallable; }; // 方块属性配置 const Block BLOCK_DATA[] = { {' ', "\033[0m", false, false}, // AIR {'*', "\033[32m", true, false}, // GRASS {'#', "\033[37m", true, false}, // STONE {'~', "\033[36m", false, true}, // WATER {'%', "\033[33m", true, true}, // SAND {'H', "\033[31m", true, false}, // WOOD {'&', "\033[92m", false, false} // LEAVES }; // 游戏状态 BlockType world[HEIGHT][WIDTH]; int playerX = WIDTH/2; int playerY = HEIGHT/2; int health = 100; int gameTime = 0; int selectedSlot = 0; map<BlockType, int> inventory; bool isNight = false; struct Entity { int x, y; char type; // 'Z':僵尸 'C':鸡 int health; }; vector<Entity> entities; // 生成世界 void generateWorld() { srand(time(0)); for(int y=0; y<HEIGHT; y++) { for(int x=0; x<WIDTH; x++) { if(y > HEIGHT/2 + rand()%4) { world[y][x] = STONE; } else if(y == HEIGHT/2) { world[y][x] = GRASS; } else if(y < HEIGHT/2 - 5) { world[y][x] = (rand()%100 < 30) ? WATER : AIR; } else { world[y][x] = AIR; } // 生成树木 if(y == HEIGHT/2 && rand()%100 < 3 && x > 10 && x < WIDTH-10) { int height = 3 + rand()%3; for(int ty=y-1; ty>=y-height; ty--) world[ty][x] = WOOD; for(int tx=x-2; tx<=x+2; tx++) for(int ty=y-height-2; ty<=y-height; ty++) if(tx>=0 && tx<WIDTH && ty>=0) world[ty][tx] = LEAVES; } } } // 初始化物品栏 inventory[GRASS] = 99; inventory[STONE] = 10; inventory[WOOD] = 5; inventory[SAND] = 20; } // 物理更新 void updatePhysics() { // 沙子/水下落 for(int y=HEIGHT-2; y>=0; y--) { for(int x=0; x<WIDTH; x++) { BlockType bt = world[y][x]; if(BLOCK_DATA[bt].fallable) { if(world[y+1][x] == AIR) { world[y+1][x] = bt; world[y][x] = AIR; } else if(world[y+1][x] == WATER && bt == SAND) { swap(world[y][x], world[y+1][x]); } } } } } // 生成生物 void spawnEntities() { if(gameTime%100 == 0) { if(entities.size() < 5 && rand()%100 < (isNight ? 30 : 10)) { Entity e; e.x = playerX + (rand()%20 -10); e.y = playerY + (rand()%10 -5); e.type = (rand()%100 < 80) ? 'Z' : 'C'; e.health = (e.type == 'Z') ? 20 : 5; if(e.x >=0 && e.x < WIDTH && e.y >=0 && e.y < HEIGHT) entities.push_back(e); } } } // 实体AI void updateEntities() { for(auto& e : entities) { // 僵尸追逐玩家 if(e.type == 'Z' && !isNight) continue; int dx = playerX - e.x; int dy = playerY - e.y; if(abs(dx)+abs(dy) < 5) { // 攻击玩家 health -= 2; } else if(rand()%100 < 40) { e.x += (dx>0) ? 1 : (dx<0) ? -1 : 0; e.y += (dy>0) ? 1 : (dy<0) ? -1 : 0; } } } // 渲染界面 void render() { system("cls"); // 绘制视野范围 for(int y = playerY - VIEW_RADIUS; y <= playerY + VIEW_RADIUS; y++) { for(int x = playerX - VIEW_RADIUS; x <= playerX + VIEW_RADIUS; x++) { // 昼夜效果 if(isNight && (abs(x-playerX) + abs(y-playerY)) > VIEW_RADIUS/2) { cout << "\033[34m"; // 暗蓝色调 } if(x == playerX && y == playerY) { cout << "\033[33m@\033[0m"; } else if(x >=0 && x < WIDTH && y >=0 && y < HEIGHT) { Block b = BLOCK_DATA[world[y][x]]; cout << b.color << b.symbol << "\033[0m"; } else { cout << "\033[34m~\033[0m"; // 边界水域 } } cout << endl; } // 状态栏 cout << "生命: ["; for(int i=0; i<10; i++) cout << (health>i*10 ? "■" : " "); cout << "] 时间: " << gameTime/10 << " 昼夜: " << (isNight?"夜晚":"白天"); // 物品栏 cout << "\n物品栏:"; auto it = inventory.begin(); for(int i=0; i<INVENTORY_SIZE; i++) { if(it == inventory.end()) { cout << " [空]"; continue; } string color = BLOCK_DATA[it->first].color; cout << (i==selectedSlot ? ">" : " ") << color << it->first << "x" << it->second << "\033[0m "; ++it; } } // 玩家操作 void handleInput() { if(_kbhit()) { char c = _getch(); switch(tolower(c)) { case 'w': playerY--; break; case 's': playerY++; break; case 'a': playerX--; break; case 'd': playerX++; break; case ' ': { // 破坏方块 int tx = playerX, ty = playerY; if(world[ty][tx] != AIR) { inventory[world[ty][tx]]++; world[ty][tx] = AIR; } break; } case 'e': { // 放置方块 auto it = inventory.begin(); for(int i=0; i<selectedSlot; i++) ++it; if(it != inventory.end() && it->second >0) { world[playerY][playerX] = it->first; inventory[it->first]--; } break; } case 'q': selectedSlot = (selectedSlot +1) % INVENTORY_SIZE; break; } } } int main() { generateWorld(); while(health > 0) { gameTime++; isNight = (gameTime/100)%2; updatePhysics(); spawnEntities(); updateEntities(); handleInput(); render(); Sleep(50); } cout << "\n游戏结束!\n"; return 0; }
正在渲染内容...
点赞
0
收藏
0