手把手教你制作C++农场游戏

引言

农场类游戏因其轻松愉快的玩法和简单的机制,成为编程初学者的理想练习项目。本文将带你一步步用C++实现一个基础农场游戏,涵盖面向对象编程、游戏循环和用户交互等核心概念。

游戏功能规划

  1. 作物生长系统‌:模拟不同作物的生长周期
  2. 土地管理‌:5x5的可种植土地网格
  3. 时间推进‌:按天计算作物生长进度
  4. 用户交互‌:通过菜单进行操作

代码实现

1. 基类与派生类设计

class Crop {
protected:
    std::string name;
    int growthDays;
    float buyPrice;
    float sellPrice;
    int currentGrowth;
public:
    virtual void grow() = 0;
    virtual char getSymbol() const = 0;
    virtual ~Crop() = default;
};

class Wheat : public Crop {
public:
    Wheat() {
        name = "Wheat";
        growthDays = 3;
        buyPrice = 10;
        sellPrice = 15;
        currentGrowth = 0;
    }
    void grow() override {
        if(currentGrowth < growthDays) currentGrowth++;
    }
    char getSymbol() const override {
        return currentGrowth == growthDays ? 'W' : 'w';
    }
};

2. 农场管理系统

class Farm {
private:
    std::vector<std::vector<std::unique_ptr<Crop>>> land;
    int width;
    int height;
public:
    Farm(int w, int h) : width(w), height(h), land(h, std::vector<std::unique_ptr<Crop>>(w)) {}
    
    void plantCrop(int x, int y, Crop* crop) {
        if(x >= 0 && x < width && y >= 0 && y < height) {
            land[y][x].reset(crop);
        }
    }
    
    void update() {
        for(auto& row : land) {
            for(auto& crop : row) {
                if(crop) crop->grow();
            }
        }
    }
    
    void display() const {
        for(const auto& row : land) {
            for(const auto& crop : row) {
                std::cout << (crop ? crop->getSymbol() : '.') << " ";
            }
            std::cout << std::endl;
        }
    }
};

3. 游戏主循环

class GameEngine {
private:
    Farm farm;
    int dayCount;
public:
    GameEngine() : farm(5, 5), dayCount(0) {}
    
    void run() {
        while(true) {
            displayMenu();
            handleInput();
            updateGame();
        }
    }
    
    void displayMenu() const {
        std::cout << "\nDay " << dayCount << "\n";
        farm.display();
        std::cout << "\n1. Plant wheat\n2. Next day\n3. Exit\n";
    }
    
    void handleInput() {
        int choice;
        std::cin >> choice;
        
        switch(choice) {
            case 1: {
                int x, y;
                std::cout << "Enter position (x y): ";
                std::cin >> x >> y;
                farm.plantCrop(x, y, new Wheat());
                break;
            }
            case 3:
                exit(0);
        }
    }
    
    void updateGame() {
        farm.update();
        dayCount++;
    }
};

int main() {
    GameEngine engine;
    engine.run();
    return 0;
}

游戏玩法说明

  1. 启动游戏后,会显示5x5的空白土地
  2. 选择1种植小麦,输入坐标(x y)
  3. 选择2推进到下一天,观察作物生长
  4. 选择3退出游戏

扩展建议

  1. 添加更多作物类型(玉米、胡萝卜等)
  2. 实现天气系统影响生长速度
  3. 添加经济系统和商店功能
  4. 实现存档/读档功能

结语

通过这个简单的农场游戏项目,你学习了C++的面向对象编程、智能指针使用和游戏循环设计。尝试按照扩展建议添加新功能,这将帮助你巩固编程技能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值