小编给大家分享一下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;
}
上面便是圣诞树的简单实现,下面来说下原理:
函数 layer 画出树的层次,根据坐标来输出位置;
void layer(int x, int y, int num, int col)
函数 triangle 画出小三角形,作为点缀;
void triangle(int x, int y, int num, int col)
函数 triangleRight 和 triangleLeft 画出圣诞树顶部的蝴蝶结;
void triangleRight(double x, double y, double num, double col);
void triangleLeft(double x, double y, double num, double col);
函数 hideCursor 负责隐藏光标;
void hideCursor()
函数 getCoord 负责确定输出字符的位置;
void getCoord(double y, double x)
函数 color 负责设置输出的颜色;
void color(int a)
主函数的原理如下:
void color(int a)
主函数通过一个 while 循环,不断刷新圣诞树和圣诞树点缀的颜色。
以上是“C语言如何实现一个闪烁的圣诞树”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。