C++实现跳动的红心,看起来比较光滑的
时间: 2024-01-22 12:19:27 浏览: 31
以下是一个使用C++实现跳动红心的示例代码:
```cpp
#include <iostream>
#include <cmath>
#include <windows.h>
using namespace std;
const double PI = 3.14159265358979323846;
int main() {
int width = 80; // 窗口宽度
int height = 40; // 窗口高度
int x = width / 2; // 红心x坐标
int y = height / 2; // 红心y坐标
int r = min(x, y) - 5; // 红心半径
int t = 0; // 时间变量
while (true) {
double theta = t * PI / 180; // 角度转弧度
int dx = (int)(r * (16 * pow(sin(theta), 3))); // 根据心形函数计算x偏移量
int dy = (int)(-r * (13 * cos(theta) - 5 * cos(2 * theta) - 2 * cos(3 * theta) - cos(4 * theta))); // 根据心形函数计算y偏移量
// 清空屏幕
system("cls");
// 输出红心
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if ((j - x) * (j - x) + (i - y) * (i - y) <= r * r + 1) { // 判断点是否在红心内
if (abs((j - x) - dx) <= 2 && abs((i - y) - dy) <= 2) { // 判断点是否在红心上
cout << "*";
} else {
cout << " "; // 空白填充
}
} else {
cout << " "; // 空白填充
}
}
cout << endl;
}
// 更新时间变量
t++;
// 等待一段时间
Sleep(50);
}
return 0;
}
```
该示例代码使用了心形函数来计算红心上每个点的位置,然后输出到屏幕上。借助Windows API中的Sleep函数,可以使红心每隔一段时间更新一次位置,从而实现跳动效果。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)