2018-05-20

520表白代码

今天是520了,笔者翻出了大一的时候写的代码,刚接触程序,一开始学的C,
只是一个简单的图形。(你连女朋友都没有画什么小心心╭(╯^╰)╮)

笔者自己写的部分(low的要死)

# include <stdio.h>
# include <math.h>


# define U 0.06
# define V 0.025
# define M 1.1
# define N 1.2

int main(void)
{
float x, y;
float m, n;
printf("小心心送给你\n");

for ( y=2; y>=-2; y-=U )
{
for ( x=-1.2; x<=1.2; x+=V)
{
if ( ( ( (x*x + y*y - 1)*(x*x + y*y - 1)*(x*x + y*y - 1) - x*x*y*y*y ) <= 0 ) )
printf("*");
else
printf(" ");
}
printf("\n");
}
getchar();


return 0;
}

辣鸡,连缩进都没有!

效果图如下:

小心心_one

当然,也可以改变颜色,用 system("color 0A");
其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:

2=绿色
(只有这一种颜色你们看着用吧╭(╯^╰)╮)

system("color F4");

上述语句是把背景改成白色,心改成红色,效果如下

小心心_two

或者直接右击控制台来改变背景色前景色也是可以的


大佬写的部分

效果图如下:

心动

首先需要自己装上这个库:

1.GetStdHandle(STD_OUTPUT_HANDLE);//获取控制台输出句柄
2.SetConsoleScreenBufferSize(o, size); // 重新设置缓冲区大小
3.SMALL_RECT rc = {0, 0, 100-1, 40-1}; // 重置窗口位置和大小
  SetConsoleWindowInfo(o, true, &rc);
4.SetConsoleTitle("LOVE");//设置控制台标题
5.system("color ec"); // 设置为红色,显得浪漫温馨点
6.SetConsoleTextAttribute(o, hjzgg_color[color_index]);//设置下个即将输出字体的前景色和背景色
7.SetConsoleCursorPosition(o, coord);//设置控制台光标的位置
8.WriteConsole(o, one_word, 2, NULL, 0);//在控制台光标的位置处连续输出一定长度的字符串
9.pthread_mutex_lock(&mutex);//给线程上锁,利用互斥变量
10.pthread_mutex_unlock(&mutex);给线程解锁
11.pthread_create(&tid[1], NULL, first_init_down, NULL); //创建线程
12.pthread_join(tid[0], NULL);//用来等待一个线程的结束
13.pthread_cond_timedwait(&cond, &mutex, &outtime);//线程进入等待,一定时间后自行苏醒

代码部分如下

#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <tchar.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>

pthread_mutex_t mutex;//互斥信号量
pthread_cond_t cond;//条件变量
HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
_TCHAR buffer[25][80] = { _T(' ') };
_TCHAR ramp[] = _T(".:-=+*#%@");

WORD hjzgg_color[7]={
    BACKGROUND_INTENSITY|FOREGROUND_INTENSITY|FOREGROUND_RED | BACKGROUND_RED|BACKGROUND_GREEN, //red on yellow

    BACKGROUND_INTENSITY|FOREGROUND_INTENSITY|FOREGROUND_GREEN | BACKGROUND_RED|BACKGROUND_GREEN,
    BACKGROUND_INTENSITY|FOREGROUND_INTENSITY|FOREGROUND_BLUE | BACKGROUND_RED|BACKGROUND_GREEN,

    BACKGROUND_INTENSITY|FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE | BACKGROUND_RED|BACKGROUND_GREEN,

    BACKGROUND_INTENSITY|FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN | BACKGROUND_RED|BACKGROUND_GREEN,
    BACKGROUND_INTENSITY|FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE | BACKGROUND_RED|BACKGROUND_GREEN,
    BACKGROUND_INTENSITY|FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE | BACKGROUND_RED|BACKGROUND_GREEN

};

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

char flower1[11][22]={//一行20个字符
"         {@}        ",
"      {@} * {@}     ",
"   {@} * {@} * {@}  ",
"{@}* {@} * {@} * {@}",
" \\ {@} * {@} * {@} /",
"    \\ \\ \\ L / / /   ",
"     \\\\ \\ O / //    ",
"       \\\\ V //      ",
"        \\\\E//       ",
"         >=<        ",
"        //*\\\\       "
};

char flower2[9][28]={//一行27个字符
".....@---------------@.....",
"-- @ @ @ --------- @ @ @ --",
".@ @\\@/@ @-------@ @\\@/@ @.",
".\\@|@|@|@/-....--\\@|@|@|@/.",
". \\\\\\|/// --..--- \\\\\\|/// -",
" - \\\\|// --..----- \\\\|//.--",
" -- \\|/--..---.---- \\|/---.",
" -- =&= ---..------ =&= ---",
" -- /|\\ --------.---/|\\ -.-"
} ;

char piercing_arrow[][34]={//一行33个字符
"      ☆☆ ☆☆  ☆☆ ☆☆       ",
"     ★★      ★       ★★     ",
"    ☆☆   快乐每一天    ☆☆    ",
"     ★★               ★★     ",
">>>---------I LOVE YOU --------->",
"        ★★         ★★        ",
"          ☆☆     ☆☆          ",
"            ★★  ★★           ",
"               ☆☆              "
};

char love_word[][6][60]={//一共5个部分, 三行情书
    {
        "你  如  水  的  眸  光  笼  着  江  南  的  烟  雨",
        "映  入  我  那  一  寸  一  寸  消  融  的  心",
        "从  那  一  刻  我  不  在  属  于  自  己"
    },
    {
        "我  默  默  收  集  你  的  所  有",
        "不  知  还  要  多  久",
        "却  不  曾  厌  倦"
    },

    {
        "当  初 ,初  见  只  道  是  寻  常",
        "寻  觅 ,何  枝  可  化  深  千  亿",
        "犹  道 ,碧  桃  影  里  碧  三  声"
    } ,
    {
        "在  这  个  年  纪  远  远  地  看  着  你",
        "日  光  美  好  的  一  泻  千  里",
        "温  暖  着  一  个  卑  微  而  简  单  的  心"
    } ,
    {
        "你  的  一  纸  信  笺",
        "压  在  书  底",
        "压  过  了  青  春  的  夏  季",
        "从  爱  你  的  花  季",
        "一  直  等  到",
        "爱  你  的  暮  年"
    }
} ;




void init_screen(){
    COORD size = {100, 40};
    SetConsoleScreenBufferSize(o, size); // 重新设置缓冲区大小
    SMALL_RECT rc = {0, 0, 100-1, 40-1}; // 重置窗口位置和大小
    SetConsoleWindowInfo(o, true, &rc);
    SetConsoleTitle("LOVE");
    system("color ec"); // 设置为红色,显得浪漫温馨点
}

void* show_love_word(void* arg){
    srand((unsigned)time(NULL));//随机每束花是否显示
    while(true) {
        int word_index = rand()%5;
        int col = 70;
        int row_index = 3;
        if(word_index == 4)
            row_index = 6;
        //显示word
        int color_index = rand()%4;
        for(int i=0; i<row_index; ++i){
            int row = 1;
            for(int j=0; love_word[word_index][i][j]; j+=2){
                char one_word[2];//得到一个汉字
                sprintf(one_word, "%c%c", love_word[word_index][i][j], love_word[word_index][i][j+1]);
                COORD coord = {col, row};
                pthread_mutex_lock(&mutex);
                SetConsoleTextAttribute(o, hjzgg_color[color_index]);
                SetConsoleCursorPosition(o, coord);
                WriteConsole(o, one_word, 2, NULL, 0);
                coord.Y += 1;
                if(love_word[word_index][i][j+2]){
                    SetConsoleCursorPosition(o, coord);
                    WriteConsole(o, "__", 2, NULL, 0);
                }
                SetConsoleTextAttribute(o, hjzgg_color[0]);
                pthread_mutex_unlock(&mutex);
                ++row;
                Sleep(100);
            }
            col+=4;
        }

        Sleep(2000);

        //擦除Word
        col-=4;
        for(int i=row_index-1; i>=0; --i) {
            char hjzgg_word[60];
            memcpy(hjzgg_word, love_word[word_index][i], sizeof(love_word[word_index][i]));
            int len_hjzgg_word = strlen(hjzgg_word);
            int len_word = len_hjzgg_word;
            bool is_first = true;
            while(len_word >= 0) {
                int row = 1;
                for(int j=0; j<len_hjzgg_word; j+=2){
                    char one_word[2];//得到一个汉字
                    sprintf(one_word, "%c%c", hjzgg_word[j], hjzgg_word[j+1]);
                    COORD coord = {col, row};
                    pthread_mutex_lock(&mutex);
                    SetConsoleTextAttribute(o, hjzgg_color[color_index]);
                    SetConsoleCursorPosition(o, coord);
                    WriteConsole(o, one_word, 2, NULL, 0);
                    coord.Y += 1;
                    if(j+2 == len_word){
                        SetConsoleCursorPosition(o, coord);
                        WriteConsole(o, "__", 2, NULL, 0);
                        ++row;
                    }
                    SetConsoleTextAttribute(o, hjzgg_color[0]);
                    pthread_mutex_unlock(&mutex);
                    ++row;
                }
                if(is_first){
                    is_first = false;
                    hjzgg_word[len_hjzgg_word++] = ' ';
                    hjzgg_word[len_hjzgg_word++] = ' ';
                } else {
                    len_hjzgg_word-=2;
                }
                hjzgg_word[--len_word] = ' ';
                hjzgg_word[--len_word] = ' ';
                Sleep(20);
            }
            col-=4;
        }
        Sleep(1000);
    }
}

void* first_init_top(void* arg){
    char tmp[80];
    for(int ld=0; ld < 12; ++ld){
        COORD coord_top = { 0, ld};
        memset(tmp, ' ', sizeof(tmp));
        int cc = 0;
        while(cc < 60){
            while(buffer[ld][cc]==' ') ++cc;
            tmp[cc] = buffer[ld][cc];
            ++cc;
            pthread_mutex_lock(&mutex);
            SetConsoleCursorPosition(o, coord_top);
            WriteConsole(o, tmp, 60, NULL, 0);
            pthread_mutex_unlock(&mutex);
            Sleep(15);
        }
    }
}

void* screen_down_flower(void* arg){//从第26行开始打印
    srand((unsigned)time(NULL));//随机每束花是否显示
    int flower_row[3] = {11, 9, 11};
    int flower_col[3] = {20, 27, 20};
    int flower_begin_col[3] = {5, 33, 70};
    int flower_begin_row[3] = {26, 28, 26};
    struct timeval now;
    struct timespec outtime;
    pthread_mutex_lock(&mutex);
    while(true){
        int k = 0;
        while(k < 3){
            char flower[50][50] = {' '};
            int color_index = rand()%7;
            SetConsoleTextAttribute(o, hjzgg_color[color_index]);
            if(k==0 || k==2){
                for(int i=0; i<flower_row[k]; ++i)
                    for(int j=0; j<flower_col[k]; ++j)
                        flower[i][j] = flower1[i][j];
            } else {
                for(int i=0; i<flower_row[k]; ++i)
                    for(int j=0; j<flower_col[k]; ++j)
                        flower[i][j] = flower2[i][j];
            }
            for(int i=flower_begin_row[k], j=0; j<flower_row[k]; ++j, ++i){
                COORD coord = {flower_begin_col[k], i};
                SetConsoleCursorPosition(o, coord);
                WriteConsole(o, flower[j], flower_col[k], NULL, 0);
            }
            ++k;
            SetConsoleTextAttribute(o, hjzgg_color[0]);//重新设置为原来的原色
            gettimeofday(&now, NULL);
            outtime.tv_sec = now.tv_sec;
            outtime.tv_nsec = now.tv_usec * 100000;
            pthread_cond_timedwait(&cond, &mutex, &outtime);
        }
    }
    pthread_mutex_unlock(&mutex);
}

void* first_init_down(void* arg){
    char tmp[80];
    for(int rd=24; 12 <= rd; --rd){
        COORD coord_down = { 0, rd };
        SetConsoleCursorPosition(o, coord_down);
        memset(tmp, ' ', sizeof(tmp));
        int cc = 60;
        while(cc >= 0){
            while(buffer[rd][cc]==' ') --cc;
            tmp[cc] = buffer[rd][cc];
            --cc;
            pthread_mutex_lock(&mutex);
            SetConsoleCursorPosition(o, coord_down);
            WriteConsole(o, tmp, 60, NULL, 0);
            pthread_mutex_unlock(&mutex);
            Sleep(15);
        }
    }
}
//一箭穿心图案
void show_piercing_arrow(){
    for(int i=0; i<9; ++i) {
        pthread_mutex_lock(&mutex);
        COORD coord_down = { 13, i+7 };
        SetConsoleCursorPosition(o, coord_down);
        WriteConsole(o, piercing_arrow[i], 33, NULL, 0);
        pthread_mutex_unlock(&mutex);
    }
}

void first_init(){
    show_piercing_arrow();
    pthread_t tid[4];
//    screen_down_flower
    pthread_create(&tid[2], NULL, screen_down_flower, NULL);
//  show_love_word
    pthread_create(&tid[3], NULL, show_love_word, NULL);
//    心型 init_first
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);
    pthread_create(&tid[0], NULL, first_init_top, NULL);
    pthread_create(&tid[1], NULL, first_init_down, NULL);
    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
}

int main() {
    init_screen();
    bool first = true;
    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++ = ' ';
            }
        }
        if(first){
            first = false;
            first_init();
        }
        else {
            pthread_mutex_lock(&mutex);
            for (sy = 0; sy < 25; sy++) {
                COORD coord = { 0, sy };
                SetConsoleCursorPosition(o, coord);
                WriteConsole(o, buffer[sy], 60, NULL, 0);
            }
            pthread_mutex_unlock(&mutex);
        }
        Sleep(33);
    }
    return 0;
}

哪些部分能改各位程序员应该一目了然吧,好像是一个叫小眼的大佬写的

趁着我还用不上你们拿去吧
QAQ
连伞友都没有的我忍住不能哭


忍住不能哭
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281

推荐阅读更多精彩内容

  • ​ 我们在拿到一段论述后,如果要想对这篇论述进行评价,可能首先会去看分析推理过程,其实这是部队的,我们首先就要...
    O小米粥O阅读 848评论 0 49
  • 两天没写简书了,罪恶感很强。前天是真的忘了,昨天是太迟了。今早又要加班,所以赶紧睡了,可是今早五点多就醒来了,这也...
    一路修行ld阅读 66评论 0 0
  • 莎伦 2009-11-16 12:52 2009-11-15 11:19 冬天真是穷人的冬天 向所有没暖气的人们问...
    lijutong_010阅读 283评论 0 1
  • 一年前的我时常生病,医生的一番话让而我决定去改变,那时的我体重达到220斤,做什么事情都一副懒洋洋的样子,生活对我...
    夏石雨阅读 277评论 0 1
  • 我爱跑步,但是我不爱跑。 也不是不爱跑,就是坚持不下来…… 我喜欢跑步带给我的感觉,就像村上春树说的,跑步是最适合...
    米粥的江湖阅读 289评论 6 3