[ CSDN博客评选活动 ] 小伙伴们支持下:主页左上侧投票~


作者:Linux猿

简介:CSDN博客专家,华为云享专家,数据结构和算法、C/C++、面试、刷题、Linux尽管咨询我,关注我,有问题私聊!

关注专栏:Linux 技术(优质好文持续更新中……)

欢迎小伙伴们点赞、收藏⭐、留言


圣诞节来啦!看到很多小伙伴用各种语言画出了圣诞树,于是就想用 C 语言来画一颗圣诞树,有点粗糙,下面先来看一下效果图吧!

图1 圣诞树

下面来看下源码,如下所示:

#include #include #include #include #include #include #define N 15char str[] = {'*', ' ', '@', ' ', '#', ' ', '\'',  ' ', '$', ' ', '%', ' ', '&', ' ', '!'};void color(int a){    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);}void getCoord(double y, double x){    COORD pos = { x,y };    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);}void hideCursor(){    CONSOLE_CURSOR_INFO cursor= { 1, 0 };    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);}void layer(int x, int y, int num, int col) {    color(col);    getCoord(x, y);    int idx = rand()%N;    printf("%c", str[idx]);    for(int k = 1; k <= num; ++k) {        idx = rand()%N;        getCoord(x + k - 1, y);        printf("%c", str[idx]);        for(int i = 1; i <= (k*2-1)/2; i++) {            getCoord(x + k - 1, y - i);            idx = rand()%N;            printf("%c", str[idx]);            getCoord(x + k - 1, y + i);            idx = rand()%N;            printf("%c", str[idx]);        }    }}void triangle(int x, int y, int num, int col) {    getCoord(x, y);    color(col);    printf("*");    for(int i = 1; i <= num; ++i) {            int x1 = x + i;            int y1 = y - i;        for(int j = 0; j < i * 2 + 1; ++j) {            getCoord(x1, y1 + j);            printf("*");         }    }}void triangleRight(double x, double y, double num, double col) {    getCoord(x, y*2);    color(col);    printf("*");    for(int i = 1; i <= num; ++i) {            double x1 = x - i;            double y1 = y - i;        for(int j = 0; j < i * 2 + 1; ++j) {            getCoord(x1 + j, y1 * 2);            printf("*");         }    }}void triangleLeft(double x, double y, double num, double col) {    getCoord(x, y*2);    color(col);    printf("*");    for(int i = 1; i <= num; ++i) {            double x1 = x - i;            double y1 = y + i;        for(int j = 0; j < i * 2 + 1; ++j) {            getCoord(x1 + j, y1 * 2);            printf("*");         }    }}void rectangle(int x, int y, int h, int w, int col1, int col2) {    color(col1);    for(int i = 0; i <= h; ++i) {        for(int j = 0; j