1. 程式人生 > >turtle模組的簡單運用

turtle模組的簡單運用

turtle()簡單運用:
畫一個平行四邊形並重復偏移

import turtle as t

def drawdemo(tom):
    for i in range(2):
        tom.forward(100)
        tom.right(45)
        tom.forward(100)
        tom.right(135)

def drawflower():
    windows = t.Screen()
    windows.bgcolor('blue')
    tom = t.Turtle()
    tom.shape('turtle')
    tom.color('red')
    tom.speed(0.1)
    # tom.fillcolor('yellow')#圖形顏色填充
    # tom.begin_fill()
    for i in range(360):
        drawdemo(tom)
        tom.right(1)
    # tom.end_fill()
    tom.hideturtle()
    t.done()
drawflower()

效果如下所示:
在這裡插入圖片描述
畫一個愛心:


import turtle
import time
turtle.shape('turtle')

def draw_circle():
    for i in range (200):
        turtle.right(1)
        turtle.forward(1)
def draw_heart():
    turtle.color('red','pink')
    turtle.pensize(5)
    turtle.speed(1000)
    turtle.goto(0,0)
    turtle.begin_fill()
    turtle.left(140)
    turtle.forward(112)
    draw_circle()
    turtle.left(120)
    draw_circle()
    turtle.forward(112)
    turtle.end_fill()
    turtle.hideturtle()
    turtle.done()
draw_heart()

效果如下:
在這裡插入圖片描述
亂畫一個:

import turtle
def demo(t):
    t.forward(100)
    t.left(120)
    t.forward(100)
    t.left(120)
    t.forward(100)


def draw():
    win = turtle.Screen()
    win.bgcolor('yellow')
    t = turtle.Turtle()
    t.shape('turtle')
    t.speed(2)
    t.fillcolor('blue')
    t.begin_fill()
    for i in range(12):
        demo(t)
        t.right(30)
    t.end_fill()
    t.hideturtle()
    turtle.done()
draw()

效果如下:
在這裡插入圖片描述