前言
halo,包子们上午好
圣诞节快到了
今天我的好兄弟们安排了一波圣诞节的Python代码
主要是用海龟(turtle)画图实现的,非常简单的啦
话不多说,直接开整
相关文件
关注小编,私信小编领取哟!
当然别忘了一件三连哟~~
公众号:Python日志
开发工具
Python版本:3.7.8
相关模块:
turtle模块;
random模块;
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
一、圣诞树雪花+树
效果展示
代码展示
import turtle as t
from turtle import *
import random as r
import time
import turtle# 窗口设置
turtle.title("圣诞树雪花+树 Python资料大礼包加QQ群:494958217")n = 100.0speed("fastest")
screensize(bg='black')
left(90)
forward(3*n)
color("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: returnforward(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=hideturtlet.pensize(2) #定义笔头大小for i in range(200): #画多少雪花t.pencolor("white") #定义画笔颜色为白色,其实就是雪花为白色t.pu() #提笔,pu=penupt.setx(r.randint(-350,350)) #定义x坐标,随机从-350到350之间选择t.sety(r.randint(-100,350)) #定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始t.pd() #落笔,pd=pendowndens = 6 #雪花瓣数设为6snowsize = 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,小bugt.right(int(360/dens)) #转动角度drawsnow()#画雪花
t.done() # 完成,否则会直接关闭
二、爱心圣诞树
效果展示
代码展示
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 12 12:29:09 2020@author: haoyu
"""import turtle as t
import random
import turtle# 窗口设置
turtle.title("爱心圣诞树 Python资料大礼包加QQ群:494958217")
# 爱心函数
# 将爱心分为两个半圆与一个正方形
# r为半圆半径,l = 2r为正方形边长
# 调整半径即可调整爱心大小
def loving_heart(r):l = 2 * rt.left(45)t.forward(l)t.circle(r, 180)t.right(90)t.circle(r, 180)t.forward(l)# 树函数(递归)
def tree(d, s):if d <= 0:returnt.forward(s)tree(d - 1, s * .8)t.right(120)tree(d - 3, s * .5)t.right(120)tree(d - 3, s * .5)t.right(120)t.backward(s) #回退函数#画爱心部分
t.penup()
t.goto(0,200) #设置起点位置
t.pendown()
t.pencolor('pink') #设置画笔颜色
t.color('pink')
t.begin_fill() #对图形进行填充
loving_heart(20) #执行画爱心函数
t.end_fill()#画树部分
n = 100
t.speed('fastest')
#t.Turtle().screen.delay(0)
t.right(225)
t.color("dark green")
t.backward(n * 4.8)
tree(15, n)
t.backward(n / 5)#绘制落叶
for i in range(200):a = 200 - 400 * random.random()b = 10 - 20 * random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()if random.randint(0, 1) == 0:t.color('tomato')else:t.color('wheat')t.circle(2)t.up()t.backward(a)t.right(90)t.backward(b)
t.hideturtle()
三、圣诞树烟花特效
效果展示
代码展示
'''
name:圣诞树+烟火
author:Babysen
'''import turtle as t
import random
import threading
import time
import tkinter as tk
import math
from math import cos, sin, atan, sqrt
import numpy as np
import turtle# 窗口设置
turtle.title("圣诞树烟花特效 Python资料大礼包加QQ群:494958217")t.screensize(bg='black') # 定义背景颜色# 心函数
def loving_heart(r):l = 2 * rt.left(45)t.forward(l)t.circle(r, 180)t.right(90)t.circle(r, 180)t.forward(l)# 星函数
def loving_star(n):for i in range(5):t.forward(n / 5)t.right(144)t.forward(n / 5)t.left(72)# 树函数(递归)
def tree(d, s):if d <= 0:returnt.width(5)t.forward(s)tree(d - 1, s * .8)t.right(120)tree(d - 3, s * .5)t.right(120)tree(d - 3, s * .5)t.right(120)t.backward(s) # 回退函数# 画爱心部分
t.penup()
t.goto(0, 200) # 设置起点位置
t.pendown()
t.pencolor('red') # 设置画笔颜色
t.color('red')
t.begin_fill() # 对图形进行填充
loving_heart(20) # 执行画爱心函数
t.end_fill()# 画树部分
n = 100
t.speed(0)
# t.Turtle().screen.delay(0)
t.right(225)
t.color("dark green")
t.backward(n * 4.8)
tree(15, n)
t.backward(n / 5)# 绘制落叶
for i in range(100):a = 100 - 200 * random.random()b = 10 - 20 * random.random()t.speed(0)t.up()t.forward(b)t.left(90)t.forward(a)t.down()if random.randint(1, 2) == 1:t.color('tomato')else:t.color('wheat')t.circle(4)t.up()t.backward(a)t.right(90)t.backward(b)# 绘制雪花
def drawsnow(): # 定义画雪花的方法t.speed(0)t.ht() # 隐藏笔头,ht=hideturtlet.pensize(2) # 定义笔头大小for i in range(160): # 画多少雪花t.pencolor("white") # 定义画笔颜色为白色,其实就是雪花为白色t.pu() # 提笔,pu=penupt.setx(random.randint(-350, 350)) # 定义x坐标,随机从-350到350之间选择t.sety(random.randint(-100, 350)) # 定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始t.pd() # 落笔,pd=pendowndens = 6 # 雪花瓣数设为6snowsize = random.randint(2, 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,小bugt.right(int(360 / dens)) # 转动角度drawsnow()# 画五角星
for i in range(-200, 200, 20):t.penup()t.goto(i, 300) # 设置起点位置t.pendown()t.pencolor('yellow') # 设置画笔颜色t.color('yellow')t.begin_fill() # 对图形进行填充loving_star(30)t.end_fill()t.right(0)for i in range(-150, 150, 20):t.penup()t.goto(i, 280) # 设置起点位置t.pendown()t.pencolor('yellow') # 设置画笔颜色t.color('yellow')t.begin_fill() # 对图形进行填充loving_star(30)t.end_fill()t.right(0)for i in range(-100, 100, 20):t.penup()t.goto(i, 260) # 设置起点位置t.pendown()t.pencolor('yellow') # 设置画笔颜色t.color('yellow')t.begin_fill() # 对图形进行填充loving_star(30)t.end_fill()t.right(0)for i in range(-50, 50, 20):t.penup()t.goto(i, 240) # 设置起点位置t.pendown()t.pencolor('yellow') # 设置画笔颜色t.color('yellow')t.begin_fill() # 对图形进行填充loving_star(30)t.end_fill()t.right(0)for i in range(-20, 21, 20):t.penup()t.goto(i, 220) # 设置起点位置t.pendown()t.pencolor('yellow') # 设置画笔颜色t.color('yellow')t.begin_fill() # 对图形进行填充loving_star(30)t.end_fill()t.right(0)# 写下署名
t.color("white") # 填充颜色
t.up() # 抬笔
t.goto(170, -240)
t.down() # 落笔
t.write("Author:Babysen", font=("Times New Roman", 18, "normal"))
t.ht()
# t.done()
t.hideturtle()
time.sleep(2)# 烟火
WIDTH = 0
HEIGHT = 0
ORI = (0, 0)
COLOR = {'0': '#070920', 'navyblue': '#000080'}
FIRE = []def setParam():global WIDTHglobal HEIGHTglobal ORIglobal FIREWIDTH = t.window_width()HEIGHT = t.window_height()ORI = (0, -HEIGHT / 2)# goldFIRE.append(['white', '#FFD700', '#DAA520', '#BDB76B', ])# redFIRE.append(['white', '#F08080', '#A0522D', '#DC143C', ])# greenFIRE.append(['white', '#7FFF00', '#32CD32', '#006400', ])# cyanFIRE.append(['white', '#40E0D0', '#00FFFF', '#008080', ])# pinkFIRE.append(['white', '#FF69B4', '#FF1493', '#8B008B', ])# buffFIRE.append(['white', '#FFE4C4', '#FFDAB9', '#F0FFF0', ])# bluishFIRE.append(['white', '#ADD8E6', '#AFEEEE', '#00BFFF', ])def dist(a, b): # 距离return ((a - b) * (a - b)).sum()def shoot():t.tracer(4) # 在循环中,图形将一次画出4次循环的图t.pu()target = np.array((np.random.randint(-WIDTH // 6, WIDTH // 6),HEIGHT // 6)) # 都在同一高度angle = atan((target - ORI)[1] / (target - ORI)[0])if angle < 0:angle += math.pi # 纠正负方向unit = np.array([cos(angle), sin(angle)]) # 方向向量print('angle: ', angle)print('target: ', target)# 镜头的顺序坐标seq = np.array([ORI, ORI - 55 * unit, ORI - 105 * unit, ORI - 155 * unit])t.speed(0) # 瞬动while dist(seq[0], target) > 120: # 如果没有到达爆炸中心seq = seq + 30 * unitt.goto(seq[1])t.pd()t.width(3)t.pencolor('white')t.goto(seq[0])t.pu()t.goto(seq[2])t.pd()t.width(3)t.pencolor('yellow')t.goto(seq[1])t.pu()# 清除发射上去的光束尾迹t.goto(seq[3])t.pd()t.width(10)t.pencolor(COLOR['0'])t.goto(seq[2])t.pu()time.sleep(0.05)# 发射上去的光束最后清除t.pd()t.width(10)t.goto(seq[0])t.pu()time.sleep(0.1)return target # 就在爆炸中心def explode(center):number = np.random.randint(50, 100) # 光束数colormode = np.random.randint(0, 5)unit = np.array([]) # 方向向量spd = [] # 每个光束的速度seq = [] # 每个波束的顺序坐标if colormode <= 1:coloridx = [np.random.randint(0, len(FIRE))]elif colormode == 2:coloridx = [np.random.randint(0, len(FIRE)),np.random.randint(0, len(FIRE))]elif colormode == 3:coloridx = [np.random.randint(0, len(FIRE)),np.random.randint(0, len(FIRE)),np.random.randint(0, len(FIRE))]elif colormode == 4:coloridx = []# 不同大小烟花的步骤steps = int((number ** 0.5) * 2)# 初始化for i in range(number):angle = np.random.rand() * 2 * math.pi - math.piunit = np.append(unit, [cos(angle), sin(angle)]).reshape(-1, 2)seq = np.append(seq,[center,center - 10 * unit[i], center - 50 * unit[i],center - 90 * unit[i], center - 130 * unit[i],center - 135 * unit[i]])spd.append(int(15 + (np.random.rand() - 0.5) * 5))if colormode <= 1:coloridx.append(coloridx[0])elif colormode == 2:coloridx.append(coloridx[np.random.randint(0, 2)])elif colormode == 3:coloridx.append(coloridx[np.random.randint(0, 3)])elif colormode == 4:coloridx.append(np.random.randint(0, len(FIRE)))seq = seq.reshape([-1, 6, 2]).astype(np.int32)t.tracer(0x3f3f3f3f) # 关闭自动更新,0x3f3f3f3f是一个大数字for stage in range(steps):for i in range(number):seq[i] = seq[i] + spd[i] * unit[i]seq[i][4] = centerfor cur in range(4):t.pu()t.goto(seq[i][cur + 1])t.pd()t.pencolor(FIRE[coloridx[i]][cur])t.width(4 - cur)t.goto(seq[i][cur])t.pu()if stage >= 5: # 等待所有光束就位t.update()time.sleep(0.04)# 清除每次绽放的烟花for cur in range(4, -1, -1):for i in range(number):t.pu()t.goto(seq[i][cur + 1])t.pd()t.pencolor(COLOR['0'])t.width(100)t.goto(seq[i][cur])t.pu()time.sleep(0.02)t.update()def main():t.setup(700, 750, 100, 0)setParam()while True:point = shoot()explode(point)exitonclick() # 在任何位置单击退出if __name__ == '__main__':main()
四、好看圣诞树
效果展示
代码展示
from turtle import *
import time
import turtle# 窗口设置
turtle.title("好看圣诞树 Python资料大礼包加QQ群:494958217")setup(500, 500, startx=None, starty=None)
speed(0)
pencolor("red")
pensize(10)
penup()
hideturtle()
goto(0, 150)
showturtle()
pendown()
shape(name="classic")
# 1
seth(-120)
for i in range(10):fd(12)right(2)
penup()
goto(0, 150)
seth(-60)
pendown()
for i in range(10):fd(12)left(2)
seth(-150)
penup()
fd(10)
pendown()
for i in range(5):fd(10)right(15)
seth(-150)
penup()
fd(8)
pendown()
for i in range(5):fd(10)right(15)
seth(-155)
penup()
fd(5)
pendown()
for i in range(5):fd(7)right(15)
# 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)
# 3
penup()
goto(-100, -40)
seth(-120)
pendown()
for i in range(10):fd(6)right(3)
penup()
goto(80, -39)
seth(-50)
pendown()
for i in range(10):fd(6)left(3)
seth(-155)
penup()
fd(10)
pendown()
for i in range(5):fd(8)right(10)
penup()
fd(8)
seth(-145)
pendown()
for i in range(7):fd(8)right(10)
penup()
fd(8)
seth(-145)
pendown()
for i in range(7):fd(7)right(10)
penup()
fd(8)
seth(-145)
pendown()
for i in range(7):fd(7)right(10)
penup()
fd(8)
seth(-140)
pendown()
for i in range(7):fd(6)right(10)# 4
penup()
goto(-120, -95)
seth(-130)
pendown()
for i in range(7):fd(10)right(5)
penup()
goto(100, -95)
seth(-50)
pendown()
for i in range(7):fd(10)left(5)
penup()
seth(-120)
fd(10)
seth(-155)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-155)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-155)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-160)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-160)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-165)
pendown()
for i in range(5):fd(10)right(11)
# 5
penup()
goto(-70, -165)
seth(-85)
pendown()
for i in range(3):fd(5)left(3)
penup()
goto(70, -165)
seth(-95)
pendown()
for i in range(3):fd(5)right(3)
seth(-170)
penup()
fd(10)
pendown()
pendown()
for i in range(10):fd(12)right(2)
# 6
penup()
goto(70, -165)
pendown()
seth(-90)
pensize(8)
pencolor("#de8891")
circle(-20, 90)penup()
goto(30, -185)
pendown()
seth(-180)
pensize(8)
pencolor("#de8891")
fd(40)penup()
goto(-5, -170)
pendown()
seth(-180)
pensize(8)
pencolor("#de8891")
fd(35)def guest(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(10)right(10)def guet(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(10)left(10)def qu(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(6)right(10)seth(-150)fd(20)# 树枝
guest(-70, -150, 160)
guest(100, -150, 160)
guet(110, -110, 50)
guest(160, -140, 150)
qu(80, -120, 180)
guest(70, -85, 165)
guest(-40, -85, 165)
guet(90, -50, 50)
guest(130, -80, 150)
pencolor("pink")
qu(-40, -60, 180)
pencolor('#de8891')
qu(80, -30, 180)
pencolor("pink")
qu(40, 10, 180)
pencolor("#de8891")
guest(-60, 30, 120)
guest(-20, -20, 150)
guet(45, 40, 60)
guest(-30, 40, 170)
guest(-30, 110, 115)
guet(40, 90, 60)
guest(80, 50, 160)
pencolor("#de8891")def hdj(x, y):penup()goto(x, y)seth(80)pendown()pensize(2)circle(5)seth(10)fd(15)seth(120)fd(20)seth(240)fd(20)seth(180)fd(20)seth(-60)fd(20)seth(50)fd(20)seth(-40)fd(30)seth(-130)fd(5)seth(135)fd(30)seth(-60)fd(30)seth(-150)fd(6)seth(110)fd(30)def uit(x, y):penup()goto(x, y)pendown()pensize(2)circle(5)seth(-10)fd(15)seth(90)fd(15)seth(200)fd(15)seth(160)fd(15)seth(-90)fd(15)seth(10)fd(15)seth(-60)fd(20)seth(-180)fd(5)seth(110)fd(20)seth(-90)fd(20)seth(-180)fd(6)seth(70)fd(15)hideturtle()def yut(x, y, z):penup()goto(x, y)pendown()seth(z)for po in range(5):fd(4)left(36)def ytu(x, y, z):penup()goto(x, y)pendown()seth(z)for kk in range(5):fd(4)left(36)# 小蝴蝶结
seth(0)
uit(40, -160)
hdj(-80, -120)
yut(-67, -115, 120)
yut(-86, -123, 150)
hdj(40, -50)
yut(52, -45, 130)
yut(34, -55, 160)
seth(0)
uit(-20, -60)
ytu(-4, -60, 100)
ytu(-20, -60, 120)
hdj(-30, 20)
yut(-15, 25, 130)
yut(-40, 20, 180)
uit(30, 70)
ytu(45, 70, 100)
ytu(30, 70, 120)# 大蝴蝶结
pencolor("#f799e6")
pensize(5)
penup()
seth(0)
goto(0, 150)
pendown()
circle(10)
seth(-15)
fd(40)
seth(90)
fd(40)
seth(200)
fd(40)
seth(160)
fd(40)
seth(-90)
fd(40)
seth(15)
fd(40)
seth(-70)
pencolor("#f799e6")
pensize(4)
fd(40)
seth(-180)
fd(10)
seth(100)
fd(40)
seth(-100)
fd(40)
seth(-180)
fd(10)
seth(70)
fd(40)
penup()
seth(0)
goto(0, 130)
pencolor("pink")
pendown()def iou(x, y, z):penup()goto(x, y)pencolor("#f799e6")pendown()seth(z)for po in range(10):fd(4)left(18)seth(0)
iou(35, 145, 100)
iou(-7, 145, 110)
pencolor("red")
pensize(7)
penup()
goto(-35, 135)
pendown()# 圣诞帽
seth(-20)
pensize(2)
penup()
goto(-30, -120)
pencolor("black")
pendown()
fillcolor("red")
fd(30)
circle(4, 180)
fd(30)
circle(4, 180)
penup()
goto(-25, -115)
seth(75)
pendown()
begin_fill()
for i in range(5):fd(6)right(20)
seth(-10)
for i in range(5):fd(8)right(15)
seth(145)
for i in range(5):fd(5)left(2)
seth(90)
for i in range(5):fd(1)left(2)
seth(-90)
for i in range(4):fd(4)right(6)
seth(161)
fd(30)
end_fill()
pensize(1)
pencolor("black")def koc(x, y, size):pensize(2)pencolor("black")penup()goto(x, y)pendown()begin_fill()fillcolor("yellow")for i in range(5):left(72)fd(size)right(144)fd(size)end_fill()# 星星
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("我可爱成这样,做你圣诞礼物好不好", align="right", font=("方正黄草简体", 24, "bold"))
done()