c语言显示心型代码

代码来源来自网上,本菜鸟只是剪接了一下

 

前缀和常量

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include <graphics.h>
#include <time.h>
#include <conio.h>

#define U 0.1
#define V 0.053//U、V用于控制心的形状
#define High 500  // 游戏画面尺寸
#define Width 1000
#define CharSize 25 // 每个字符显示的大小
复制代码

 

心型的字体颜色设置

复制代码
void SetColor(unsigned short ForeColor, unsigned short BackGroundColor)
{
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon, (ForeColor) | (BackGroundColor));
    // SetConsoleTextAttribute函数:是系统设置控制台背景色和前景色的函数 ,在头文件windows.h中  
     // Set Console TextAttribute 汉意:设置控制台的文本属性
     /*
     原型为:
     BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes)
     */
     //hCon:是一个 HANDLE 类型的变量,句柄,某种结构的唯一标识(其值是一个整型数)
     //ForeColor:前景色(字符颜色);BackGroundColor:背景色,用十六进制数表示
}
复制代码

 

一个特别装的滚动字符代码

复制代码
void display()
{
    int highNum = High / CharSize;
    int widthNum = Width / CharSize;
    // 存储对应字符矩阵中需要输出字符的ASCII码
    int CharRain[Width / CharSize][High / CharSize];
    int CNum[Width / CharSize]; // 每一列的有效字符个数
    int ColorG[Width / CharSize]; // 每一列字符的颜色
    int i, j, x, y;
    srand((unsigned)time(NULL)); // 设置随机函数种子
    for (i = 0; i < widthNum; i++) // 初始化字符矩阵
    {
        CNum[i] = (rand() % (highNum * 9 / 10)) + highNum / 10; // 这一列的有效字符个数
        ColorG[i] = 255;
        for (j = 0; j < CNum[i]; j++)
            CharRain[j][i] = (rand() % 26) + 65;  // 产生A-Z的随机ASCII码
    }
    initgraph(Width, High);
    BeginBatchDraw();
    settextstyle(25, 10, _T("Courier"));    // 设置字体
                                // 下面每一帧,让字符向下移动,然后最上面产生新的字符
    int num = 0;
    while (1)
    {

        num++;

        for (i = 0; i < widthNum; i++)
        {
            if (CNum[i] < highNum - 1)  // 当这一列字符没有填满时
            {
                for (j = CNum[i] - 1; j >= 0; j--)  // 向下移动一格
                    CharRain[j + 1][i] = CharRain[j][i];
                CharRain[0][i] = (rand() % 26) + 65;  // 最上面的产生A-Z的随机ASCII码
                CNum[i] = CNum[i] + 1; // 这一列的有效字符的个数+1
            }
            else // 这一列字符已经填满
            {
                if (ColorG[i] > 40)
                    ColorG[i] = ColorG[i] - 20; // 让满的这一列逐渐变暗
                else
                {
                    CNum[i] = (rand() % (highNum / 3)) + highNum / 10; // 这一列字符的个数
                    ColorG[i] = (rand() % 75) + 180;  // 这一列字符的颜色
                    for (j = 0; j < CNum[i]; j++)  // 重新初始化这一列字符
                        CharRain[j][i] = (rand() % 26) + 65;  // 产生A-Z的随机ASCII码
                }
            }
        }
        // 输出整个字符矩阵
        for (i = 0; i < widthNum; i++)
        {
            x = i * CharSize; // 当前字符的x坐标
            for (j = 0; j < CNum[i]; j++)
            {
                y = j * CharSize;  // 当前字符的y坐标
                setcolor(RGB(0, ColorG[i], 0));
                outtextxy(x, y, CharRain[j][i]); // 输出当前字符
            }
        }
        FlushBatchDraw();
        Sleep(100);
        clearrectangle(0, 0, Width - 1, High - 1);    // 清空画面全部矩形区域    
        if (num >= 20) {
            break;
            return;
        }
    }
   
    EndBatchDraw();
    /*_getch();*/
    closegraph();
    
}
View Code
复制代码

 

彩色心型代码

复制代码
void xin()
{   
    
    int i, s = 0, t, a = 10, b = 11, c = 12, d = 13, e = 14;
    int z[] = { -49,-78,-45,-83,-74,-2,-54,-82,-76,-13,-93,-95 }; //ASCII码
    float x, y, m;
    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("1");
                    }
                    if (s % 4 == 2)
                    {
                        SetColor(e, 0);
                        printf("0");
                    }
                    if (s % 4 == 3)
                    {
                        SetColor(c, 0);
                        printf("2");
                    }
                    if (s % 4 == 0)
                    {
                        SetColor(d, 0);
                        printf("4");
                    }
                }
                else
                {
                    for (i = 0; i < 42; i++)
                    {
                        if (i <= 14 || i >= 28)
                        {
                            s++;
                            if (s % 4 == 1)
                            {
                                SetColor(a, 0);
                                printf("1");
                            }
                            if (s % 4 == 2)
                            {
                                SetColor(e, 0);
                                printf("0");
                            }
                            if (s % 4 == 3)
                            {
                                SetColor(c, 0);
                                printf("2");
                            }
                            if (s % 4 == 0)
                            {
                                SetColor(d, 0);
                                printf("4");
                            }
                        }
                        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("1");
                        }
                        if (s % 4 == 2)
                        {
                            SetColor(b, 0);
                            printf("0");
                        }
                        if (s % 4 == 3)
                        {
                            SetColor(c, 0);
                            printf("2");
                        }
                        if (s % 4 == 0)
                        {
                            SetColor(d, 0);
                            printf("4");
                        }
                    }
                    else
                    {
                        for (i = 0; i < 42; i++)
                        {
                            if (i <= 14 || i >= 28)
                            {
                                s++;
                                if (s % 4 == 1)
                                {
                                    SetColor(a, 0);
                                    printf("1");
                                }
                                if (s % 4 == 2)
                                {
                                    SetColor(b, 0);
                                    printf("0");
                                }
                                if (s % 4 == 3)
                                {
                                    SetColor(c, 0);
                                    printf("2");
                                }
                                if (s % 4 == 0)
                                {
                                    SetColor(d, 0);
                                    printf("4");
                                }
                            }
                            else
                            {
                                SetColor(e, 0);
                                printf("%c", z[i - 15]);
                            }
                        }
                        break;
                    }
                }
                else
                    printf(" ");
            }
            printf("\n");
        }
        Sleep(1000);
    }
}
View Code
复制代码

 

运行函数

int main()
{
    
    display();
    xin();
}

 

posted @   fight挺  阅读(287)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示