亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語言實現(xiàn)一個閃爍的圣誕樹

 更新時間:2021年12月27日 15:55:26   作者:Linux猿  
本文詳細講解了C語言實現(xiàn)一個閃爍的圣誕樹,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

圣誕節(jié)來啦!看到很多小伙伴用各種語言畫出了圣誕樹,于是就想用 C 語言來畫一顆圣誕樹,有點粗糙,下面先來看一下效果圖吧!

圖1 圣誕樹

下面來看下源碼,如下所示:

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

上面便是圣誕樹的簡單實現(xiàn),下面來說下原理:

函數(shù) layer 畫出樹的層次,根據(jù)坐標來輸出位置;

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

函數(shù) triangle 畫出小三角形,作為點綴;

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

函數(shù)?triangleRight 和?triangleLeft 畫出圣誕樹頂部的蝴蝶結(jié);

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

函數(shù)?hideCursor 負責隱藏光標;

void hideCursor()

函數(shù)?getCoord 負責確定輸出字符的位置;

void getCoord(double y, double x)

函數(shù) color 負責設(shè)置輸出的顏色;

void color(int a)

主函數(shù)的原理如下:

void color(int a)

主函數(shù)通過一個 while 循環(huán),不斷刷新圣誕樹和圣誕樹點綴的顏色。

以上所述是小編給大家介紹的C語言實現(xiàn)一個閃爍的圣誕樹,希望對大家有所幫助。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論