- 注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
- 对前面的代码进行优化,用for,while,if,def实现:
- 画五角星
import turtle turtle.begin_fill() for i in range(5): turtle.forward(50) turtle.right(144) turtle.end_fill()
b.画同心圆
import turtle for i in range(5): turtle.up() turtle.goto(0,-20*(i+1)) turtle.down() turtle.circle(20*(i+1))
c.画太阳花
import turtle turtle.color('green','pink') turtle.begin_fill() while True: turtle.forward(200) turtle.left(170) if(abs(turtle.pos()))<1: break turtle.end_fill() turtle.hideturtle() t
urtle.done()
d.画五个五角星
import turtle turtle.bgcolor('red') turtle.color('yellow') turtle.fillcolor('yellow') def my_goto(x,y): turtle.up() turtle.goto(x,y) turtle.down() def my_draw(r): turtle.begin_fill() for i in range(5): turtle.forward(r) turtle.right(144) turtle.end_fill() my_goto(-350,180) my_draw(150) my_goto(-175,250) turtle.left(50) my_draw(50) my_goto(-100,175) turtle.left(44) my_draw(50) my_goto(-70,80) turtle.left(44) my_draw(50) my_goto(-140,50) turtle.left(50) my_draw(50) turtle.hideturtle() turtle.done()
e.画花瓣的太阳花。
import turtle def my_draw1(brad): brad.forward(100) brad.right(45) brad.forward(100) brad.right(135) def my_draw2(): window=turtle.Screen() window.bgcolor('violet') brad=turtle.Turtle() brad.shape('turtle') brad.color('black') brad.speed('fastest') for i in range(1,37): my_draw1(brad) my_draw1(brad) brad.left(10) brad.right(90) brad.forward(300) window.exitonclick() my_draw2()