狂宠粉博主 2024-10-03 14:59 采纳率: 100%
浏览 6
已结题

给我一个用c++代码写的几样东西

给我一个用c++代码写的几样东西:
1.火柴人打架动画
2.玫瑰花

  • 写回答

1条回答 默认 最新

  • 檀越@新空间 2024-10-03 15:00
    关注

    下午好🌅🌅🌅
    本答案参考ChatGPT-3.5

    这个任务需要使用到一些编程知识,下面是我为你准备的两个例子:

    火柴人打架动画

    #include <iostream>
    #include <vector>
    
    // 定义火柴人的基本结构
    struct Character {
        int x, y;
    };
    
    // 动画对象
    class Animation {
    public:
        void move(Character* p) {
            // 每次移动时,我们需要检查当前的x和y坐标是否超过了屏幕范围
            if (p->x > screen_width || p->y > screen_height) {
                std::cout << "X and Y coordinates out of the screen!" << std::endl;
                return;
            }
    
            // 向指定的方向移动
            p->x += dx;
            p->y += dy;
    
            // 如果需要的话,可以向左或向右移动
            if (dx > 0)
                dx -= 1;
            else if (dx < 0)
                dx += 1;
    
            if (dy > 0)
                dy -= 1;
            else if (dy < 0)
                dy += 1;
        }
    };
    
    int main() {
        // 创建两个火柴人
        Character* left = new Character{0, 0};
        Character* right = new Character{0, screen_height - 1};
    
        // 创建动画对象,并开始播放动画
        Animation* animation = new Animation();
        animation->move(left);
    
        while (!animation->isFinished()) {
            // 玫瑰花移动
            delete animation->currentCharacter;
            animation->currentCharacter = right;
            animation->move(right);
        }
    
        return 0;
    }
    

    玫瑰花

    #include <iostream>
    #include <vector>
    
    // 定义花的基本结构
    struct Plant {
        int id;
        int color;
    };
    
    // 花朵类的子类,用于表示单朵玫瑰花
    class Rose : public Plant {
    public:
        Rose(int id, int color) : id(id), color(color) {}
    
        void speak() {
            std::cout << "I am a beautiful rose!" << std::endl;
        }
    };
    
    int main() {
        // 创建一个包含多个花朵的列表
        std::vector<Plant> plants = {
            {1, "red"},
            {2, "yellow"},
            {3, "white"}
        };
    
        // 创建一个新的玫瑰花节点
        Rose rose(1, "red");
    
        // 遍历植物列表,并添加新的玫瑰花节点
        for (const auto& plant : plants) {
            rose.speak();
        }
    
        return 0;
    }
    

    请注意,这些只是简单的示例,实际的程序可能需要更复杂的功能,例如图形用户界面、控制台输出等。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 已采纳回答 10月3日
  • 创建了问题 10月3日