Skip to content

Instantly share code, notes, and snippets.

@sgh
Created December 30, 2019 00:12
Show Gist options
  • Save sgh/7da59d9dd6c365c7378c26c67c21cf5f to your computer and use it in GitHub Desktop.
Save sgh/7da59d9dd6c365c7378c26c67c21cf5f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import random
import os
import time
tree = list(open('tree2.txt').read().rstrip())
def colored_dot(color):
if color == 'red':
return f'\033[91m⏺\033[0m'
if color == 'green':
return f'\033[92m⏺\033[0m'
if color == 'yellow':
return f'\033[93m⏺\033[0m'
if color == 'blue':
return f'\033[94m⏺\033[0m'
def lights(color, indexes, off):
for idx in indexes:
tree[idx] = colored_dot(color) if off else '⏺'
class Color:
def __init__(self, name):
self.name = name
self.indexes = []
self.time = 0.0
self.off = False
colors = [Color('yellow'), Color('red'), Color('green'), Color('blue') ]
for i, c in enumerate(tree):
if c == 'Y':
colors[0].indexes.append(i)
tree[i] = '⏺'
if c == 'R':
colors[1].indexes.append(i)
tree[i] = '⏺'
if c == 'G':
colors[2].indexes.append(i)
tree[i] = '⏺'
if c == 'B':
colors[3].indexes.append(i)
tree[i] = '⏺'
while True:
redraw = False
for c in colors:
c.time -= 0.1
if (c.time <= 0):
c.off ^= True
c.time = random.uniform(0.5, 1.5)
lights(c.name, c.indexes, c.off)
redraw = True
if redraw:
os.system('cls' if os.name == 'nt' else 'clear')
print(''.join(tree))
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment