编写会跳动的红色c语言爱心代码
时间: 2023-09-16 22:15:26 浏览: 395
#include <stdio.h>
#include <windows.h>
int main()
{
int i, j, k;
double x, y;
const int width = 50; // 爱心图案的宽度
const int height = 40; // 爱心图案的高度
const double PI = 3.14159265358979323846;
const char* heart = "♥"; // 爱心符号
const int color = 0x0C; // 红色
// 设置控制台字体颜色
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
// 循环绘制爱心图案
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
x = (j - width / 2.0) / (width / 2.0);
y = (height / 2.0 - i) / (height / 2.0);
if (x * x + y * y - 1 <= pow(sin(PI * x), 2))
{
printf("%s", heart);
}
else
{
printf(" ");
}
}
printf("\n");
// 等待一段时间
for (k = 0; k < 10000000; k++);
}
return 0;
}