下面就是演示效果
創(chuàng)新互聯(lián)公司是一家專業(yè)從事網(wǎng)站設(shè)計(jì)、做網(wǎng)站的網(wǎng)絡(luò)公司。作為專業(yè)的建站公司,創(chuàng)新互聯(lián)公司依托的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營經(jīng)驗(yàn),為您提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣及網(wǎng)站設(shè)計(jì)開發(fā)服務(wù)!動(dòng)態(tài)圣誕樹視頻
一、實(shí)現(xiàn)步驟我們利用ANSI Escape Sequences實(shí)現(xiàn)文字和背景顏色的繪制,之后使用系統(tǒng)命令定時(shí)刷新窗口并重新繪制圣誕樹,以實(shí)現(xiàn)動(dòng)態(tài)效果。相關(guān)函數(shù)如下所示:
幾個(gè)關(guān)鍵宏含義如下:
// 此C程序用于生成一棵圣誕樹
#include#include#include#include#include#define RefRate 40000
#define randomness 5 // 越高樹葉越少
#define WIDTH_FACTOR 4
void clrscr()
{system("@cls||clear");
}
void printRandLeaf()
{char leaftypes[5] = {'.', '*', '+', 'o', 'O' };
int temp = rand() % randomness;
// Giving preference to *
if (temp == 1){int other = rand() % 5;
if(other == 3)
printf("\x1b[38;5;160m%c \x1b[0m", leaftypes[other]);
else if(other == 2)
printf("\x1b[38;5;226m%c \x1b[0m", leaftypes[other]);
else
printf("%c ", leaftypes[other]);
}else
printf("\x1b[32m%c \x1b[0m", leaftypes[1]);
}
void triangle(int f, int n, int toth)
{int i, j, k = WIDTH_FACTOR * toth - 2;
for (i = 0; i< f - 1; i++)
k--;
// number of rows
for (i = f - 1; i< n; i++) {// space handler
for (j = 0; j< k; j++)
printf(" ");
// decrementing k after each loop
k = k - 1;
// number of columns, printing stars
for (j = 0; j<= i; j++)
printRandLeaf();
printf("\n");
}
}
// Prints multiple triangles
void printTree(int h)
{int start = 1, stop = 0, diff = 3;
while (stop< h + 1) {stop = start + diff;
triangle(start, stop, h);
diff++;
start = stop - 2;
}
}
// Prints bottom part.
void printLog(int n)
{int i, j, k = WIDTH_FACTOR * n - 4;
for (i = 1; i<= 6; i++) {// space handler
for (j = 0; j< k; j++)
printf(" ");
for (j = 1; j<= 6; j++)
printf("\x1b[38;5;58m#\x1b[0m");
printf("\n");
}
}
// Driver code
int main()
{srand(time(NULL));
int ht = 9;
printf("\n*********MERRY CHRISTMAS*********\n\n");
// refresh loop
while (1) {clrscr();
printf("\n\n\n");
printTree(ht);
printLog(ht);
usleep(RefRate);
}
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧