注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
对前面的代码进行优化,用for,while,if,def实现:
- 画五角星
-
from turtle import * color('green') while True: forward(233) left(144) if abs(pos())<1: break
-
- 画同心圆
-
from turtle import * for i in range(5): up() goto(0,-20*i) down() circle(20*i)
-
- 画太阳花
-
from turtle import * color('gold','yellow') begin_fill() while True: forward(300) left(200) if abs(pos())<1: break end_fill() done()
-
- 画五个五角星
-
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)
-
用函数定义画钻石花瓣的太阳花
-
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()
-
-