使用Python的turtle库绘制花

使用Python的turtle库绘制花

首页角色扮演代号:花更新时间:2024-06-08

以下是使用Python的turtle库绘制花的示例代码:

import turtle

# 设置画布和画笔

canvas = turtle.Screen()

canvas.bgcolor("white")

pen = turtle.Turtle()

pen.speed(0)

pen.width(2)

# 绘制花朵

def draw_flower():

pen.color("red")

pen.begin_fill()

for _ in range(36):

pen.forward(100)

pen.left(170)

pen.end_fill()

# 绘制花瓣

def draw_petals():

pen.color("yellow")

pen.begin_fill()

for _ in range(36):

pen.forward(50)

pen.left(170)

pen.end_fill()

# 绘制叶子

def draw_leaves():

pen.color("green")

pen.begin_fill()

for _ in range(3):

pen.forward(50)

pen.left(120)

pen.end_fill()

# 绘制整朵花

def draw_flower_with_leaves():

draw_flower()

pen.up()

pen.goto(0, -100)

pen.down()

draw_petals()

pen.up()

pen.goto(-50, -150)

pen.down()

draw_leaves()

pen.up()

pen.goto(50, -150)

pen.down()

draw_leaves()

# 开始绘制

draw_flower_with_leaves()

# 隐藏画笔并保持窗口打开

pen.hideturtle()

turtle.done()

运行以上代码,将会在屏幕上绘制出一朵带有红色、黄色和绿色花瓣的花。您可以根据需要调整代码中的参数来改变花朵的大小和形状。

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved