使用Python繪制圣誕樹可以使用turtle庫來實現,下面是一個簡單的繪制圣誕樹的示例代碼:
import turtle
def draw_triangle(x, y, size, color):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for _ in range(3):
turtle.forward(size)
turtle.right(120)
turtle.end_fill()
def draw_tree(x, y, size):
draw_triangle(x, y, size, "green")
draw_triangle(x - size/2, y + size, size, "green")
draw_triangle(x + size/2, y + size, size, "green")
draw_triangle(x - size, y + size*2, size, "green")
draw_triangle(x + size, y + size*2, size, "green")
draw_triangle(x - size*1.5, y + size*3, size, "green")
draw_triangle(x + size*1.5, y + size*3, size, "green")
draw_triangle(x - size*2, y + size*4, size, "green")
draw_triangle(x + size*2, y + size*4, size, "green")
def draw_star(x, y, size):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor("yellow")
turtle.begin_fill()
for _ in range(5):
turtle.forward(size)
turtle.right(144)
turtle.end_fill()
def main():
turtle.speed(0)
turtle.bgcolor("black")
draw_tree(0, -200, 50)
draw_star(0, 100, 20)
turtle.hideturtle()
turtle.done()
if __name__ == "__main__":
main()
運行該代碼后,將會在窗口中顯示一個簡單的圣誕樹。您可以根據需要調整樹的大小和位置。