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);
}