欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言實(shí)現(xiàn)一個(gè)閃爍的圣誕樹(shù)

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

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

圖1 圣誕樹(shù)

下面來(lái)看下源碼,如下所示:

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

上面便是圣誕樹(shù)的簡(jiǎn)單實(shí)現(xiàn),下面來(lái)說(shuō)下原理:

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

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

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

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

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

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

函數(shù)?hideCursor 負(fù)責(zé)隱藏光標(biāo);

void hideCursor()

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

void getCoord(double y, double x)

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

void color(int a)

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

void color(int a)

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

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

相關(guān)文章

最新評(píng)論