You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需第三方库与外部工具:C++17实现雪人雪花飘落动画

Yes, you can add snowfall animation using standard C++17!

You don't need Turbo C++ or any external tools—we can pull this off with standard C++ libraries and ANSI escape codes (supported by most modern terminals: Linux, macOS, Windows 10+). Here's how it works:

Key Concepts Used:

  • ANSI Escape Codes: To clear the screen and reset the cursor position between frames (so we can redraw the animation smoothly).
  • <chrono> & <thread>: To control the animation frame rate (so it doesn't run too fast).
  • <random>: To generate random positions for snowflakes each frame.
  • String Storage: Storing the snowman as a multi-line string to easily redraw it each frame.

Complete Code Example:

#include <iostream>
#include <chrono>
#include <thread>
#include <random>
#include <vector>
#include <string>

int main() {
    // Store the snowman as a vector of lines for easy access
    const std::vector<std::string> snowman = {
        " * * ",
        "* * ",
        " * * * ",
        " * * ",
        " * ",
        "* * * ",
        " * * * ",
        " HHHHHHH * ",
        " * HHHHHHH ",
        " * HHHHHHHHHHH * * ",
        " * @ @ * ",
        " * ^ * ",
        " * * * * ",
        " ||| * * ||| ",
        " * ======== * ",
        " * O * ",
        " * O * ",
        " * O * ",
        " * * ",
        " * * * "
    };

    // Set up random number generator for snowflake positions
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> row_dist(0, snowman.size() - 1);
    const int max_col = 20; // Adjust based on your widest snowman line
    std::uniform_int_distribution<int> col_dist(0, max_col - 1);

    // Animation loop (run until Ctrl+C is pressed)
    while (true) {
        // Clear screen and move cursor to top-left corner (ANSI escape code)
        std::cout << "\033[2J\033[H";

        // Generate 15 random snowflake positions for this frame
        std::vector<std::pair<int, int>> snowflakes;
        for (int i = 0; i < 15; ++i) {
            snowflakes.emplace_back(row_dist(gen), col_dist(gen));
        }

        // Draw each line of the snowman, overlaying snowflakes
        for (int row = 0; row < snowman.size(); ++row) {
            std::string line = snowman[row];
            // Check if any snowflake is on this row
            for (const auto& sf : snowflakes) {
                if (sf.first == row && sf.second < line.size()) {
                    line[sf.second] = '*';
                }
            }
            std::cout << line << "\n";
        }

        // Wait ~33ms to get ~30 frames per second
        std::this_thread::sleep_for(std::chrono::milliseconds(33));
    }

    return 0;
}

How It Works:

  1. Snowman Storage: The snowman is stored as a vector<string> so we can easily modify each line to add snowflakes.
  2. Random Snowflakes: Each frame generates 15 random (row, column) pairs where snowflakes appear.
  3. Screen Clearing: The "\033[2J\033[H" code clears the entire screen and moves the cursor to the top-left, so each frame starts fresh.
  4. Frame Timing: sleep_for ensures the animation runs at a smooth pace—adjust the milliseconds if it's too fast/slow.

Compatibility Notes:

  • Windows: For Windows 10 or later, ANSI escape codes are enabled by default. If you're on an older Windows version, you may need to enable VT100 support via SetConsoleMode (a Windows-specific API, but still part of the OS—no external tools required).
  • Linux/macOS: Works out of the box in any modern terminal (like Terminal.app, GNOME Terminal, etc.).

To run the code, just compile it with a C++17-compliant compiler (like GCC, Clang, or MSVC) using:

g++ -std=c++17 snowman.cpp -o snowman
./snowman

Press Ctrl+C to stop the animation.

内容的提问来源于stack exchange,提问作者Cataster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:37:43