管理员
2024-11-25 15:54:33
0
控制台打印一个圣诞树:简简单单的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++项目开发,点击【正在跳转】加入群聊,即可获取