圣诞节要到了,不给对象画一颗圣诞树吗?
那就用python画一颗圣诞树吧
?
# -*- codeing = utf-8 -*-
# @Time : 2021/12/14 10:35
# @Author : 1
# @File : 圣诞树.py.py
# @Softestr : PyCharm
import turtle
import time
import random
turtle.screensize(700,600, "thistle3")
turtle.pensize(5) # 画笔的粗度
def draw_star():
turtle.pencolor("yellow")
turtle.fillcolor("red") # 选择颜色
turtle.penup() # 抬起
star_x = 25*random.randint(0,10)
star_y = 10*random.randint(0,10)
turtle.goto(280-star_x, 260-star_y)
turtle.pendown() # 按下
turtle.goto(200-star_x, 220-star_y)
turtle.begin_fill()
# 画星星
for _ in range(5):
turtle.forward(10)
turtle.right(144)
turtle.end_fill()
# time.sleep(1)
def langxuan():
# 定义一个函数
for i in range(200): # 设定范围
turtle.right(1)
turtle.forward(1)
turtle.pensize(3) # 设置画笔粗细
turtle.speed(0) # 设置画笔速度 [0-10]之间
turtle.color('pink', 'pink') # 画笔的颜色, 要填充的颜色
turtle.begin_fill() # 准备开始填充图形
turtle.left(140) # 逆时针移动140度
turtle.forward(112) # 当前画笔方向移动112px
langxuan() # 调用函数
turtle.left(120)
langxuan()
turtle.forward(112)
turtle.end_fill() # 填充完成
turtle.goto(0, 90) # 画笔移动到的位置
turtle.color('violet') # 画笔颜色(同时设置字体颜色)
turtle.write('I Love You宝', font=('Arial', 18, 'normal'), align='center', move=True) # 写入内容
for i in range(10):
draw_star()
turtle.penup()
turtle.goto(60, 80)
turtle.color("violet")
# turtle.write("hyx", font=('Arial', 40, 'normal'))
turtle.goto(0, -10)
turtle.left(140)
# 定义圣诞树的绿叶函数
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)
# 画文字
turtle.penup()
turtle.forward(100)
turtle.write('祝你天天开心丫',font=('Colonna MT',15,'bold'))
turtle.left(90)
turtle.forward(20)
turtle.write('给陈佳希画颗圣诞树',font=('Colonna MT',15,'bold'))
turtle.right(90)
turtle.left(180)
turtle.forward(400)
turtle.write('祝你圣诞快乐',font=('Colonna MT',20,'bold'))
turtle.left(180)
turtle.forward(300)
turtle.right(90)
turtle.forward(40)
turtle.left(90)
turtle.pendown()
n = 90
""" 设置绘图速度
'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)
for i in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
turtle.up()
turtle.forward(b)
turtle.left(90)
turtle.forward(a)
turtle.down()
if random.randint(0, 1) == 0:
turtle.color('tomato')
else:
turtle.color('wheat')
turtle.circle(2)
turtle.up()
turtle.backward(a)
turtle.right(90)
turtle.backward(b)
turtle.mainloop()
?
上效果图
?大家通过其,用自己创意修该,给对象一个惊喜吧!
|