繪制櫻花的代碼:
import turtle
# 設置畫布大小
turtle.setup(800, 600)
# 畫櫻花的枝干
def draw_branch(branch_len):
if branch_len > 3:
# 設置畫筆粗細和顏色
turtle.pensize(branch_len / 3)
turtle.pencolor('brown')
# 畫右邊的枝干
turtle.forward(branch_len)
turtle.right(20)
draw_branch(branch_len - 15)
# 畫左邊的枝干
turtle.left(40)
draw_branch(branch_len - 15)
# 返回樹干處
turtle.right(20)
turtle.backward(branch_len)
# 畫櫻花的花瓣
def draw_petal():
turtle.pensize(2)
turtle.pencolor('pink')
turtle.fillcolor('pink')
# 開始填充
turtle.begin_fill()
turtle.left(60)
turtle.forward(30)
turtle.right(60)
turtle.circle(60, 120)
turtle.right(60)
turtle.forward(30)
turtle.end_fill()
# 移動畫筆到指定位置
def move_pen(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
# 主函數
def main():
# 隱藏畫筆
turtle.hideturtle()
# 設置速度和角度
turtle.speed(5)
turtle.left(90)
# 畫櫻花的枝干
move_pen(0, -200)
draw_branch(100)
# 畫櫻花的花瓣
move_pen(0, 0)
for _ in range(6):
draw_petal()
turtle.left(60)
# 關閉畫布
turtle.done()
if __name__ == '__main__':
main()
繪制玫瑰的代碼:
import turtle
# 設置畫布大小
turtle.setup(800, 600)
# 畫玫瑰
def draw_rose():
turtle.speed(10)
turtle.pensize(2)
turtle.pencolor('red')
# 畫玫瑰的花瓣
for _ in range(18):
turtle.forward(100)
turtle.left(100)
turtle.forward(100)
turtle.right(160)
# 畫玫瑰的花蕊
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
# 移動畫筆到指定位置
def move_pen(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
# 主函數
def main():
# 隱藏畫筆
turtle.hideturtle()
# 畫玫瑰
move_pen(0, 0)
draw_rose()
# 關閉畫布
turtle.done()
if __name__ == '__main__':
main()
繪制圣誕樹的代碼:
import turtle
# 設置畫布大小
turtle.setup(800, 600)
# 畫圣誕樹
def draw_tree():
turtle.speed(10)
turtle.pensize(3)
turtle.pencolor('green')
# 畫樹干
turtle.fillcolor('brown')
turtle.begin_fill()
turtle.left(90)
turtle.forward(100)
turtle.right(90)
turtle.backward(10)
turtle.right(90)
turtle.forward(10)
turtle.right(90)
turtle.backward(20)
turtle.right(90)
turtle.forward(10)
turtle.right(90)
turtle.backward(10)
turtle.right(90)
turtle.forward(100)
turtle.end_fill()
# 畫樹葉
turtle.fillcolor('green')
turtle.begin_fill()
turtle.left(180)
turtle.circle(100, -180)
turtle.right(180)
turtle.forward(100)
turtle.right(60)
turtle.circle(80, -180)
turtle.right(180)
turtle.forward(80)
turtle.right(120)
turtle.circle(80, -180)
turtle.right(180)
turtle.forward(80)
turtle.right(60)
turtle.circle(100, -180)
turtle.end_fill()
# 移動畫