实战 | 教你用Python画各种版本的圣诞树

前言

一年一度的圣诞节马上就要到了,看到朋友圈好多小伙伴已经开始炫耀自己制作的圣诞树了。今天给大家分享通过Pythoy用Turtle库来画圣诞树。(复制即可使用)

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

1.方块圣诞树

import turtle

screen = turtle.Screen()

screen.setup(375, 700)

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, 13):

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 += 3

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(13, 17):

y = 30 * i

for j in range(2):

x = 30 * j

square.goto(x, -y + 280)

square.stamp()

square.goto(-x, -y + 280)

square.stamp()

效果展示:

2.线条圣诞树

import turtle

# 定义圣诞树的绿叶函数

def tree(d, s):

if d <= 0:

return

turtle.forward(s)

tree(d - 1, s * .8)

turtle.right(120)

tree(d - 3, s * .5)

turtle.right(120)

tree(d - 3, s * .5)

turtle.right(120)

turtle.backward(s)

n = 100

""" 设置绘图速度

'fastest' : 0

'fast' : 10

'normal' : 6

'slow' : 3

'slowest' : 1

"""

turtle.speed('fastest') # 设置速度

turtle.left(90)

turtle.forward(3 * n)

turtle.color("orange", "yellow")

turtle.left(126)

# turtle.begin_fill()

for i in range(5):

turtle.forward(n / 5)

turtle.right(144)

turtle.forward(n / 5)

turtle.left(72)

turtle.end_fill()

turtle.right(126)

turtle.color("dark green")

turtle.backward(n * 4.8)

# 执行函数

tree(15, n)

turtle.backward(n / 5)

效果展示:

3.豪华圣诞树

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() # 完成,否则会直接关闭

效果展示:

4.附赠动态圣诞贺卡

代码:

# -*- coding: utf-8 -*-

import turtle as T

import random

import time

# 绘图区域

t = T.Turtle()

# 画布大小

w = T.Screen()

t.hideturtle() # 隐藏画笔

t.getscreen().tracer(5, 0)

w.screensize(bg='maroon')

t.left(90) t.up()

t.forward(280)

t.down()

t.pensize(3)

# 画五角星

n=100

t.color("orange","yellow")

t.begin_fill()

t.left(126)

for i in range(5):

t.forward(n/5)

t.right(144)

t.forward(n/5)

t.left(71)

t.end_fill()

t.left(60)

t.pensize(8)

t.forward(60)

t.right(20)

t.right(116)

t.pensize(6)

# 画树冠

t.color('dark green')

n=130

for i in range(6):

time.sleep(0.5)

a=1+i/2

t.begin_fill()

t.left(90)

t.forward(n*a*0.707)

t.left(135)

t.forward(n*a)

t.left(135)

t.forward(n*a*0.707)

t.end_fill()

t.up()

t.left(90)

t.forward(n*a*0.707/3)

t.left(135)

t.forward(n*a/6)

t.left(135)

t.down()

# 画树干

t.up()

t.right(135)

t.forward(30)

t.right(90)

t.forward(157)

t.down()

t.color('saddlebrown')

t.begin_fill()

t.forward(80)

t.right(90)

t.forward(45)

t.right(90)

t.forward(80)

t.right(90)

t.forward(45)

t.end_fill()

t.up()

t.backward(45)

t.right(90)

t.backward(470)

t.down()

# 画灯

def light(l,t):

t.pensize(3)

colors = ["magenta","darkorange","red","blue"]

for i in range(l):

time.sleep(0.2)

b = 70+16*i

a = b/2*random.randint(-100,100)/100

t.up()

t.forward(b)

t.left(90)

t.forward(a)

t.down()

t.color("lightyellow",colors[i%4])

t.begin_fill()

t.circle(10)

t.end_fill()

t.up()

t.backward(a)

t.right(90)

t.backward(b)

t.down()

t.pensize(1)

# 画雪花

def snow(m,t):

for i in range(m):

a = 400 - 800 * random.random()

b = 600 - 800 * random.random()

t.up()

t.forward(b)

t.left(90)

t.forward(a)

t.down()

t.color('white')

t.begin_fill()

t.circle(1)

t.end_fill()

t.up()

t.backward(a)

t.right(90)

t.backward(b)

light(24,t)

snow(600, t)

# 文字

t.goto(-200,200)

my_word = ("Merry Christmas")

t.write(my_word,font=("Edwardian Script ITC",40,"bold"))

time.sleep(0.3)

t.goto(-100,50)

my_word = ("and")

t.write(my_word,font=("Edwardian Script ITC",50,"bold"))

time.sleep(0.3)

t.goto(-150,-100)

my_word = ("Happy New Year")

t.write(my_word,font=("Edwardian Script ITC",40,"bold"))

time.sleep(0.3)

t.clear()

w.screensize(bg='black')

t.goto(-200,0)

my_word = ("Prudued by: Justine")

t.write(my_word,font=("Edwardian Script ITC",45,"bold"))

t.goto(0,-100)

my_word = ("Dec. 24th, 2019")

t.write(my_word,font=("Edwardian Script ITC",20,"bold"))

time.sleep(5)

效果展示:

图片为动态效果哦~

感兴趣的话就赶紧尝试一下吧!

你可能感兴趣的:(实战 | 教你用Python画各种版本的圣诞树)