温馨提示×

c++爱心跳动代码怎么写

c++
小亿
121
2024-06-03 18:17:14
栏目: 编程语言

下面是一个简单的C++代码,可以实现爱心跳动效果:

#include <iostream>
#include <cmath>

int main() {
    const int rows = 10;
    const int cols = 30;

    char heart[rows][cols];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            heart[i][j] = ' ';
        }
    }

    double x, y, f;

    for (int i = 0; i < 360; i++) {
        f = i * M_PI / 180;
        x = 16 * pow(sin(f), 3);
        y = 13 * cos(f) - 5 * cos(2 * f) - 2 * cos(3 * f) - cos(4 * f);

        int row = static_cast<int>(y) + 5;
        int col = static_cast<int>(x) + 15;

        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            heart[row][col] = '*';
        }

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                std::cout << heart[i][j];
            }
            std::cout << std::endl;
        }
    }

    return 0;
}

运行这段代码可以看到一个爱心在控制台中跳动的效果。您可以根据自己的需要调整爱心的大小和跳动速度。

1