专栏文章

我的世界2D小游戏

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@miq8m8zu
此快照首次捕获于
2025/12/04 00:44
3 个月前
此快照最后确认于
2025/12/04 00:44
3 个月前
查看原文
#include #include #include #include #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) {}
CPP
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> blocks;
CPP
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(WORLD_HEIGHT)) { srand(time(0)); generateTerrain(); }
CPP
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) {}
CPP
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);
CPP
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 条评论,欢迎与作者交流。

正在加载评论...