用c++写一个可以跳动的爱心程序
时间: 2023-03-21 15:03:50 浏览: 138
以下是一个可以让爱心跳动的C语言程序:
```c
#include <stdio.h>
int main()
{
while (1) {
printf("\n");
printf(" ♥♥\n");
printf(" ♥ ♥\n");
printf("♥ ♥\n");
printf(" ♥ ♥\n");
printf(" ♥♥\n");
printf("\n");
// 通过控制台清屏来产生跳动的效果
system("cls");
}
return 0;
}
```
当程序运行时,爱心图案会不断地在控制台中跳动。要实现这种效果,我们可以使用`while`循环和`system("cls")`函数,每次输出爱心图案后清空控制台屏幕,这样就可以让爱心图案不停地跳动了。
相关问题
用C++写一个会跳动的爱心
以下是一个简单的用C++绘制跳动的爱心的示例代码:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main(){
const float PI = 3.1415926f;
const int WIDTH = 80;
const int HEIGHT = 40;
const float MIN_T = -2.2f;
const float MAX_T = 2.2f;
const float T_INC = 0.1f;
const float A = 2.f;
const float B = 1.6f;
const float C = 1.f;
for(float t = MIN_T; t <= MAX_T; t += T_INC){
float x = A * sin(t) * sin(t) * sin(t);
float y = B * cos(t) * cos(t) * cos(t);
int row = round((y + C) * (HEIGHT - 1) / (2 * C));
int col = round((x + A) * (WIDTH - 1) / (2 * A));
cout << "\033[" << row << ";" << col << "H❤" << flush;
usleep(10000);
cout << "\033[" << row << ";" << col << "H " << flush;
}
return 0;
}
```
该程序使用终端进行输出,每次输出一个爱心的字符,然后等待一段时间再清除该字符,以实现跳动的效果。程序使用了三角函数来计算爱心的路径,使用了ANSI控制序列来控制终端输出的位置和清除输出。
用c++写一个会跳动的爱心
好的,下面是用C语言编写的跳动爱心代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int i,j,n;
char a[13][20]={
" ** ** ",
" * * * * ",
" * ** * ",
" * * ",
" * * ",
" * * ",
" * * ",
" * * ",
" * * ",
" * ",
" * ",
" * ",
" * "
};
n=30;//跳动次数
while(n--)
{
system("cls");//清屏
for(i=0;i<13;i++)
{
for(j=0;j<20;j++)
{
if(a[i][j]=='*') printf("%c",3);//输出红色心形
else printf(" ");
}
printf("\n");
}
Sleep(200);//延时200ms
for(i=0;i<13;i++)//向上移动
{
for(j=0;j<20;j++)
{
if(a[i][j]=='*') a[i-1][j]='*';
else a[i-1][j]=' ';
}
}
a[12][0]=' ';//最上面的一行清空
}
return 0;
}
```
希望你喜欢这个爱心跳动的程序!