turtle

使用命令行提示符的部分小技巧:
命令行修改
1、直接在命令行改:cd + 文件夹
2、在文件夹下面用:shift + 右键,选择在此处打开命令行窗口
查找代码含义:
import + turtle
help(turtle)
查找代码的用法
Turtle. + 制表键

turtle画图

代码 功能
forward() 画直线
right() 向右转换方向
left() 向左转换方向
circle() 画圆
up() 抬笔
down() 落笔
mainloop() 显示画图窗口
goto() 移动画笔

练习

画五星红旗

import turtle

def start_paint(turtle, l):
    turtle.begin_fill()
    turtle.fillcolor('yellow')
    turtle.pencolor('yellow')
    turtle.forward(l)
    turtle.left(36)
    turtle.forward(l)
    turtle.right(108)
    turtle.forward(l)
    turtle.left(36)
    turtle.forward(l)
    turtle.right(108)
    turtle.forward(l)
    turtle.left(36)
    turtle.forward(l)
    turtle.right(108)
    turtle.forward(l)
    turtle.left(36)
    turtle.forward(l)
    turtle.right(108)
    turtle.forward(l)
    turtle.left(36)
    turtle.forward(l)
    turtle.end_fill()
    turtle.hideturtle()

def main():
    width,height = 600, 400
    #初始化屏幕和海龟
    window = turtle.Screen()
    tr = turtle.Turtle()
    tr.hideturtle()
    tr.speed(10)
    #画红旗
    tr.penup()
    tr.goto(-width/2, height/2)
    tr.pendown()
    tr.begin_fill()
    tr.fillcolor('red')
    tr.fd(width)
    tr.right(90)
    tr.fd(height)
    tr.right(90)
    tr.fd(width)
    tr.right(90)
    tr.fd(height)
    tr.right(90)
    tr.end_fill()
    #画大星星
    tr.penup()
    tr.goto(-width/3,height/4)
    tr.pendown()
    start_paint(tr, 20)
    #画小星星
    localtion = [(-width/4.8,height/3.5),(-width/5.8,height/4),(-width/5.5,height/5),(-width/4.8,height/8.5)]
    for local in localtion:
        tr.penup()
        tr.goto(local)
        tr.pendown()
        start_paint(tr, 5)

main()

画太阳花

import turtle as tr

tr.pencolor('red')
tr.begin_fill()
tr.fillcolor('yellow')
while True:
    tr.forward(300)
    tr.left(170)
    if abs(tr.pos()) < 1:# 画笔是否回到原点
        break
tr.end_fill()
tr.hidetr()
tr.mainloop()

画几何图形

import turtle as tr

tr.pencolor('red')
count = 1
tr.left(20)
lenght = 20
while count < 24:
    if count % 6 == 0:
        lenght += 10
    tr.forward(lenght)
    for _ in range(5):
        tr.right(60)
        tr.forward(lenght)
    count += 1
tr.hideturtle()
tr.mainloop()

你可能感兴趣的:(turtle)