【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 天前
算法竞赛C++常用的STL
c++·算法
weilx12344 天前
C++笔记-文件IO-<fcntl.h>
c++
Smileyqp沛沛4 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车4 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光4 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·4 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁4 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven4 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
程序猿编码4 天前
告别改源码适配模型:纯 C++ 可配置 LLM 推理引擎,全格式全结构兼容
c++·大模型·llm·推理引擎
此生决int4 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++