【c++】 我的世界

太久没更新小游戏了

给个赞和收藏吧,求求了

要游戏的请私聊我

cpp 复制代码
#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;
}    
相关推荐
端平入洛1 天前
auto有时不auto
c++
哇哈哈20212 天前
信号量和信号
linux·c++
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马2 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝2 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc2 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼2 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx2 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX2 天前
020-C++之unordered容器
数据结构·c++
会编程的土豆2 天前
2.25 做题
数据结构·c++·算法