要使用Python畫櫻花樹,可以按照以下步驟進行:
import turtle
import random
window = turtle.Screen()
window.bgcolor("black")
window.title("櫻花樹")
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.up()
pen.left(90)
pen.color("white")
pen.pensize(3)
def draw_branch(branch_len):
if branch_len < 10:
pen.color("pink")
else:
pen.color("brown")
if branch_len < 30:
pen.color("pink")
if branch_len > 5:
angle = random.randint(22, 30)
length = random.randint(10, 15)
pen.pensize(branch_len / 10)
pen.down()
pen.forward(branch_len)
pen.left(angle)
draw_branch(branch_len - length)
pen.right(angle * 2)
draw_branch(branch_len - length)
pen.left(angle)
pen.up()
pen.backward(branch_len)
pen.goto(0, -200)
pen.setheading(90)
pen.down()
pen.color("brown")
pen.forward(60)
draw_branch(60)
window.mainloop()
這樣就可以使用Python繪制櫻花樹了。你可以根據自己的需求調整繪制效果,比如顏色、分支長度等。