網(wǎng)站首頁 編程語言 正文
圣誕節(jié)來啦!看到很多小伙伴用各種語言畫出了圣誕樹,于是就想用 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; }
上面便是圣誕樹的簡單實現(xiàn),下面來說下原理:
函數(shù) layer 畫出樹的層次,根據(jù)坐標來輸出位置;
void layer(int x, int y, int num, int col)
函數(shù) triangle 畫出小三角形,作為點綴;
void triangle(int x, int y, int num, int col)
函數(shù)?triangleRight 和?triangleLeft 畫出圣誕樹頂部的蝴蝶結;
void triangleRight(double x, double y, double num, double col); void triangleLeft(double x, double y, double num, double col);
函數(shù)?hideCursor 負責隱藏光標;
void hideCursor()
函數(shù)?getCoord 負責確定輸出字符的位置;
void getCoord(double y, double x)
函數(shù) color 負責設置輸出的顏色;
void color(int a)
主函數(shù)的原理如下:
void color(int a)
主函數(shù)通過一個 while 循環(huán),不斷刷新圣誕樹和圣誕樹點綴的顏色。
原文鏈接:https://blog.csdn.net/nyist_zxp/article/details/122117876
相關推薦
- 2023-07-14 css :如何讓背景平鋪整個頁面
- 2022-08-19 insert語句返回新增主鍵id失敗的解決方法
- 2024-02-01 webstorm中Line comment at first column,Block commen
- 2023-04-07 React?Fiber構建源碼解析_React
- 2023-06-21 python?__add__()的具體使用_python
- 2022-02-22 算出兩個時間范圍是否有交集(前后端通用算法)
- 2022-10-25 Python繪制loss曲線和準確率曲線實例代碼_python
- 2022-01-18 解決CSRF verification failed. Request aborted.的問題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支