欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c语言心形告白代码实现

程序员文章站 2022-06-01 19:34:29
心形告白代码实现 1.彩色告白 #include #include #include...

心形告白代码实现

1.彩色告白  
#include<stdio.h>  
#include<math.h>  
#include<windows.h>  
#include<time.h>  
#define u 0.1  
#define v 0.053  
void setcolor(unsigned short forecolor,unsigned short backgroundcolor)  
{  
    handle hcon=getstdhandle(std_output_handle);  
    setconsoletextattribute(hcon,(forecolor%16)|(backgroundcolor%16*16));  
}  
int main()  
{  
    int i,s=0,t,a=10,b=11,c=12,d=13,e=14;  
    int z[] = {32,32,206,210,207,178,187,182,196,227,33,32,32};  
    float x,y;  
    srand(time(null));  
    for(y=1.3;y>=-1.1;y-=u)  
    {  
        for(x=-2;x<1.4;x+=v)  
        {  
            if((((x*x+y*y-1)*(x*x+y*y-1)*(x*x+y*y-1)-x*x*y*y*y)<=0))  
            {  
                if(y>=1.3-10*u||y<=1.3-11*u)  
                {  
                s++;  
                if(s%4==1){setcolor(a,0);printf("l");}  
                if(s%4==2){setcolor(e,0);printf("o");}  
                if(s%4==3){setcolor(c,0);printf("v");}  
                if(s%4==0){setcolor(d,0);printf("e");}  
                }  
                else  
                {     
                    for(i = 0;i < 42;i++)  
                    {     
                        if(i<=14||i>=28)  
                        {  
                            s++;  
                            if(s%4==1){setcolor(a,0);printf("l");}  
                            if(s%4==2){setcolor(e,0);printf("o");}  
                            if(s%4==3){setcolor(c,0);printf("v");}  
                            if(s%4==0){setcolor(d,0);printf("e");}  
                        }  
                        else  
                        {  
                            setcolor(b,0);  
                            printf("%c", z[i-15]);  
                            sleep(50);  
                        }  
                    }  
                    break;  
                }  
            }  
                else  
                    printf(" ");  
                    sleep(1);  
        }  
    printf("\n");  
    }  
    printf("按任意键继续!");  
    getchar();    
    while(1)  
    {  
                system("cls");  
            t=a;a=b;b=c;c=d;d=e;e=t;  
            for(y=1.3;y>=-1.1;y-=u)  
        {  
            for(x=-2;x<1.4;x+=v)  
            {  
                if((((x*x+y*y-1)*(x*x+y*y-1)*(x*x+y*y-1)-x*x*y*y*y)<=0))  
                {  
                    if(y>=1.3-10*u||y<=1.3-11*u)  
                    {  
                    s++;  
                    if(s%4==1){setcolor(a,0);printf("l");}  
                    if(s%4==2){setcolor(b,0);printf("o");}  
                    if(s%4==3){setcolor(c,0);printf("v");}  
                    if(s%4==0){setcolor(d,0);printf("e");}  
                    }  
                    else  
                    {     
                        for(i = 0;i < 42;i++)  
                        {     
                            if(i<=14||i>=28)  
                            {  
                                s++;  
                                if(s%4==1){setcolor(a,0);printf("l");}  
                                if(s%4==2){setcolor(b,0);printf("o");}  
                                if(s%4==3){setcolor(c,0);printf("v");}  
                                if(s%4==0){setcolor(d,0);printf("e");}  
                            }  
                            else  
                            {  
                                setcolor(e,0);  
                                printf("%c", z[i-15]);  
                            }  
                        }  
                        break;  
                    }  
                }  
                    else  
                        printf(" ");  
            }  
                printf("\n");  
        }  
            sleep(1000);  
            system("cls");  
              
    }  
}  
  
2.简单心形  
#include <math.h>   
#include <stdio.h>  
int main()  
{  
float y, x, a;  
for (y = 1.5f;y > -1.5f;y -= 0.1f)  
{  
for (x = -1.5f;x < 1.5f;x += 0.05f)  
{  
a = x*x + y*y - 1;  
putchar(a*a*a - x*x*y*y*y <= 0.0f ? '*' : ' ');  
}  
putchar('\n');  
}  
return 0;  
}  
  
3.花纹心形  
#include <math.h>  
#include <stdio.h>  
int main()  
{  
float y, x, z,f;  
for (y = 1.5f;y > -1.5f;y -= 0.1f)  
{  
for (x = -1.5f;x < 1.5f;x += 0.05f)  
{  
z = x*x + y*y - 1;  
f = z*z*z - x*x*y*y*y;  
putchar(f <= 0.0f ? ".:-=+*#%@"[(int)(f*-8.0f)] : ' ');  
}  
putchar('\n');  
}  
  
  
getchar();  
return 0;  
}  
  
4.跳动心形  
#include <stdio.h>  
#include <math.h>  
#include <windows.h>  
#include <tchar.h>  
  
float f(float x, float y, float z) {  
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;  
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;  
}  
  
float h(float x, float z) {  
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)  
if (f(x, y, z) <= 0.0f)  
return y;  
return 0.0f;  
}  
  
int main() {  
handle o = getstdhandle(std_output_handle);  
_tchar buffer[25][80] = { _t(' ') };  
_tchar ramp[] = _t(".:-=+*#%@");  
  
for (float t = 0.0f;; t += 0.1f) {  
int sy = 0;  
float s = sinf(t);  
float a = s * s * s * s * 0.2f;  
for (float z = 1.3f; z > -1.2f; z -= 0.1f) {  
_tchar* p = &buffer[sy++][0];  
float tz = z * (1.2f - a);  
for (float x = -1.5f; x < 1.5f; x += 0.05f) {  
float tx = x * (1.2f + a);  
float v = f(tx, 0.0f, tz);  
if (v <= 0.0f) {  
float y0 = h(tx, tz);  
float ny = 0.01f;  
float nx = h(tx + ny, tz) - y0;  
float nz = h(tx, tz + ny) - y0;  
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);  
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;  
*p++ = ramp[(int)(d * 5.0f)];  
}  
else  
*p++ = ' ';  
}  
}  
  
for (sy = 0; sy < 25; sy++) {  
coord coord = { 0, sy };  
setconsolecursorposition(o, coord);  
writeconsole(o, buffer[sy], 79, null, 0);  
}  
sleep(33);  
}  
}  
5.心形小程序   
#include<stdio.h>  
#include<math.h>  
#include<windows.h>  
#include<time.h>  
#include<stdlib.h>  
void setcolor(unsigned short forecolor,unsigned short backgroundcolor)  
{  
    handle hcon=getstdhandle(std_output_handle);  
    setconsoletextattribute(hcon,(forecolor%16)|(backgroundcolor%16*16));  
}  
int main()  
{  
  
    float x,y;  
    int s=0,n,i,j;  
        for(i = 1;i <= 50;i++)  
    {  
        setcolor(0,14);  
        printf(" ");  
        printf("%d%%",2*i);  
        sleep(101-2*i);  
        printf("\b\b\b");  
    }  
    setcolor(15,0);  
    printf("\b\n加载完成!即将进入:");  
    sleep(2000);  
    system("cls");  
    do  
    {  
        system("color 0e");  
        for(i=0;i<=56;i++)  
    {  
        if(i>=20){printf("*");sleep(10);}  
        else printf(" ");  
    }printf("\n");  
    for(i=0;i<=56;i++)  
    {  
        if(i>=20){printf("*");sleep(10);}  
        else printf(" ");  
    }  
    printf("\n\t\t\t你想要怎样的心形:\n");  
    printf("\n\t\t\t1:由love组成的心形!\n");  
    printf("\n\t\t\t2:由随机字符组成的心形!\n");  
    printf("\n\t\t\t3:由随机数字组成的心形!\n");  
    printf("\n\t\t\t4:由随机颜色组成的心形!\n");  
    printf("\n\t\t\t5:退出!\n");  
    for(i=0;i<=56;i++)  
    {  
        if(i>=20){printf("*");sleep(10);}  
        else printf(" ");  
    }printf("\n");  
    for(i=0;i<=56;i++)  
    {  
        if(i>=20){printf("*");sleep(10);}  
        else printf(" ");  
    }  
    printf("\n\t\t\t");  
  
    srand(time(null));  
    scanf("%d",&n);  
    system("cls");  
  
  
  
        switch(n)  
            {  
        case 1:  
    for(x=1.3;x>=-1.1;x-=0.1)  
    {   for(y=-2;y<=1.4;y+=0.053)  
        {  
            if(pow(x*x+y*y-1,3)-x*x*x*y*y<=0)  
            {  
                s++;  
                if(s%4==1)printf("l");  
                if(s%4==2)printf("o");  
                if(s%4==3)printf("v");  
                if(s%4==0)printf("e");  
                sleep(2);  
            }  
            else  
                printf(" ");  
        }  
    printf("\n");  
    }  
    printf("绘制完成,按任意键返回主页面!");break;  
  
  
  
  
    case 2: for(x=1.3;x>=-1.1;x-=0.1)  
    {   for(y=-2;y<=1.4;y+=0.053)  
        {  
            if(pow(x*x+y*y-1,3)-x*x*x*y*y<=0)  
            {  
                printf("%c",rand()%10+rand()%10+97);  
                sleep(2);  
            }  
            else  
                printf(" ");  
        }  
    printf("\n");  
    }  
    printf("绘制完成,按任意键返回主页面!");break;  
          
      
      
      
      
      
      
    case 3:for(x=1.3;x>=-1.1;x-=0.1)  
    {   for(y=-2;y<=1.4;y+=0.053)  
        {  
            if(pow(x*x+y*y-1,3)-x*x*x*y*y<=0)  
            {  
                printf("%d",rand()%10);  
                sleep(2);  
            }  
            else  
                printf(" ");  
        }  
    printf("\n");  
    }  
    printf("绘制完成,按任意键返回主页面!");break;  
          
          
          
          
          
        case 4: for(x=1.3;x>=-1.1;x-=0.1)  
    {   for(y=-2;y<=1.4;y+=0.053)  
        {  
            if(pow(x*x+y*y-1,3)-x*x*x*y*y<=0)  
            {  
                setcolor(0,rand()%6+10);  
                printf(" ");  
                sleep(2);  
            }  
            else  
            {setcolor(0,0);  
            printf(" ");}  
        }  
    printf("\n");  
    }  
    setcolor(15,0);  
    printf("绘制完成,按任意键返回主页面!");break;  
        default:break;  
        }     
        getch();  
        system("cls");  
    }while(n!=5);  
  
}