Christmas tree python script
I made a Python script that displays a Christmas tree with flashing lights in the terminal.
import random
import time
from datetime import date
import os
def clear_screen():
"""Clears the console screen."""
os.system('cls' if os.name == 'nt' else 'clear')
def display_tree():
"""Displays a Christmas tree with animations."""
colors = [33, 34, 31] # ANSI color codes for yellow, blue, and red
color = random.choice(colors) # Randomly select a color
print('\033[32m') # Set text color to green
# Print the top star of the tree
print(" " * 13 + "*")
stars = 3
for i in range(13):
if i <= 8:
spaces = 10 - i + 1
print(" " * spaces, end="")
print(f'\033[{color}m' + "o" + '\033[32m', end="") # Decorate with a colored "o"
print("*" * stars, end="")
print(f'\033[{color}m' + "o")
stars += 2
else:
# Print the tree trunk
print('\033[32m', end="") # Set text color to green
print(" " * 10 + "#######")
# Reset text formatting
print("\033[m")
def display_message():
"""Displays the holiday message."""
year = date.today().year + 1
print(" " * 5 + "MERRY CHRISTMAS")
print(" " * 5 + f"HAPPY NEW YEAR {year}")
print(" " * 5 + "Press CTRL+Z TO EXIT")
def main():
"""Main function to run the animated Christmas tree."""
while True:
clear_screen()
display_tree()
display_message()
time.sleep(0.3) # Pause for 0,3 seconds
if __name__ == "__main__":
main()
#python #python3 #programming #code #christmas #christmas2024 #xmas
Vplog Golpv reshared this.