Let's look at the code.
from random import randrange
import time
Your imports are very minimal! Good.
# Snow animation
# Snow is simply the `#` symbol here. Half of the snow moves at 1 char
# per frame, while the other half moves at 0.5 chars per frame. The
# program ends when all the snow reaches the bottom of the screen.
# The viewing area is 80x25. It puts 100 snow flakes to output, half
# fast, and half slow. Every frame it dispenses 4 flakes, 2 fast and
# 2 slow ones, at random locations at the top of the viewing area.
This looks more like a docstring to me. It would be nice to render it as such. You can do this by dropping the # signs, and surrounding it in """ quotes.
screen = {'x': 80, 'y': 20}
drops = []
Global variables are not that nice. But this is a simple file, so maybe we can leave it like this for now? Let's.
def createRainDrop(x, y, speed):
return {'x': x, 'y': y, 'speed': speed}
I think something like a class would be better for this. Let's try
class RainDrop(object):
def __init__(self, x, y, speed):
self.x = x
self.y = y
self.speed = speed
Of course, now we need to replace createRainDrop(...) with RainDrop(...), and drop['...'] with drop.....
def createRandomDrops():
dropCount = 4
for i in range(dropCount):
yield RainDrop(randrange(0, screen['x']), 0, min((i % 2) + 0.5, 1))
Now, you're creating rainDrops with a floating-point (non-integral) y here. Better to write
yield RainDrop(randrange(0, screen['x']), 0, min(int(i % 2 + 0.5), 1))
That's better.
def moveDrops():
for drop in drops:
drop.y = drop.y + drop.speed
We're modifying drop here, instead of asking it to modify itself. We should be writing something like drop.moveDown() here, or maybe drop.tick() ('tick' is what's commonly used to notify an event about stepping forward in time).
def drawDrops():
out = [''] * screen['y']
for y in range(screen['y']):
for x in range(screen['x']):
out[y] += '#' if any([drop.x == x and drop.y == y for drop in drops]) else ' '
return '\n'.join(out)
Here, for all the positions on the screen, you're looping over all the drops. Ideally we'd turn that around:
def drawDrops():
out = [[' ' for _ in range(screen['x'])] for _ in range(screen['y'])]
for drop in drops:
out[drop.y][drop.x] = '#'
Now that's a bit faster and cleaner.
def dropsOnScreen():
return any([drop.y < screen['y'] for drop in drops])
Makes sense. Except I'd suggest to not use the [...], which creates a list. Better to use
def dropsOnScreen():
return any(drop.y < screen['y'] for drop in drops)
This behaves the same, but does not have to create an intermediate list.
drops += createRandomDrops()
while dropsOnScreen():
if len(drops) < 100:
drops += createRandomDrops()
print(drawDrops())
moveDrops()
time.sleep(0.100)
You want to get rid of the duplicated call to drops += createRandomDrops().
while True:
if len(drops) < 100:
drops += createRandomDrops()
if not dropsOnScreen():
break
print(drawDrops())
moveDrops()
time.sleep(0.100)
But in my opinion, the extra createRandomDrops is not that bad.