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

yipeiwu_com7年前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编程实现希尔排序

python编程实现希尔排序

观察一下”插入排序“:其实不难发现她有个缺点:   如果当数据是”5, 4, 3, 2, 1“的时候,此时我们将“无序块”中的记录插入到“有序块”时,估计俺们要崩盘,每次插入都要移动位置...

Python 调用DLL操作抄表机

# -*- coding: GBK -*- from ctypes import * dll = windll.LoadLibrary('JBA188.dll') a = dll.tes...

python模块restful使用方法实例

python模块restful使用方法实例

RESTful架构,目前是比较流行的一种互联网软件架构。REST,即Representational State Transfer的缩写。 说白点就是网站即软件,再白点就是一个服务软件支...

pytorch实现onehot编码转为普通label标签

label转onehot的很多,但是onehot转label的有点难找,所以就只能自己实现以下,用的topk函数,不知道有没有更好的实现 one_hot = torch.tensor...

python在命令行下使用google翻译(带语音)

说明1. 使用google翻译服务获得翻译和语音;2. 使用mplayer播放获得的声音文件,因此,如果要播放语音,请确保PATH中能够找到mplayer程序,如果没有mplayer,请...