C語言實(shí)現(xiàn)一個(gè)閃爍的圣誕樹
圣誕節(jié)來啦!看到很多小伙伴用各種語言畫出了圣誕樹,于是就想用 C 語言來畫一顆圣誕樹,有點(diǎn)粗糙,下面先來看一下效果圖吧!

圖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;
}
上面便是圣誕樹的簡(jiǎn)單實(shí)現(xiàn),下面來說下原理:
函數(shù) layer 畫出樹的層次,根據(jù)坐標(biāo)來輸出位置;
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 畫出圣誕樹頂部的蝴蝶結(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ù)通過一個(gè) while 循環(huán),不斷刷新圣誕樹和圣誕樹點(diǎn)綴的顏色。
以上所述是小編給大家介紹的C語言實(shí)現(xiàn)一個(gè)閃爍的圣誕樹,希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
strings命令分析淺談Go和C++編譯時(shí)的一點(diǎn)小區(qū)別
今天小編就為大家分享一篇關(guān)于strings命令分析淺談Go和C++編譯時(shí)的一點(diǎn)小區(qū)別,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
C++實(shí)現(xiàn)LeetCode(18.四數(shù)之和)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(18.四數(shù)之和),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
QT線程池的使用(QThreadPool類和QRunnable類)
本文主要介紹了QT線程池的使用(QThreadPool類和QRunnable類),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
C++語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
Visual?Studio?2022?安裝低版本?.Net?Framework的圖文教程
這篇文章主要介紹了Visual?Studio?2022?如何安裝低版本的?.Net?Framework,首先打開?Visual?Studio?Installer?可以看到vs2022?只支持安裝4.6及以上的版本,那么該如何安裝4.6以下的版本,下面將詳細(xì)介紹,需要的朋友可以參考下2022-09-09

