首页 > 其他 > 详细

条件、循环、函数定义 练习

时间:2017-09-13 21:07:41      阅读:298      评论:0      收藏:0      [点我收藏+]

注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。

对前面的代码进行优化,用for,while,if,def实现:

  1. 画五角星 
    1. from turtle import *
      color(green)
      while True:
          forward(233)
          left(144)
          if abs(pos())<1:
              break

      技术分享

  2. 画同心圆
    1. from turtle import *
      for i in range(5):
          up()
          goto(0,-20*i)
          down()
          circle(20*i)

      技术分享

  3. 画太阳花
    1. from turtle import *
      color(gold,yellow)
      begin_fill()
      while True:
          forward(300)
          left(200)
          if abs(pos())<1:
              break
      end_fill()
      done()
      技术分享
  4. 画五个五角星
    1. import turtle
      turtle.setup(288,192,0,0)
      turtle.color(yellow)
      turtle.fillcolor(yellow)
      turtle.bgcolor(red)
      def hpk_goto(x,y):
          turtle.up()
          turtle.goto(x,y)
          turtle.down()
      def hpk_draw(r):
          turtle.begin_fill()
          for i in range(5):
              turtle.forward(r)
              turtle.right(144)
          turtle.end_fill()
      hpk_goto(-124.8,57.6)
      hpk_draw(57.6)
      
      hpk_goto(-57.6,76.8)
      hpk_draw(19.2)
      
      hpk_goto(-38.4,57.6)
      hpk_draw(19.2)
      
      hpk_goto(-38.4,28.8)
      hpk_draw(19.2)
      
      hpk_goto(-57.6,9.6)
      hpk_draw(19.2)

      技术分享

    2. 用函数定义画钻石花瓣的太阳花

      1. import turtle
        
        def draw_1(hpk):
            hpk.forward(100)
            hpk.right(45)
            hpk.forward(100)
            hpk.right(135)
        def draw_2():
            
            window=turtle.Screen()
            window.bgcolor(blue)
            hpk=turtle.Turtle()
            hpk.shape(turtle)
            hpk.color(orange)
            hpk.speed(fastest)
        
            for i in range(1,37):
                draw_1(hpk)
                draw_1(hpk)
                hpk.left(10)
                
            hpk.right(90)
            hpk.forward(155)
            hpk.color(green)
            hpk.forward(145)
            window.exitonclick()
        draw_2()
            

        技术分享

条件、循环、函数定义 练习

原文:http://www.cnblogs.com/kang8823/p/7513488.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!