diff --git a/.gitignore b/.gitignore index e69de29..5c9faf9 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +*.bak +__pycache__ diff --git a/README.md b/README.md index 5c8c07a..2d4c430 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,24 @@ -# terminal-rain.py +# terminal-rain -Rain in the linux terminal - written in python3. \ No newline at end of file +![](https://gitlab.com/W13R/terminal-rain/-/raw/master/terminal-rain-showcase.webp) + +Rain in the linux terminal - written in python3. +The terminal emulator has to support ANSI escape codes. + +## Usage + +Run the .py script files with python3: + +``` +python3 rain.py +python3 colorful-rain.py +python3 colorful-rain.py b +``` + +Alternatively, on most linux distros, you can run the scripts with: + +``` +./.py +``` + +Tip: Use a GPU-accelerated terminal emulator like [alacritty](https://github.com/alacritty/alacritty) for better performance. diff --git a/colorful-rain.py b/colorful-rain.py new file mode 100644 index 0000000..f01a5bc --- /dev/null +++ b/colorful-rain.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +''' +terminal-rain - colorful-rain.py +Copyright (c) 2021 Julian Müller (W13R) +License: MIT +''' + +import os, random, sys, time + +if len(sys.argv) > 1 and sys.argv[1] == "b": + colorpalette = [ + "\u001b[38;5;198m", + "\u001b[38;5;198m", + "\u001b[38;5;99m", + "\u001b[38;5;33m", + "\u001b[38;5;33m" + ] +else: + colorpalette = [ + "\u001b[38;5;196m", + "\u001b[38;5;202m", + "\u001b[38;5;220m", + "\u001b[38;5;82m", + "\u001b[38;5;39m", + "\u001b[38;5;129m" + ] + +resetcolor = "\u001b[0m" + +class Raindrop(): + def __init__(self, posX, color): + self.posX = posX + self.posY = 0 + self.symbol = "|" + self.color = color + +# array for the raindrops +raindrops = [] + +def generate_new_raindrops(n): + # generate n new raindrops per frame + for _ in range(n): + raindrops.append(Raindrop(random.randint(1, 1000) % (width + 1), colorpalette[random.randint(0, len(colorpalette) - 1)])) + +def remove_old_raindrops(x, n): + # when there are more than x raindrops, delete the last n + if len(raindrops) > x: + for _ in range(n): + raindrops.pop(0) + +def calculate_raindrop_locations(): + # calculate the new position for every raindrop + for elem_ in raindrops: + if elem_.posY <= height - 1: + elem_.posY += 1 + else: + elem_.symbol = "_" + +def write(): + # clear the screen + sys.stdout.write("\u001b[2J") + # write the raindrops to the terminal + for elem_ in raindrops: + sys.stdout.write(f"\u001b[{elem_.posY};{elem_.posX}f") + sys.stdout.write(elem_.color) + sys.stdout.write(elem_.symbol) + sys.stdout.write(resetcolor) + sys.stdout.write(f"\u001b[H") + # flush the changes to the terminal + sys.stdout.flush() + + +while True: + width, height = os.get_terminal_size() + generate_new_raindrops(3) + remove_old_raindrops(230, 3) + calculate_raindrop_locations() + write() + time.sleep(0.032) diff --git a/rain.py b/rain.py new file mode 100644 index 0000000..00d2486 --- /dev/null +++ b/rain.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +''' +terminal-rain - rain.py +Copyright (c) 2021 Julian Müller (W13R) +License: MIT +''' + +import os, random, sys, time + +class Raindrop(): + def __init__(self, posX): + self.posX = posX + self.posY = 0 + self.symbol = "|" + + +# array for the raindrops +raindrops = [] + +def generate_new_raindrops(n): + # generate n new raindrops per frame + for _ in range(n): + raindrops.append(Raindrop(random.randint(1, 1000) % (width + 1))) + +def remove_old_raindrops(x, n): + # when there are more than x raindrops, delete the last n + if len(raindrops) > x: + for _ in range(n): + raindrops.pop(0) + +def calculate_raindrop_locations(): + # calculate the new position for every raindrop + for elem_ in raindrops: + if elem_.posY <= height - 1: + elem_.posY += 1 + else: + elem_.symbol = "_" + +def write(): + # clear the screen + sys.stdout.write("\u001b[2J") + # write the raindrops to the terminal + for elem_ in raindrops: + sys.stdout.write(f"\u001b[{elem_.posY};{elem_.posX}f") + sys.stdout.write(elem_.symbol) + sys.stdout.write(f"\u001b[H") + # flush the changes to the terminal + sys.stdout.flush() + + +while True: + width, height = os.get_terminal_size() + generate_new_raindrops(3) + remove_old_raindrops(230, 3) + calculate_raindrop_locations() + write() + time.sleep(0.032) diff --git a/terminal-rain-showcase.webp b/terminal-rain-showcase.webp new file mode 100644 index 0000000..d72e1d5 Binary files /dev/null and b/terminal-rain-showcase.webp differ