Let it snow()

The following script creates an animated snowfall effect in a terminal window. It continuously updates and displays snowflakes falling from the top of the terminal to the bottom. The snowflakes are represented by Unicode characters (feel free to change!) and their movement is simulated by updating their positions in a grid that represents the terminal window.

import time
import random
import sys

def snow():
    # Manually set the terminal size
    h, w = 20, 80  # Adjust these values as needed
    flakes = [" ", " ", "β„οΈŽ", "❅", "❆"]

    # Initialize air space
    air = [[1 for _ in range(h)] for _ in range(w)]

    try:
        while True:
            # Update snowflakes
            for x in range(w):
                for y in range(h - 1, -1, -1):
                    if y == 0:
                        # Generate new snowflake
                        air[x][y] = random.choice(range(1, len(flakes))) if random.random() < 0.1 else 1
                    else:
                        # Move snowflake down
                        air[x][y] = air[x][y - 1]

            # Print snowflakes
            for y in range(h):
                for x in range(w):
                    print(flakes[air[x][y]], end="")
                print()

            # Reset cursor to top
            sys.stdout.write("\033[F" * h)
            time.sleep(0.2)

    except KeyboardInterrupt:
        pass

snow()

It is a Python variation of the code provided for Julia here.

Let’s break down the Python script for a terminal-based snowfall animation in a coherent and structured way.

I use the following three modules:

  1. time: Used for introducing delays in the animation loop.
  2. random: Generates random numbers for the random placement of snowflakes.
  3. sys: Handles terminal operations, like cursor positioning.

The snow() function is the core function where the snowfall animation is implemented.

  • Setting the Terminal Size:
    • h, w = 20, 80: Defines the dimensions of the terminal window for the animation (h for height, w for width).
  • Snowflake Characters:
    • flakes = [" ", " ", "β„οΈŽ", "❅", "❆"]: A list of characters used to display snowflakes and empty space. You could insert any snowflake character here.
  • Initializing the Air Space:
    • air = [[1 for _ in range(h)] for _ in range(w)]: Initializes a 2D grid representing the terminal window. Each cell can either have a snowflake or be empty. I love Python One-Liners. πŸ˜‰

The following loop is repeated forever until you terminate the computation:

  • Continual Update: The while True loop keeps the animation running until manually stopped.
  • Updating Snowflakes: Snowflakes at the top row have a chance to be newly generated. Snowflakes in other rows move down by one position.
  • Displaying Snowflakes: The grid is iterated over to print snowflakes as per their updated positions.
  • Resetting Cursor: Moves the cursor back to the top of the terminal to overwrite the previous frame.
  • Frame Delay: time.sleep(0.2) pauses the loop briefly to control the snowfall speed.

You can stop the animation with a keyboard interrupt (like Ctrl+C).

The call to snow() activates the function and starts the snowfall animation. However, you need to do it from a terminal (Linux) or Powershell/cmd (Windows) by running python your_script.py assuming you’ve stored the code in a file your_script.py. Easy. ❄️

✨ Recommended: How to Make a Beep Sound in Python? [Linux/macOS/Win]