Created
October 3, 2020 22:17
-
-
Save Ceasar/bdae81edfdc3e68bb73a3a216c58aa15 to your computer and use it in GitHub Desktop.
Create cool streams of art.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Generate a stream of characters that look like rain drops. | |
""" | |
import random | |
import time | |
class Bullet(object): | |
def __init__(self, pos=0, char='*', speed=1): | |
self.pos = pos | |
self.char = char | |
self.speed = speed | |
def update(self, chars): | |
chars[self.pos] = self.char | |
self.pos = (self.pos + self.speed) % len(chars) | |
class RandomBullet(object): | |
def __init__(self, pos=0, char='*', p=1, bias=0): | |
self.pos = pos | |
self.char = char | |
self.p = p | |
self.bias = bias | |
def update(self, chars): | |
if random.random() <= self.p: | |
chars[self.pos] = self.char | |
speed = int(round(random.randint(-1, 1) + self.bias)) | |
self.pos = (self.pos + speed) % len(chars) | |
effects = [ | |
# Bullet(), | |
# Bullet(char='C', speed=-1), | |
RandomBullet(p=0.02, char='C'), | |
RandomBullet(p=0.02, char='E'), | |
RandomBullet(p=0.02, char='A'), | |
RandomBullet(p=0.02, char='S'), | |
RandomBullet(p=0.02, char='A'), | |
RandomBullet(p=0.02, char='R'), | |
] | |
pos = 0 | |
while True: | |
chars = [' '] * 80 | |
for effect in effects: | |
effect.update(chars) | |
print(''.join(chars)) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment