详解python使用turtle库来画一朵花

yipeiwu_com5年前Python基础

看了群主最后成像的图片,应该是循环了36次画方框,每次有10度的偏移。

当然不能提前看答案,自己试着写代码。

之前有用过海龟画图来画过五角星、奥运五环、围棋盘等,所以感觉不难。

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:wxh
 
def run():
  '''
  主方法
  :return: None
  '''
  import turtle
  length = 150 # 线段长度
  angle = 45  # 角度
  offset_angle = 10 # 每次偏移的角度
  turtle.screensize(800, 800)
  turtle.bgcolor('blue')
 
  def draw():
    '循环画方框'
    turtle.forward(length)
    turtle.right(angle)
    turtle.forward(length)
    turtle.right(180 - angle)
    turtle.forward(length)
    turtle.right(angle)
    turtle.forward(length)
    turtle.right(180 - angle)
    turtle.right(offset_angle)
 
  turtle.penup()
  turtle.goto(0, -400)
  turtle.left(90)
  turtle.pendown()
  turtle.pencolor('gold')
  turtle.forward(length * 3)
  turtle.left(180)
  for i in range(36):
    draw()
 
  turtle.hideturtle()  # 隐藏画笔
  turtle.done()
 
if __name__ == '__main__':
  run()

最后是效果图:

以上所述是小编给大家介绍的python使用turtle库来画一朵花详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

python中update的基本使用方法详解

前言 Python 字典 update()方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中。 语法格式 d.update(e) 参数说明 将...

python为tornado添加recaptcha验证码功能

复制代码 代码如下:    from urllib.request import urlopen    from urllib...

python计算列表内各元素的个数实例

如下所示: list = [1,2,3,4,5,6,7,5,4,3,2,12] set = set(list) dict = {} for item in set: dict.u...

Python拼接微信好友头像大图的实现方法

Python拼接微信好友头像大图的实现方法

基于 itchat 库来获取微信好友头像并执行拼接操作,对微信上文字化好友列表数据进行可视化展示。 获取好友头像 def save_avatar(folder): """ 保...

Python中属性和描述符的正确使用

关于@property装饰器 在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问。 那么为什么要这样做呢?因为@property让我们将自定义的代码同变量...