Advertisement
Kabbalah

Christmas

Dec 24th, 2018
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. """ christmas.py
  2. Prints a christmas tree on the terminal using coloured and blinking characters.
  3. Uses ansi terminal escape sequences.
  4. The '\033[' part is the escape code.
  5. We pass '5;' for the colours other than green to make them blink.
  6. The next part is the colour code and the 'm' ends the sequence.
  7. To reset the colour we pass "\033[0m" after each character.
  8. Python 3 version by antiloquax (2015), based on code from datamungeblog.com.
  9. """
  10.  
  11. from random import choice
  12. from random import random
  13.  
  14.  
  15. def main():
  16.     """Make the tree and print it."""
  17.     # If you change this, use an odd number.
  18.     SIZE = 21
  19.     print(makeTree(SIZE))
  20.  
  21.  
  22. def makeTree(size):
  23.     """Creates the tree string."""
  24.     # Probability that a character will be green.
  25.     prob_gr = 0.6
  26.     # Colour codes.
  27.     colours = [31, 33, 34, 35, 36, 37]
  28.     # Characters to use for decorations. Experiment with these.
  29.     # The chr(169) and chr(174) characters may not work in all terminals
  30.     # (extended ASCII, c and r in a circle).
  31.     decs = ['@', '&', '*', chr(169), chr(174)]
  32.  
  33.     # Format string for printing blinking characters.
  34.     blink_col = "\033[5;{0}m{1}\033[0m"
  35.     # String to print a green octothorpe ('#').
  36.     leaf = "\033[32m#\033[0m"
  37.  
  38.     # Width of the tree, will grow by 2 each time.
  39.     width = 1
  40.     # Initialise the tree string, with a star at the top.
  41.     tree = "\n{}*\n".format(' ' * (size))
  42.  
  43.     """ Main Loop starts now."""
  44.  
  45.     """ We can't use the normal "format" centering approach:
  46.        ("{:^nn}".format(string) where "nn" is the width of the line),
  47.        with these ansi codes. This is because Python sees the strings as being
  48.        more than one character long (15 & 10 for baubles and leaves)."""
  49.  
  50.     # Loop from (size - 1) down to 0, using the counter as the padding size.
  51.     for pad in range(size - 1, -1, -1):
  52.         # Increase the width of the tree by 2.
  53.         width += 2
  54.  
  55.         # Put the characters for the line in "temp".
  56.         temp = ""
  57.         for j in range(width):
  58.             # Make some leaves.
  59.             if random() < prob_gr:
  60.                 temp += leaf
  61.             # And also some baubles.
  62.             else:
  63.                 temp += blink_col.format(choice(colours), choice(decs))
  64.  
  65.         # Add that string to the line, with padding.
  66.         tree += "{0}{1}\n".format(' ' * pad, temp)
  67.  
  68.     # Add a "trunk" of 2 lines and return.
  69.     return tree + "{0}{1}\n".format(' ' * (size - 1), "000") * 2
  70.  
  71.  
  72. if __name__ == "__main__":
  73.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement