tft每日頭條

 > 生活

 > 怎麼用c語言編出聖誕樹

怎麼用c語言編出聖誕樹

生活 更新时间:2024-07-30 13:15:35

控制台打印一個聖誕樹:簡簡單單的C語言知識,真的很基礎,小白也能看得懂哦

/******************************* 聖誕樹 byC語言小白入門 *******************************/ #include<stdio.h> #include <stdlib.h> #include <time.h> #include<Windows.h> #define X 25 //畫面長度 int background[20][2 * X] = { 0 }; int SNOW = 30; //雪花密度 /******************************* 畫樹 *******************************/ void tree() { int i, j, x, y; for (i = 0; i < 3; i ) { y = i; for (j = 0; j < 2 * i 1; j ) { background[y][(X - i) j] = 1; } } for (i = 0; i < 5; i ) { y ; for (j = 0; j < 2 * (i 1) 1; j ) { background[y][(X - (i 1)) j] = 1; } } for (i = 0; i < 7; i ) { y ; for (j = 0; j < 2 * (i 3) 1; j ) { background[y][(X - (i 3)) j] = 1; } } for (i = 0; i < 5; i ) { y ; for (j = 0; j < 3; j ) { background[y][X (2 * j - 2)] = 2; } } } /******************************* 畫雪花 *******************************/ void snow() { int i; srand(time(NULL)); for (i = 0; i < SNOW; i ) { int x, y; x = rand() % (2 * X); y = rand() % 20; if ((background[y][x] == 0)) { background[y][x] = 3; } } } /******************************* 打印 *******************************/ void display() { int x, y; for (y = 0; y < 20; y ) { for (x = 0; x < 2 * X; x ) { if (background[y][x] == 0) { printf(" "); } if (background[y][x] == 1) { printf("0"); } if (background[y][x] == 2) { printf("|"); } if (background[y][x] == 3) { printf("*"); } } printf("\n"); } } /******************************* 清除雪花 *******************************/ void clear_snow() { int i, j; for (i = 0; i < 20; i ) { for (j = 0; j < 2 * X; j ) { if (background[i][j] == 3) { background[i][j] = 0; } } } } void main() { tree(); while (1) { snow(); display(); Sleep(1); system("cls"); clear_snow(); } }

最終的效果圖

怎麼用c語言編出聖誕樹(C語言設計一個閃閃的聖誕樹)1

也沒有那麼高大上的啦,就很簡單的,效果的話是動态的,可以閃動的呢。

更多有趣的C/C 項目開發,點擊【正在跳轉】加入群聊,即可獲取

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved