简体   繁体   English

如何在没有第三方库的情况下为雪人制作动画?

[英]How to animate snowman without third party libraries?

I have the following code that is just some cout statements that output an image of a cool snowman This is something i like to show to novice programmers to show them how cool programming can be.我有以下代码,它只是一些 cout 语句,output 一个雪人的图像 这是我想向新手程序员展示的东西,向他们展示编程有多酷。 I wanna take this a step further and show some animation, but after some research, i realized i'd need to download some external programs like turboc++ for animations in c++.我想更进一步,展示一些 animation,但经过一些研究,我意识到我需要下载一些外部程序,如 turboc++ 用于 c++ 中的动画。

Is there way to make the snow animated falling down without downloading anything?有没有办法在不下载任何东西的情况下使雪动画落下? i can run this in C++17 if necessary for some advanced headers, as long as i dont have to download anything.如果需要一些高级头文件,我可以在 C++17 中运行它,只要我不需要下载任何东西。

#include <iostream>

int main() {

  std::cout << "    *                   *        \n";
  std::cout << "*        *                       \n";
  std::cout << " *                *            * \n";
  std::cout << "      *     *                    \n";
  std::cout << "                      *          \n";
  std::cout << "*         *                 *    \n";
  std::cout << "     *        *     *            \n";
  std::cout << "           HHHHHHH        *      \n";
  std::cout << " *         HHHHHHH               \n";
  std::cout << "     *   HHHHHHHHHHH    *     *  \n";
  std::cout << "         *   @ @   *             \n";
  std::cout << "        *     ^     *            \n";
  std::cout << "  *       *       *         *    \n";
  std::cout << "         ||| * * |||             \n";
  std::cout << "         * ======== *            \n";
  std::cout << "       *      O       *          \n";
  std::cout << "      *       O        *         \n";
  std::cout << "      *       O        *         \n";
  std::cout << "       *              *          \n";
  std::cout << "           *   *   *             \n";

  return 0;

Here's a quick program I wrote that I think does what you want:这是我写的一个快速程序,我认为它可以满足您的需求:

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <thread>

using namespace std;

bool update(vector <string> &snowman){
    bool ret = false;
    int row_len = snowman[0].size();
    for(int i = 0; i < row_len; ++i){
        if(snowman.back()[i] == '*'){ret = true; snowman.back()[i] = ' ';}
    }

    for(int i = snowman.size()-2; i >= 0; --i){
        for(int j = 0; j < row_len; ++j){
            if(snowman[i][j] == '*'){
                char temp = snowman[i+1][j];
                if(temp == 'H' || temp == '0'){
                    snowman[i][j] = ' ';
                    continue;
                }

                swap(snowman[i][j], snowman[i+1][j]);
                ret = true;
            }
        }
    }

    return ret;
}

int main()
{
    vector <string> snowman = {
        "    *                   *        ",
        "*        *                       ",
        " *                *            * ",
        "      *     *                    ",
        "                      *          ",
        "*         *                 *    ",
        "     *        *     *            ",
        "           HHHHHHH        *      ",
        " *         HHHHHHH               ",
        "     *   HHHHHHHHHHH    *     *  ",
        "         0   @ @   0             ",
        "        0     ^     0            ",
        "  *       0       0         *    ",
        "         ||| 0 0 |||             ",
        "         0 ======== 0            ",
        "       0      O       0          ",
        "      0       O        0         ",
        "      0       O        0         ",
        "       0              0          ",
        "           0   0   0             ",
    };

    do{
        this_thread::sleep_for(0.5s);
        system("clear");
        for(string &item : snowman){
            cout << item << endl;
        }
        cout << endl;
    }
    while(update(snowman));

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM