爱心代码c++语言合集
时间: 2025-05-29 08:52:11 浏览: 19
### C++ 爱心形状代码集合
以下是几个经典的 C++ 编写爱心形状的代码示例:
#### 示例一:静态 ASCII 艺术爱心
这是一个简单的静态爱心图案,利用数学函数绘制。
```cpp
#include <iostream>
#include <cmath>
int main() {
double x, y;
int width = 20;
for (double y = 2.0; y >= -2.0; y -= 0.1) {
for (double x = -2.5; x <= 2.5; x += 0.05) {
double temp = pow(x * x + y * y - 1, 3);
double heart = pow(x, 2) + pow(y, 2) * 1.5 - pow(pow(x, 2), 1.5);
if (temp <= heart || abs(x) <= 0.05 && y < 0) {
std::cout << "*";
} else {
std::cout << " ";
}
}
std::cout << "\n";
}
return 0;
}
```
该代码基于笛卡尔坐标系下的心脏曲线方程[^1]。
---
#### 示例二:动态爱心动画
这个例子展示如何创建一个动态变化的爱心图形。通过调整参数 `animationSpeed` 和屏幕尺寸,可以改变动画的效果。
```cpp
#include <iostream>
#include <conio.h> // For _sleep()
using namespace std;
void drawHeart(int screenWidth, int screenHeight, float animationSpeed) {
const char heartSymbol = '♥';
const int maxFrames = 10;
for (int frame = 0; frame < maxFrames; ++frame) {
system("cls"); // Clear console screen
for (float y = screenHeight / 2; y >= -(screenHeight / 2); --y) {
for (float x = -(screenWidth / 2); x <= screenWidth / 2; ++x) {
double distanceFromCenterX = ((x * 0.07) - pow((y * 0.05), 2));
double distanceFromCenterY = (y * 0.05);
double equationResult = pow(distanceFromCenterX, 2) + pow(abs(distanceFromCenterY), 2) - pow(distanceFromCenterX, 2) * pow(abs(distanceFromCenterY), 3);
if (equationResult <= 0.6 + sin(frame * M_PI / 4)) { // Add dynamic effect with sine wave
cout << heartSymbol;
} else {
cout << " ";
}
}
cout << endl;
}
_sleep(animationSpeed); // Pause between frames to create animation effect
}
}
int main() {
int screenWidth = 80;
int screenHeight = 25;
float animationSpeed = 100; // Milliseconds per frame
drawHeart(screenWidth, screenHeight, animationSpeed);
return 0;
}
```
此代码实现了动态效果,并依赖于 Windows 的 `_sleep()` 函数以及控制台清屏命令 `system("cls")` 来完成动画渲染[^3]。
---
#### 示例三:带颜色的爱心(仅限 Windows)
如果希望在 Windows 平台上显示彩色爱心,则可以通过设置控制台的颜色属性来增强视觉体验。
```cpp
#include <windows.h>
#include <iostream>
#include <cmath>
using namespace std;
void setConsoleColor(WORD colorAttr) {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, colorAttr);
}
int main() {
double x, y;
int width = 20;
for (double y = 2.0; y >= -2.0; y -= 0.1) {
for (double x = -2.5; x <= 2.5; x += 0.05) {
double temp = pow(x * x + y * y - 1, 3);
double heart = pow(x, 2) + pow(y, 2) * 1.5 - pow(pow(x, 2), 1.5);
if (temp <= heart || abs(x) <= 0.05 && y < 0) {
setConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); // Red color
cout << "❤"; // Unicode heart symbol
setConsoleColor(FOREGROUND_WHITE); // Reset to default white text
} else {
cout << " ";
}
}
cout << "\n";
}
return 0;
}
```
这里使用了 Windows API 中的 `SetConsoleTextAttribute` 方法更改字体颜色为红色[^2]。
---
#### 示例四:满屏爱心飘动效果
这种类型的程序会随机生成多个小型爱心并让它们在整个屏幕上移动。
```cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
struct Heart {
int x, y;
};
void moveHearts(vector<Heart>& hearts, int screenWidth, int screenHeight) {
for (auto& heart : hearts) {
heart.y++;
if (heart.y > screenHeight) {
heart.x = rand() % screenWidth;
heart.y = 0;
}
}
}
void renderScreen(const vector<Heart>& hearts, int screenWidth, int screenHeight) {
system("cls");
for (int row = 0; row < screenHeight; ++row) {
for (int col = 0; col < screenWidth; ++col) {
bool isHeartHere = false;
for (const auto& heart : hearts) {
if (heart.x == col && heart.y == row) {
cout << "❤";
isHeartHere = true;
break;
}
}
if (!isHeartHere) {
cout << " ";
}
}
cout << endl;
}
}
int main() {
srand(time(nullptr));
int screenWidth = 80;
int screenHeight = 25;
vector<Heart> hearts(100);
for (auto& heart : hearts) {
heart.x = rand() % screenWidth;
heart.y = rand() % screenHeight;
}
while (true) {
moveHearts(hearts, screenWidth, screenHeight);
renderScreen(hearts, screenWidth, screenHeight);
Sleep(100); // Adjust speed of movement
}
return 0;
}
```
这段代码模拟了许多小爱心在屏幕上不断下落的过程。
---
阅读全文
相关推荐


















