“使用C语言画心形并拓展”
源码链接:公众号内回复qita0004获取
windows可执行软件链接:公众号内回复qita0005获取
Dev-Cpp编译软件链接:公众号内回复qita0006获取
1、实现效果视频链接
2、效果图:



3、源码
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <string.h>
/*
对应的颜色码表:
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 10 = 淡绿色
3 = 浅绿色 11 = 淡浅绿色
4 = 红色 12 = 淡红色
5 = 紫色 13 = 淡紫色
6 = 黄色 14 = 淡黄色
7 = 白色 15 = 亮白色
*/
using namespace std;
void goto_xy(int x,int y);//跳转光标所在行
void HideCursor();//隐藏光标
int main() {
/********************************************设置字体***********************************/
//现在仅支持纯英文或者纯中文字符
//cText为纯英文时 nSkipNum=1
//cText为纯中文时 nSkipNum=2
int nSkipNum = 2;//跳转字符数,中文为两个字节
char cText[31] = "节日快乐!";//超过15个中文字符(包括中文标点),将31改大
/****************************************END*********************************************/
/************************************设置背景色和字体颜色********************************/
//此处设置背景色和字体是闪动还是不闪动
//添加背景字: isText = true
//添加背景字: isText = false
//闪动时 isSimple = false
//不闪时 isSimple = true,并设置背景和字体颜色,具体颜色参照最上方的颜色对照
bool isText = true;
bool isSimple = true;
int bkgColorSimple = 14;
int textColorSimple = 12;
/****************************************END*********************************************/
char bkgTextColor[16];
int nSkip = 0;
while(1)
{
for(int i=0;i<16;i++)//循环背景跳色
{
goto_xy(0,0);//每次开始再cmd的0,0位置
HideCursor();//隐藏光标
if(!isSimple)sprintf(bkgTextColor,"color %x%x",i,16-i);//将颜色传给变量
else sprintf(bkgTextColor,"color %x%x",bkgColorSimple,textColorSimple);
system(bkgTextColor);//执行指令
for (float y = 1.5; y > -1.5; y -= 0.1)//画心并添加背景文字
{
for (float x = -1.5; x < 1.5; x += 0.04)
{
float z = x * x + y * y - 1;
if(z * z * z - x * x * y * y * y <= 0.0)
{
printf("*");
}
else
{
nSkip++;
if(isText)
{
if(nSkip%nSkipNum==nSkipNum-1)//添加背景字
{
int i = nSkip - 1;
if(nSkipNum == 2)cout<<cText[i]<<cText[i+1];
else cout<<cText[i];
if(cText[i+nSkipNum]=='\0')nSkip = -1;
}
else
{
printf("");
}
}
else printf(" ");
}
}
putchar('\n');
}
}
}
}
void goto_xy(int x,int y)
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {x,y};
SetConsoleCursorPosition(hOut,pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}