【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;
}    
相关推荐
孞㐑¥4 小时前
Linux之Socket 编程 UDP
linux·服务器·c++·经验分享·笔记·网络协议·udp
水木兰亭7 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
CoderCodingNo8 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法
秋风&萧瑟10 小时前
【C++】C++中的友元函数和友元类
c++
梁诚斌10 小时前
使用OpenSSL接口读取pem编码格式文件中的证书
开发语言·c++
2301_8035545214 小时前
c++中的绑定器
开发语言·c++·算法
海棠蚀omo14 小时前
C++笔记-位图和布隆过滤器
开发语言·c++·笔记
消失的旧时光-194315 小时前
c++ 的标准库 --- std::
c++·jni
GiraKoo15 小时前
【GiraKoo】C++11的新特性
c++·后端
不午睡的探索者15 小时前
告别性能瓶颈!Python 量化工程师,进击 C++ 高性能量化交易的“必修课”!
c++·github