本文首先发表在我的个人博客上,欢迎访问
https://sunguoqi.com/2021/12/25/christmas/
世上本没有圣诞老人,所有的礼物和惊喜都来自爱你的人!
文章目录
一、前言
今天是圣诞节,不知道啥时候起,圣诞节也成撒狗粮的日子了。🙃
朋友圈各种秀对象画的圣诞树的狗粮也是吃够了(好酸臭哇)。
咱是没那福分,想要圣诞树看来是只有自己动手了!
二、桌面应用程序
记得前几年的圣诞节,在某个交流群里有人分享过这样一个程序,当时啥也不懂,只知道很酷。惊叹之后就没有然后了,今天追根溯源找到了这组桌面小摆件的出处。
这是一个国外的网站,目测应该是做桌面小部件的那种网站,在这个站点上选取你喜欢的程序,下载下来解压双击运行就可以显示在你的桌面上了。
下载下来的是exe
可执行文件,支持多开,右键有一些工具选项卡,可以设置开机自启,显示在顶部等等。
因为是打包好的exe
程序,直接分享给朋友,在朋友电脑上就可以打开了,非常的 Nice!
三、Web 网页程序
Web 开发是代码和艺术的结合,做 Web 开发真的是一件很酷的事情!
下面是我收集的一些圣诞网页,点击链接即可在线预览。
代码有点多,限于文章篇幅,没有在文章中展示,源码获取方式在文末。
1、圣诞贺卡
在线预览:https://box.sunguoqi.com/tree/demo1/
2、更好的我们
在线预览:https://box.sunguoqi.com/tree/demo2/
3、流星圣诞树
在线预览:https://box.sunguoqi.com/tree/demo3/
4、星球圣诞树
在线预览:https://box.sunguoqi.com/tree/demo4/
6、Merry Christmas
在线预览:https://box.sunguoqi.com/tree/demo5/
7、CodePen
CodePen 网址:https://codepen.io/
授之以鱼,不如授之以渔。CodePen 是一款优秀的前端 demo 展示平台,里面有很多大佬的作品,在检索页面搜索Christmas Tree
选择你喜欢的 demo 点进去,就可以白嫖他的前端代码了。
四、Python Turtle 绘图
后端语言我主要使用的是 python,毕竟人生苦短嘛。。。。。。当然我只会一点皮毛(狗头)。
众所周知,python 中的 Turtle 库(海龟库)是一个非常强大的绘图库,下面是利用 Turtle 制作出来的一些圣诞树作品。
1、Demo1
程序源码:
import turtle as t # as就是取个别名,后续调用的t都是turtle
from turtle import *
import random as r
import time
n = 100.0
speed("fastest") # 定义速度
screensize(bg='black') # 定义背景颜色,可以自己换颜色
left(90)
forward(3 * n)
color("orange", "yellow") # 定义最上端星星的颜色,外圈是orange,内部是yellow
begin_fill()
left(126)
for i in range(5): # 画五角星
forward(n / 5)
right(144) # 五角星的角度
forward(n / 5)
left(72) # 继续换角度
end_fill()
right(126)
def drawlight(): # 定义画彩灯的方法
if r.randint(0, 30) == 0: # 如果觉得彩灯太多,可以把取值范围加大一些,对应的灯就会少一些
color('tomato') # 定义第一种颜色
circle(6) # 定义彩灯大小
elif r.randint(0, 30) == 1:
color('orange') # 定义第二种颜色
circle(3) # 定义彩灯大小
else:
color('dark green') # 其余的随机数情况下画空的树枝
color("dark green") # 定义树枝的颜色
backward(n * 4.8)
def tree(d, s): # 开始画树
if d <= 0: return
forward(s)
tree(d - 1, s * .8)
right(120)
tree(d - 3, s * .5)
drawlight() # 同时调用小彩灯的方法
right(120)
tree(d - 3, s * .5)
right(120)
backward(s)
tree(15, n)
backward(n / 2)
for i in range(200): # 循环画最底端的小装饰
a = 200 - 400 * r.random()
b = 10 - 20 * r.random()
up()
forward(b)
left(90)
forward(a)
down()
if r.randint(0, 1) == 0:
color('tomato')
else:
color('wheat')
circle(2)
up()
backward(a)
right(90)
backward(b)
t.color("dark red", "red") # 定义字体颜色
t.write("Merry Christmas ", align="center", font=("Comic Sans MS", 40, "bold")) # 定义文字、位置、字体、大小
def drawsnow(): # 定义画雪花的方法
t.ht() # 隐藏笔头,ht=hideturtle
t.pensize(2) # 定义笔头大小
for i in range(200): # 画多少雪花
t.pencolor("white") # 定义画笔颜色为白色,其实就是雪花为白色
t.pu() # 提笔,pu=penup
t.setx(r.randint(-350, 350)) # 定义x坐标,随机从-350到350之间选择
t.sety(r.randint(-100, 350)) # 定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始
t.pd() # 落笔,pd=pendown
dens = 6 # 雪花瓣数设为6
snowsize = r.randint(1, 10) # 定义雪花大小
for j in range(dens): # 就是6,那就是画5次,也就是一个雪花五角星
# t.forward(int(snowsize)) #int()取整数
t.fd(int(snowsize))
t.backward(int(snowsize))
# t.bd(int(snowsize)) #注意没有bd=backward,但有fd=forward,小bug
t.right(int(360 / dens)) # 转动角度
drawsnow() # 调用画雪花的方法
t.done() # 完成,否则会直接关闭
2、Demo2
程序源码:
import turtle
screen = turtle.Screen()
screen.setup(800,600)
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()
circle.goto(0,280)
circle.stamp()
k = 0
for i in range(1, 17):
y = 30*i
for j in range(i-k):
x = 30*j
square.goto(x,-y+280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
if i % 4 == 0:
x = 30*(j+1)
circle.color('red')
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
k += 2
if i % 4 == 3:
x = 30*(j+1)
circle.color('yellow')
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
square.color('brown')
for i in range(17,20):
y = 30*i
for j in range(3):
x = 30*j
square.goto(x,-y+280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
turtle.exitonclick()
3、Demo3
程序源码:
from turtle import *
import time
setup(500, 500, startx=None, starty=None)
speed(0)
pencolor("#008500")
pensize(10)
penup()
hideturtle()
goto(0, 150)
showturtle()
pendown()
shape(name="classic")
# 2
penup()
goto(-55, 34)
pendown()
seth(-120)
for i in range(10):
fd(8)
right(5)
penup()
goto(50, 35)
seth(-60)
pendown()
for i in range(10):
fd(8)
left(5)
seth(-120)
penup()
fd(10)
seth(-145)
pendown()
for i in range(5):
fd(10)
right(15)
penup()
fd(10)
seth(-145)
pendown()
for i in range(5):
fd(12)
right(15)
penup()
fd(8)
seth(-145)
pendown()
for i in range(5):
fd(10)
right(15)
penup()
seth(-155)
fd(8)
pendown()
for i in range(5):
fd(11)
right(15)
.........................省略部分代码...................
# 星星
seth(-15)
koc(-120, -70, 10)
seth(10)
koc(100, -20, 10)
seth(-10)
koc(10, 40, 10)
seth(30)
koc(-80, 60, 10)
koc(100, -150, 10)
koc(-140, -150, 10)
koc(20, 120, 10)
# 袜子
seth(-20)
pensize(2)
penup()
goto(-20, 80)
pencolor("black")
pendown()
fillcolor("red")
fd(25)
circle(4, 180)
fd(25)
circle(4, 180)
penup()
goto(-15, 80)
pendown()
begin_fill()
fillcolor("red")
seth(-120)
fd(20)
seth(150)
fd(5)
circle(7, 180)
fd(15)
circle(5, 90)
fd(30)
seth(160)
fd(18)
end_fill()
penup()
seth(0)
goto(100, -230)
pendown()
write("Happy Merry Christmas ", align="center", font=("Comic Sans MS", 24, "bold"))
done()
4、Demo4
程序源码:
# coding=utf-8
# 此为部分代码
import turtle
from star import draw_star
from bear import draw_bear
from tree import draw_tree
from stake import draw_stake
from light import draw_light
turtle.screensize(1200, 800, "#FFFFCC")
# 画笔颜色
turtle.pencolor("#cc0033")
# 抬起画笔
turtle.penup()
# 画笔颜色、尺寸
turtle.pencolor("#cc0033")
turtle.pensize(5)
# 放下画笔
turtle.pendown()
# draw_star(star_len=100, start_pos=[0, 0], pencolor="#333333", pensize=2, fill=True)
# draw_star(star_len=80, start_pos=[5, -2], pencolor="#333333", pensize=2, fill=True)
# draw_bear(start_pos=[-50, -100], scale=0.5)
# 树干
draw_stake(start_pos=[0, -150])
# 树
draw_tree(start_pos=[0, 50], scale=1)
draw_tree(start_pos=[0, 75], scale=1.3)
draw_tree(start_pos=[0, 95], scale=1.65)
draw_tree(start_pos=[0, 105], scale=2.4)
draw_tree(start_pos=[0, 130], scale=3.2)
# 星星
draw_star(star_len=23, start_pos=[-30, 130], pencolor="#333333", pensize=2, fill=True)
draw_star(star_len=15, start_pos=[-19.5, 126.5], pencolor="#333333", pensize=2, fill=True)
# 小熊
draw_bear(start_pos=[-20, 30], scale=10)
draw_bear(start_pos=[60, -30], scale=10)
draw_bear(start_pos=[0, -135], scale=10)
# 彩灯
draw_light(start_pos=[-100, -165], color="#FF00FF", scale=5, seth=180)
draw_light(start_pos=[-50, -175], color="#FFFF00", scale=5, seth=210)
draw_light(start_pos=[-10, -175], color="#EE6363", scale=5, seth=250)
draw_light(start_pos=[60, -175], color="#9370DB", scale=5, seth=250)
draw_light(start_pos=[-100, -100], color="#FFFF00", scale=5, seth=180)
draw_light(start_pos=[-50, -100], color="#EE6363", scale=5, seth=210)
draw_light(start_pos=[-10, -80], color="#FF00FF", scale=5, seth=250)
draw_light(start_pos=[60, -120], color="#FFFF00", scale=5, seth=250)
draw_light(start_pos=[30, 0], color="#9370DB", scale=5, seth=0)
draw_light(start_pos=[-10, -50], color="#FFFF00", scale=5, seth=210)
draw_light(start_pos=[-30, 15], color="#FFFF00", scale=5, seth=150)
draw_light(start_pos=[0, 60], color="#EE6363", scale=5, seth=300)
turtle.done()
5、将 python 程序打包成 exe 教程
python 程序虽然运行起来简单,但是没法像 exe 可执行文件那样直接双击打开,不过我们可以将 python 程序打包成 exe 文件,步骤如下。
打开终端,安装pyinstaller
库:pip install pyinstaller
,在文件目录下执行以下命令:
pyinstaller -F -i favicon .ico index.py --noconsole
具体参数的用法可以上网去查,输入上面的命令后回车,打包开始进行…
打包成功后,在当前目录下会多出几个文件夹,exe 程序在dist
分发目录下,双击即可运行。
五、C 语言控制台程序
C 语言在终端中输出用字符串组成的圣诞树。emmm,好吧,我 C 语言的知识基本忘完了。
1、Demo1
程序源码:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265359
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;
}
#define T px + scale * r * cosf(theta), py + scale * r * sin(theta)
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 main(int argc, char* argv[]) {
int n = argc > 1 ? atoi(argv[1]) : 3;
for (sy = 0.8f; sy > 0.0f; sy -= 0.02f, putchar('\n'))
for (sx = -0.35f; sx < 0.35f; sx += 0.01f)
putchar(f(0, 0, PI * 0.5f, 1.0f, n) < 0 ? '*' : ' ');
}
2、Demo2
程序源码
#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();
}
}
3、其他 C 语言代码
参考知乎的一篇文章:https://www.zhihu.com/question/27015321
六、源码获取
微信搜索公众号【不负人间理想】,关注公众号后,回复关键词【圣诞树】即可获得下载链接!!!
七、后记
除了圣诞节,今天还是 22 考研的日子,学长学姐们考研加油哇!!!一研为定!!!一战成硕!!!冲冲冲!!!