码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-13 18:30:09      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:方式   优化   break   hit   forward   es2017   定义   war   col   

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

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

a.画五角星

 

import turtle
for i in range(5):
    turtle.forward(100)
    turtle.right(144)
turtle.hideturtle()

技术分享

 

b.画同心圆

import turtle
turtle.speed(10)
for i in range(6):
    turtle.color(purple)
    turtle.up()
    turtle.goto(0,-20*(i+1))
    turtle.down()
    turtle.circle(20*(i+1))
turtle.hideturtle()

技术分享

 c.画太阳花

import turtle

turtle.color(red,yellow)
turtle.speed(10)
turtle.begin_fill()
while True:
    turtle.forward(200)
    turtle.left(170)
    if(abs(turtle.pos()))<1:
        break
turtle.end_fill()
turtle.hideturtle()
done()

技术分享

 

d.画五个角星

法1:

import turtle
turtle.setup(600,400,0,0)
turtle.color(yellow)
turtle.bgcolor(red)
turtle.fillcolor(yellow)
turtle.speed(10)

def jzx_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

def jzx_draw5(r):
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(r)
        turtle.right(144)
    turtle.end_fill()

jzx_goto(-250,75)
jzx_draw5(100)

jzx_goto(-120,150)
jzx_draw5(50)

jzx_goto(-80,100)
jzx_draw5(50)

jzx_goto(-90,30)
jzx_draw5(50)

jzx_goto(-140,0)
jzx_draw5(50)

turtle.hideturtle()
turtle.done()

 法2:

import turtle
turtle.setup(600,400,0,0)
turtle.color(yellow)
turtle.bgcolor(red)
turtle.fillcolor(yellow)
turtle.speed(10)

def jzx_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

jzx_goto(-250,75)
turtle.begin_fill()
for i in range(5):
    turtle.forward(100)
    turtle.right(144)
turtle.end_fill()

turtle.up()
turtle.goto(-120,150)
turtle.down()

turtle.begin_fill()
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()

jzx_goto(-80,100)
turtle.begin_fill()
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()

jzx_goto(-90,30)
turtle.begin_fill()
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()

jzx_goto(-140,0)
turtle.begin_fill()
for i in range(5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()
turtle.hideturtle()
turtle.done()

 

技术分享

e.画◇花瓣的太阳花。

 

import turtle
turtle.fillcolor(yellow)
turtle.bgcolor(white)
turtle.speed(10)
turtle.begin_fill()
for i in range(1,37):
    turtle.forward(100)
    turtle.right(45)
    turtle.forward(100)
    turtle.right(135)
    turtle.forward(100)
    turtle.right(45)
    turtle.forward(100)
    turtle.right(135)
    turtle.left(10)
turtle.end_fill()
turtle.begin_fill()
turtle.right(90)
turtle.forward(300)
turtle.hideturtle()
turtle.end_fill()

技术分享

 

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

标签:方式   优化   break   hit   forward   es2017   定义   war   col   

原文地址:http://www.cnblogs.com/jzx-089/p/7515511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!