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

74次阅读
没有评论

共计 1873 个字符,预计需要花费 5 分钟才能阅读完成。

以下是使用 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 库来绘制相应的图形。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-22发表,共计1873字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)