一、前言
圣诞节快到了,又到了给对象画圣诞树的时候了,这次我们不用备忘录,我们用代码,属于程序员的浪漫。
二,效果展示
三,实现步骤
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 <windows.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#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 < d2 ? d1 : d2;
}
float f(float px, float py, float theta, float scale, int n) {
float d = 0.0f;
for (float r = 0.0f; r < 0.8f; r += 0.02f)
d = opUnion(d, sdCircle(T, 0.05f * scale * (0.95f - r)));
if (n > 0)
for (int t = -1; t <= 1; t += 2) {
float tt = theta + t * 1.8f;
float ss = scale * 0.9f;
for (float r = 0.2f; r < 0.8f; r += 0.1f) {
d = opUnion(d, f(T, tt, ss * 0.5f, n - 1));
ss *= 0.8f;
}
}
return d;
}
int ribbon() {
float x = (fmodf(sy, 0.1f) / 0.1f - 0.5f) * 0.5f;
return sx >= x - 0.05f && sx <= x + 0.05f;
}
int main(int argc, char* argv[]) {
int n = argc > 1 ? atoi(argv[1]) : 3;
float zoom = argc > 2 ? atof(argv[2]) : 1.0f;
for (sy = 0.8f; sy > 0.0f; sy -= 0.02f / zoom, putchar('\n'))
for (sx = -0.35f; sx < 0.35f; sx += 0.01f / zoom) {
if (f(0, 0, PI * 0.5f, 1.0f, n) < 0.0f) {
if (sy < 0.1f) {
putchar('.');
} else {
if (ribbon()) {
putchar('=');
} else {
putchar("............................#j&o"[rand() % 32]);
}
}
} else {
putchar(' ');
}
}
Sleep(1000);
printf("\n\n\n");
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
//_sleep(5000);
printf("Merry Christmas!\n");
Sleep(500);
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
Sleep(500);
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
printf("2022/12/24");
getchar();
}
2,彩色圣诞树
#include<windows.h>
#include<stdio.h>
#include<time.h>
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;i<height;i++){
if(colorOfLeaves==14){
Sleep(60);
}
goto_xy(25-i-1,i+3);
for(int j=2*i+2;j>0;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;
}
喜欢的话点个赞吧!!!