以下是一個簡單的Python圣誕樹代碼:
- def draw_tree(height):
- ????for i in range(height):
- ????????print(' ' * (height - i - 1) + '*' * (2 * i + 1))
- ????print(' ' * (height - 1) + '|')
- draw_tree(10)
這個函數會繪制一個等腰三角形,其中每一行的星號數量從1開始,每次遞增2,空格數量則相反,從等腰三角形的高度減1開始,每次遞減1。最后一行輸出一個豎線,表示圣誕樹的樹干。你可以根據需要調整高度來改變圣誕樹的大小。
這段代碼只是繪制了一個基本的圣誕樹形狀,你可以進一步擴展它,讓圣誕樹更加生動和有趣。例如,你可以使用Python的turtle模塊,繪制一個會動的圣誕樹。
以下是一個使用turtle模塊的Python圣誕樹代碼:
- import turtle
- def draw_tree(height):
- ????tree = turtle.Turtle()
- ????tree.penup()
- ????tree.goto(0, -height)
- ????tree.pendown()
- ????tree.color('green')
- ????for i in range(height):
- ????????tree.forward(2)
- ????????tree.right(144)
- ????????tree.forward(4)
- ????????tree.right(144)
- ????????tree.forward(2)
- ????????tree.right(144)
- ????????tree.forward(4)
- ????????tree.right(144)
- ????tree.right(90)
- ????tree.forward(height * 2)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height * 2)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height * 2)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height * 2)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height)
- ????tree.right(90)
- ????tree.forward(height * 2)
當然,我們可以進一步增加代碼的復雜性和視覺效果。例如,我們可以使用Python的Pygame庫來創建一個更加生動和有趣的圣誕樹。
以下是一個使用Pygame庫的Python圣誕樹代碼:
- import pygame
- import math
- # 初始化Pygame
- pygame.init()
- # 設置屏幕大小和標題
- screen = pygame.display.set_mode((800, 600))
- pygame.display.set_caption("Christmas Tree")
- # 設置顏色
- green = (0, 255, 0)
- red = (255, 0, 0)
- brown = (100, 50, 0)
- yellow = (255, 255, 0)
- white = (255, 255, 255)
- black = (0, 0, 0)
- # 繪制圣誕樹函數
- def draw_tree(x, y, height, width):
- ????pygame.draw.line(screen, green, (x, y), (x + width // 2, y + height), 10)
- ????pygame.draw.line(screen, green, (x + width // 2, y + height), (x + width, y), 10)
- ????pygame.draw.line(screen, green, (x + width // 2, y), (x + width // 2, y + height), 10)
- ????pygame.draw.polygon(screen, green, [(x + width // 4, y + height // 4), (x + width * 3 // 4, y + height // 4), (x + width // 2, y + height), (x + width // 4, y + 3 * height // 4)])
- ????pygame.draw.polygon(screen, green, [(x + width // 4, y), (x + width * 3 // 4, y), (x + width // 2, y + height // 2), (x + width // 4, y + height // 4)])
- ????pygame.draw.polygon(screen, green, [(x + width // 4, y + height // 2), (x + width * 3 // 4, y + height // 2), (x + width // 2, y + height), (x + width // 4, y + 3 * height // 4)])
- ????pygame.draw.line(screen, green, (x + width // 4, y + height), (x + width // 4 - height // 4 * math.tan(math.pi / 6), y + height // 2), height // 8 * math.cos(math.pi / 6))
- ????pygame.draw.line(screen, green, (x + width // 4, y + height), (x + width // 4 - height // 8 * math.tan(math.pi / 6), y + height - height // 4), height // 8 * math.cos(math.pi / 6))
- ????pygame.draw.circle(screen, brown, (x + width // 4 - height // 8 * math.tan(math.pi / 6), y + height - height // 4), height // 8)
- ????pygame.draw.circle(screen, brown, (x + width //