一、前言

圣诞节快到了,又到了给对象画圣诞树的时候了,这次我们不用备忘录,我们用代码,属于程序员的浪漫。

二,效果展示

三,实现步骤

windows.h简介

我们先思考一个问题,如何用C程序输出不同颜色的字体?一般来说,只靠应用程序本身很难达到这个目的,因为运行窗口的管理属于操作系统资源,而应用程序想要使用这些资源就必须通过操作系统提供的接口来实现。

为了解决这类问题,微软提供了丰富的Windows API(Application Programming Interface),这些API并不会直接在应用程序中展开,而是以动态链接的形式存在着,在应用程序需要API实现某些功能的时候,才调用相应的动态链接库。通过调用各种Windows API函数,应用程序可以管理它的用户界面、显示各种图形和文字、播放音乐等等。

windows.h是微软操作系统中非常重要的一个头文件,它包含了多个头文件以及多种API函数接口,比如Winbase.h(Windows内核函数等)、Winuser.h(Windows用户界面管理函数等)、Wingdi.h(Windows图形设备接口函数等)

四,完整代码

1,黑白圣诞树

#include #include #include #include #define PI 3.14159265359#define T px + scale * r * cosf(theta), py + scale * r * sin(theta)float sx, sy;float sdCircle(float px, float py, float r) {float dx = px - sx, dy = py - sy;return sqrtf(dx * dx + dy * dy) - r;}float opUnion(float d1, float d2) {return d1 

2,彩色圣诞树

#include#include#include void color(const unsigned short textColor);void goto_xy(int x, int y);void tree(int height,int colorOfLeaves);void snow(int n); /*根据参数改变字体颜色*/void color(const unsigned short textColor){if(textColor>=0&&textColor<=15){//参数在0-15的范围颜色SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),textColor);}else{//默认的字体颜色是白色SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);}}/*定位光标位置到指定坐标*/void goto_xy(int x, int y){HANDLE hout;hout=GetStdHandle(STD_OUTPUT_HANDLE);COORD pos={x,y};SetConsoleCursorPosition(hout,pos);}/*画圣诞树*/void tree(int height,int colorOfLeaves){//画五角星if(colorOfLeaves==10){color(3);}else{color(11);}goto_xy(24,3);printf("%c%c\n",161,239);//GBK中五角星对应的编码为A1EF,换成十进制就是161 239//画叶子color(colorOfLeaves);for(int i=1;i0;j--){printf("*");}printf("\n");}//画树干goto_xy(25-1,height+3);color(4);printf("||");printf("\n");}/*绘制雪景*/void snow(int n){srand(time(0));for(int i=0;i<n;i++){int x=rand()%50;int y=rand()%15;goto_xy(x,y);color(7);if(i<n-5){printf("·");}else{printf("*");} }}int main(){system("title 圣诞快乐!");system("mode con cols=50 lines=15");//设置窗口大小 while(1){snow(30);tree(10,10);Sleep(1000);//延迟1000毫秒tree(10,14);Sleep(1000);//延迟1000毫秒system("cls");//清屏}return 0;}

喜欢的话点个赞吧!!!