add files & write README

This commit is contained in:
W13R 2021-06-11 20:57:37 +02:00
parent 0d1bb7d726
commit 4070989a9c
5 changed files with 163 additions and 2 deletions

2
.gitignore vendored
View file

@ -0,0 +1,2 @@
*.bak
__pycache__

View file

@ -1,3 +1,24 @@
# terminal-rain.py
# terminal-rain
![](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:
```
./<scriptname>.py
```
Tip: Use a GPU-accelerated terminal emulator like [alacritty](https://github.com/alacritty/alacritty) for better performance.

80
colorful-rain.py Normal file
View file

@ -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)

58
rain.py Normal file
View file

@ -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)

BIN
terminal-rain-showcase.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB