服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现一个闪烁的圣诞树

C语言实现一个闪烁的圣诞树

2022-07-29 10:30Linux猿 C/C++

本文详细讲解了C语言实现一个闪烁的圣诞树,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

C语言实现一个闪烁的圣诞树

图1 圣诞树

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <stdbool.h>
 
#define N 15
char 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 <= w/2; ++j) {
            getCoord(x + i, y - j);
            if(i % 3 || j % 2)
                printf("*");
            else {
                color(col2);
                printf("!");
                color(col1);
            }
                
            getCoord(x + i, y + j);
            if(i % 3 || j % 2)
                printf("*");
            else {
                color(col2);
                printf("!");
                color(col1);
            }
            
        }
    }
}
 
int main() {
    hideCursor();
    int colTop = 4;
    int colMid = 4;
    int colEnd = 13;
    while(true) {
        colTop = colTop == 4 ? 9 : 4;
        triangleLeft(5, 27.8, 2, colTop);
        triangleRight(5, 27.8, 2, colTop);
        Sleep(100);
        layer(5, 55, 10, 2);
        layer(9, 55, 16, 2);
        layer(14, 55, 26, 2);
        colMid = colMid == 4 ? 5 : 4;
        triangle(11, 55, 3, colMid);
        triangle(19, 60, 3, colMid);
        triangle(29, 42, 3, colMid);
        triangle(31, 57, 3, colMid);
        colEnd = colEnd == 13 ? 1 : 13;
        rectangle(40, 55, 15, 18, 6, colEnd);
        Sleep(200);
    }
    return 0;
}

上面便是圣诞树的简单实现,下面来说下原理:

函数 layer 画出树的层次,根据坐标来输出位置;

?
1
void layer(int x, int y, int num, int col)

函数 triangle 画出小三角形,作为点缀;

?
1
void triangle(int x, int y, int num, int col)

函数 triangleRight 和 triangleLeft 画出圣诞树顶部的蝴蝶结;

?
1
2
void triangleRight(double x, double y, double num, double col);
void triangleLeft(double x, double y, double num, double col);

函数 hideCursor 负责隐藏光标;

?
1
void hideCursor()

函数 getCoord 负责确定输出字符的位置;

?
1
void getCoord(double y, double x)

函数 color 负责设置输出的颜色;

?
1
void color(int a)

主函数的原理如下:

?
1
void color(int a)

主函数通过一个 while 循环,不断刷新圣诞树和圣诞树点缀的颜色。

以上所述是小编给大家介绍的C语言实现一个闪烁的圣诞树,希望对大家有所帮助。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/nyist_zxp/article/details/122117876

延伸 · 阅读

精彩推荐