Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created September 15, 2015 18:26
Show Gist options
  • Save AndyNovo/638461f983bd8f790bad to your computer and use it in GitHub Desktop.
Save AndyNovo/638461f983bd8f790bad to your computer and use it in GitHub Desktop.
ASCII Christmas Tree with Snow
import random, os, sys, time
#Animate some ASCII art (thanks Philip: http://stackoverflow.com/a/2785568/1347629)
def draw_to_screen(content):
clear_console = 'clear' if os.name == 'posix' else 'CLS'
os.system(clear_console)
sys.stdout.write(content)
sys.stdout.flush()
time.sleep(0.2)
#Some randomized snow
def tree_background(n):
result = ""
for i in range(n):
if (random.random() < .05):
result += "*"
else:
result += ' '
return result
#some logic for drawing the tree:
def snow_tree(top, bottom):
tree = ""
width = 2*top + 1
for r in range(top):
row = tree_background(width)
row = row[:top-r] + "^"*(2*r+1) + row[top+r:]
tree = tree + "\n" + row
for b in range(bottom):
tree += "\n" + " "*(top-2) + "#"*5 + " "*(top-2)
return tree+"\n"
#Draw to the screen 50 times
for i in range(50):
draw_to_screen(snow_tree(10, 3))
#One last clear
draw_to_screen("\n\n")
#To run, save into file and from command line python3 tree.py
#Adjust size of tree to fit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment