请问,爱心代码如何做到两次跳动变一次色?

目前的想法是想着播放一次红色的然后在播放一次粉色的,但是没有想好怎么进行红粉的交替出现。

该回答引用ChatGpt
代码如下 ,还请参考,如果可行 还请点击 采纳,感谢!

#include <iostream>
#include <string>

int main() {
    std::string colors[] = {"red", "pink"};
    int count = 0;
    while (count < 10) {
        std::cout << colors[count % 2] << std::endl;
        count++;
    }
    return 0;
}


代码实现了红色和粉色交替出现 10 次。因为数组长度为 2,所以使用 count % 2 来求余,从而访问数组中的不同元素。

红色和粉色的交替可以使用sleep(暂停指定秒数),然后使用清屏命令清除原来的图案,然后再次画出粉色图案来实现。

通过参考搜索网络资料,做了一个简单的实现,程序先显示红色爱心图案一秒,然后暂停一秒,(如果需要显示两次红色爱心,可以调节i的值来实现),接着显示接近粉色的爱心图案(实际颜色应该是浅紫色,因为不知道怎么调出粉色来,所以用浅紫色代替的),代码如下:

参考链接:


https://zhidao.baidu.com/question/1763838583097847788.html

https://www.jb51.net/article/264316.htm

https://www.zhihu.com/question/487797828


#include<stdio.h>
#include<Windows.h>
#include <unistd.h>

int main() {
    
    float x, y, a;
    int i=0;
    while(i<2){
        // https://blog.csdn.net/weixin_64559564/article/details/123938625
        for (y = 1.5; y > -1.5; y -= 0.1)
        {
            for (x = -1.5; x < 1.5; x += 0.05)
            {
                a = x * x + y * y - 1;
                putchar(a * a * a - x * x * y * y * y <= 0.0 ? '*' : ' ');
            }
            if(i==0){
                system("color 0c");  //红色 
            }else if(i==1){
                        
        
                // https://zhidao.baidu.com/question/1763838583097847788.html
                // https://www.jb51.net/article/264316.htm
                system("color 0d");      // 浅紫色,和粉色接近 
            }
            putchar('\n');
        
        }
            
        // https://www.zhihu.com/question/487797828
        sleep(1); // 打印出爱心图案后,程序暂停显示画面一秒 
        
        if(i==0){ //如果是第一次出现画面,则清除打印的图案,以便开始打印下一个图案 
            // https://www.fke6.com/html/23492.html
            system("cls");
        }
        
        
        i++;
    } 
    
    
    
    return 0;
}


img