public
Last active

Make your terminal snow with python

  • Download Gist
snowjob.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#!/usr/bin/env python
import os
import random
import time
import platform
 
snowflakes = {}
 
try:
# Windows Support
from colorama import init
init()
except ImportError:
pass
 
def get_terminal_size():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return None
return cr
 
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
 
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
cr = (25, 80)
return int(cr[1]), int(cr[0])
 
columns, rows = get_terminal_size()
 
def clear_screen(numlines=100):
"""Clear the console.
 
numlines is an optional argument used only as a fall-back.
"""
if os.name == "posix":
# Unix/Linux/MacOS/BSD/etc
os.system('clear')
elif os.name in ("nt", "dos", "ce"):
# DOS/Windows
os.system('cls')
else:
# Fallback for other operating systems.
print('\n' * rows)
 
def get_random_flake():
if not platform.system() == 'Windows':
try:
# python3 support
try:
cmd = unichr
except NameError:
cmd = chr
 
flake = cmd(random.choice(range(0x2740, 0x2749)))
 
return flake
except:
pass
 
return " *"
 
def move_flake(col):
if snowflakes[col][0]+1 == rows:
snowflakes[col] = [1, get_random_flake()]
else:
print("\033[%s;%sH " % (snowflakes[col][0], col))
 
snowflakes[col][0] += 1
 
print("\033[%s;%sH%s" % (snowflakes[col][0], col, snowflakes[col][1]))
 
print("\033[1;1H")
 
if __name__ == "__main__":
 
clear_screen()
 
while True:
col = random.choice(range(1, int(columns)))
 
# its already on the screen, move it
if col in snowflakes.keys():
move_flake(col)
else:
# otherwise put it on the screen
flake = get_random_flake()
snowflakes[col] = [1, flake]
 
print("\033[%s;%sH%s" % (snowflakes[col][0], col,
snowflakes[col][1]))
 
# key any flakes on the screen moving
for flake in snowflakes.keys():
move_flake(flake)
 
time.sleep(0.1)

This latest version gives you random snowflakes, if your terminal doesn't support the characters drop to the old version that uses * or modify get_random_flake to always return *

It now supports python3 and windows (hopefully)

This is neat :) I forked it and added varying speeds to the snow flakes to give it a pseudo-parallax effect.

Awesome, yours looks really nice

Wasn't working for me at first, kept getting stuff like:

Traceback (most recent call last):
File "snowjob.py", line 107, in
snowflakes[col][1]))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2747' in position 7: ordinal not in range(128)

I forced 80x25 screen size on SL4A, replaced get_random_flake() with

def get_random_flake():
    return "*"

and this runs on Android. Thank you!

Very good and very nice looking. I couldn't get it to run without problems on the windows console (Python 2.7) but on Cygwin it works like a charm.

Merry Christmas!

Works like a charm py3k @ win64. :)

Doesn't work in either py3.2.1 or py 2.7 in Windows for me. I've tried a number of character sets, all I get is output like this:

[5;69H
[6;69H *
[1;1H
[14;71H
[15;71H *
[1;1H

It IS animated, though.... :)

That's neat man, I love it.

@tensigh you need to install colorama for it to work in windows

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.