温馨提示×

使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实现

小云
114
2023-09-17 03:10:46
栏目: 编程语言

以下是使用Python的turtle库绘制樱花、玫瑰和圣诞树的代码实现。

樱花:

import turtle
def draw_branch(branch_len):
if branch_len > 3:
if branch_len < 30:
turtle.pencolor("pink")
else:
turtle.pencolor("brown")
turtle.forward(branch_len)
turtle.right(20)
draw_branch(branch_len - 15)
turtle.left(40)
draw_branch(branch_len - 15)
turtle.right(20)
turtle.penup()
turtle.backward(branch_len)
turtle.pendown()
def draw_sakura():
turtle.speed(0)
turtle.penup()
turtle.left(90)
turtle.backward(200)
turtle.pendown()
draw_branch(100)
turtle.exitonclick()
draw_sakura()

玫瑰:

import turtle
def draw_flower():
turtle.color("red")
turtle.begin_fill()
turtle.left(50)
turtle.forward(133)
turtle.circle(50, 180)
turtle.right(140)
turtle.circle(50, 180)
turtle.forward(133)
turtle.end_fill()
def draw_stem():
turtle.color("green")
turtle.right(90)
turtle.forward(200)
def draw_rose():
turtle.penup()
turtle.backward(100)
turtle.pendown()
draw_flower()
draw_stem()
turtle.exitonclick()
draw_rose()

圣诞树:

import turtle
def draw_tree(branch_len):
if branch_len > 5:
turtle.forward(branch_len)
turtle.right(20)
draw_tree(branch_len - 15)
turtle.left(40)
draw_tree(branch_len - 15)
turtle.right(20)
turtle.backward(branch_len)
def draw_star():
turtle.color("yellow")
turtle.begin_fill()
for _ in range(5):
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
def draw_christmas_tree():
turtle.speed(0)
turtle.penup()
turtle.left(90)
turtle.backward(200)
turtle.pendown()
turtle.color("green")
turtle.begin_fill()
turtle.forward(100)
turtle.right(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(100)
turtle.end_fill()
turtle.penup()
turtle.left(90)
turtle.forward(120)
turtle.left(90)
turtle.pendown()
draw_tree(100)
turtle.penup()
turtle.backward(100)
turtle.pendown()
draw_star()
turtle.exitonclick()
draw_christmas_tree()

这些代码可以在Python环境中运行,并使用turtle库来绘制相应的图形。

0