• 个人简介

    三角洲:LQYloveLSZ 打火影的私信

    //#include #include <conio.h> #include <windows.h> #include #include

    using namespace std;

    // ==================== 常量 ==================== const int WORLD_W = 80; const int WORLD_H = 30; const int VIEW_W = 40; const int VIEW_H = 20;

    // 方块类型 const int AIR = 0; const int GRASS = 1; const int DIRT = 2; const int STONE = 3; const int WOOD = 4; const int LEAF = 5; const int BEDROCK = 6;

    // 方块显示 struct BlockInfo { char symbol; int color; // Windows 控制台颜色 };

    BlockInfo BLOCKS[7] = { {' ', 0}, // AIR {'.', 10}, // GRASS 绿色 {',', 6}, // DIRT 黄色 {'#', 8}, // STONE 灰色 {'|', 6}, // WOOD 黄色 {'%', 2}, // LEAF 深绿 {'@', 4} // BEDROCK 红色 };

    // ==================== 游戏状态 ==================== struct Game { int world[WORLD_H][WORLD_W]; int px, py; // 玩家位置 int vx, vy; // 速度 bool onGround; int inventory[7]; // 每种方块数量 int selected; // 当前选中物品 int dayTime; // 0~999 昼夜循环 bool running; };

    // ==================== 地形生成 ==================== void generateWorld(Game& g) { srand((unsigned)time(0));

    for (int y = 0; y < WORLD_H; y++)
        for (int x = 0; x < WORLD_W; x++)
            g.world[y][x] = AIR;
    
    // 简单地形:中间高,两边低
    int base = WORLD_H / 2;
    for (int x = 0; x < WORLD_W; x++) {
        int h = base + (rand() % 5 - 2);
        if (h < 5) h = 5;
        if (h > WORLD_H - 4) h = WORLD_H - 4;
    
        for (int y = h; y < WORLD_H; y++) {
            if (y == WORLD_H - 1)
                g.world[y][x] = BEDROCK;
            else if (y == h)
                g.world[y][x] = GRASS;
            else if (y < h + 3)
                g.world[y][x] = DIRT;
            else
                g.world[y][x] = STONE;
        }
    
        // 随机生成树
        if (x > 3 && x < WORLD_W - 3 && rand() % 12 == 0) {
            int groundY = h - 1;
            if (g.world[groundY][x] == AIR) {
                // 树干
                for (int i = 0; i < 3; i++)
                    if (groundY - i >= 0)
                        g.world[groundY - i][x] = WOOD;
                // 树叶
                for (int dy = -2; dy <= 0; dy++)
                    for (int dx = -1; dx <= 1; dx++) {
                        int ly = groundY - 3 + dy;
                        int lx = x + dx;
                        if (ly >= 0 && lx >= 0 && lx < WORLD_W)
                            if (g.world[ly][lx] == AIR)
                                g.world[ly][lx] = LEAF;
                    }
            }
        }
    }
    
    // 玩家出生点
    g.px = WORLD_W / 2;
    g.py = 0;
    while (g.py < WORLD_H && g.world[g.py][g.px] == AIR)
        g.py++;
    if (g.py > 0) g.py--;
    
    g.vx = 0;
    g.vy = 0;
    g.onGround = false;
    g.selected = 1;
    g.dayTime = 0;
    g.running = true;
    
    for (int i = 0; i < 7; i++)
        g.inventory[i] = 0;
    g.inventory[WOOD] = 8;
    g.inventory[STONE] = 8;
    

    }

    // ==================== 碰撞检测 ==================== bool isSolid(int block) { return block != AIR; }

    bool collides(Game& g, int x, int y) { if (x < 0 || x >= WORLD_W || y < 0 || y >= WORLD_H) return true; return isSolid(g.world[y][x]); }

    // ==================== 物理更新 ==================== void updatePhysics(Game& g) { const int GRAVITY = 1; const int JUMP = -3; const int SPEED = 1;

    // 水平移动
    int nx = g.px + g.vx;
    if (!collides(g, nx, g.py) && !collides(g, nx, g.py + 1))
        g.px = nx;
    
    // 垂直移动
    g.vy += GRAVITY;
    if (g.vy > 4) g.vy = 4;
    
    int ny = g.py + g.vy;
    if (!collides(g, g.px, ny) && !collides(g, g.px + 1, ny)) {
        g.py = ny;
        g.onGround = false;
    } else {
        if (g.vy > 0)
            g.onGround = true;
        g.vy = 0;
    }
    
    g.vx = 0;
    

    }

    // ==================== 挖掘 / 放置 ==================== void mineBlock(Game& g, int tx, int ty) { if (tx < 0 || tx >= WORLD_W || ty < 0 || ty >= WORLD_H) return; int b = g.world[ty][tx]; if (b == AIR || b == BEDROCK) return; g.inventory[b]++; g.world[ty][tx] = AIR; }

    void placeBlock(Game& g, int tx, int ty, int type) { if (tx < 0 || tx >= WORLD_W || ty < 0 || ty >= WORLD_H) return; if (g.world[ty][tx] != AIR) return; if (type <= AIR || type > BEDROCK) return; if (g.inventory[type] <= 0) return;

    // 不能放在玩家身体位置
    if ((tx == g.px || tx == g.px + 1) &&
        (ty == g.py || ty == g.py + 1))
        return;
    
    g.world[ty][tx] = type;
    g.inventory[type]--;
    

    }

    // ==================== 输入处理 ==================== void handleInput(Game& g) { if (_kbhit()) { char key = _getch();

        switch (key) {
        case 'a': case 'A':
            g.vx = -1;
            break;
        case 'd': case 'D':
            g.vx = 1;
            break;
        case ' ':
            if (g.onGround)
                g.vy = -3;
            break;
        case 'q': case 'Q':
            g.running = false;
            break;
        case 'e': case 'E':
            g.selected++;
            if (g.selected > BEDROCK)
                g.selected = 1;
            break;
        case 'r': case 'R':
            // 挖掘玩家前方方块
            {
                int dir = (g.vx >= 0) ? 1 : -1;
                mineBlock(g, g.px + dir, g.py);
            }
            break;
        case 'f': case 'F':
            // 放置选中方块
            {
                int dir = (g.vx >= 0) ? 1 : -1;
                placeBlock(g, g.px + dir, g.py, g.selected);
            }
            break;
        case '1': g.selected = 1; break;
        case '2': g.selected = 2; break;
        case '3': g.selected = 3; break;
        case '4': g.selected = 4; break;
        case '5': g.selected = 5; break;
        }
    }
    

    }

    // ==================== 渲染 ==================== void draw(Game& g) { system("cls");

    // 昼夜颜色
    bool night = (g.dayTime > 500);
    int bg = night ? 0 : 1; // 0黑 1蓝
    
    // 计算视野范围
    int startX = g.px - VIEW_W / 2;
    int startY = g.py - VIEW_H / 2 + 2;
    
    if (startX < 0) startX = 0;
    if (startY < 0) startY = 0;
    if (startX + VIEW_W > WORLD_W) startX = WORLD_W - VIEW_W;
    if (startY + VIEW_H > WORLD_H) startY = WORLD_H - VIEW_H;
    
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    
    for (int vy = 0; vy < VIEW_H; vy++) {
        for (int vx = 0; vx < VIEW_W; vx++) {
            int wx = startX + vx;
            int wy = startY + vy;
    
            char ch = ' ';
            int col = 7;
    
            if (wx >= 0 && wx < WORLD_W && wy >= 0 && wy < WORLD_H) {
                int block = g.world[wy][wx];
                ch = BLOCKS[block].symbol;
                col = BLOCKS[block].color;
                if (col == 0) col = 7;
            }
    
            // 玩家
            if ((wx == g.px && wy == g.py) ||
                (wx == g.px + 1 && wy == g.py) ||
                (wx == g.px && wy == g.py + 1) ||
                (wx == g.px + 1 && wy == g.py + 1)) {
                ch = 'P';
                col = 14;
            }
    
            SetConsoleTextAttribute(h, col | (bg << 4));
            cout << ch;
        }
        cout << endl;
    }
    
    // UI
    SetConsoleTextAttribute(h, 15);
    cout << "WASD移动 空格跳跃 R挖掘 F放置 E切换方块 1-5选择 Q退出" << endl;
    
    const char* NAMES[7] = {
        "空气","草方","泥土","石头","木头","树叶","基岩"
    };
    
    cout << "选中: " << NAMES[g.selected]
         << " 数量: " << g.inventory[g.selected]
         << " 昼夜: " << (night ? "夜晚" : "白天")
         << endl;
    
    cout << "背包: ";
    for (int i = 1; i <= 5; i++) {
        SetConsoleTextAttribute(h, BLOCKS[i].color | (bg << 4));
        cout << "[" << NAMES[i] << ":" << g.inventory[i] << "] ";
    }
    cout << endl;
    

    }

    // ==================== 主循环 ==================== int main() { Game g; generateWorld(g);

    cout << "===== 控制台版 Minecraft =====" << endl;
    cout << "按任意键开始..." << endl;
    _getch();
    
    while (g.running) {
        handleInput(g);
        updatePhysics(g);
        draw(g);
    
        g.dayTime = (g.dayTime + 1) % 1000;
    
        Sleep(100);
    }
    
    system("cls");
    cout << "游戏结束,感谢游玩!" << endl;
    return 0;
    

    }

  • 通过的题目

  • 最近活动

    This person is lazy and didn't join any contests or homework.
  • 最近编写的题解

    This person is lazy and didn't write any solutions.