C++充满特效的爱心代码
一只贴代码君 2024-10-17 12:35:02 阅读 56
当然!接下来这个版本的代码进一步增强了特效,包括动态的爱心变换和更多种类的闪烁效果:
<code>#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;
}
这个版本的代码在原有的基础上,增加了动态变化的爱心颜色和形状,以及不同类型的闪烁效果。每次循环都会更新爱心的颜色和形状,同时产生闪烁的星星效果。试着编译并运行,享受更加丰富多彩的效果吧!
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。