C++充满特效的爱心代码

45 篇文章 1 订阅
8 篇文章 0 订阅

当然!接下来这个版本的代码进一步增强了特效,包括动态的爱心变换和更多种类的闪烁效果:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <unistd.h>

using namespace std;

const int width = 80;
const int height = 25;

void clearScreen() {
    cout << "\033[2J\033[1;1H"; // ANSI escape code to clear screen
}

void delay(int milliseconds) {
    usleep(milliseconds * 1000); // usleep takes microseconds
}

void printDynamicHeart(int frame) {
    const char colors[] = {'\033[91m', '\033[93m', '\033[92m', '\033[94m', '\033[95m', '\033[96m'};
    const char heartSymbol = 3;
    const int scale = 5;

    double angle = frame * 0.1;
    for (int y = height / 2; y >= -height / 2; y--) {
        for (int x = -width / 2; x <= width / 2; x++) {
            double a = pow(x * scale * 0.04, 2) + pow(y * 0.1, 2) - 1;
            if (a <= 0) {
                int colorIndex = (int)((angle + x + y) / 10) % 6;
                cout << colors[colorIndex] << heartSymbol;
            } else {
                cout << ' ';
            }
        }
        cout << endl;
    }
    cout << "\033[0m"; // Reset color
}

void printSparkles(int numSparkles) {
    const char sparkleSymbols[] = {'*', '.', '+', 'o'};
    srand(time(0));

    for (int i = 0; i < numSparkles; i++) {
        int x = rand() % width - width / 2;
        int y = rand() % height - height / 2;
        int sparkleIndex = rand() % 4;
        cout << "\033[" << y << ";" << x << "H" << sparkleSymbols[sparkleIndex];
    }
}

int main() {
    int numSparkles = 500;
    int duration = 100;

    clearScreen();
    int frame = 0;
    while (true) {
        printSparkles(numSparkles);
        delay(duration);
        clearScreen();
        printDynamicHeart(frame);
        frame++;
        clearScreen();
        delay(duration);
    }

    return 0;
}

这个版本的代码在原有的基础上,增加了动态变化的爱心颜色和形状,以及不同类型的闪烁效果。每次循环都会更新爱心的颜色和形状,同时产生闪烁的星星效果。试着编译并运行,享受更加丰富多彩的效果吧!

  • 17
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只贴代码君(yaosicheng)

帅帅的你,留下你的支持吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值