下面就是演示效果
创新互联公司是一家专业从事网站设计、做网站的网络公司。作为专业的建站公司,创新互联公司依托的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、网络营销推广及网站设计开发服务!动态圣诞树视频
一、实现步骤我们利用ANSI Escape Sequences实现文字和背景颜色的绘制,之后使用系统命令定时刷新窗口并重新绘制圣诞树,以实现动态效果。相关函数如下所示:
几个关键宏含义如下:
// 此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;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧