【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;
}    
相关推荐
apocelipes21 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
郝学胜_神的一滴2 天前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
见过夏天3 天前
C++ 基础入门完全指南
c++
用户805533698034 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK5 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境5 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境5 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴6 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境8 天前
C++ 的Eigen 库全解析
c++
卷无止境8 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端