houseplant.py
python · 142 lines
1#!/usr/bin/env python32"""A houseplant, because green is nice."""34import random56# ANSI colors7DARK_GREEN = "\033[32m"8BRIGHT_GREEN = "\033[92m"9TERRACOTTA = "\033[38;5;173m"10PINK = "\033[38;5;211m"11YELLOW = "\033[93m"12SOIL = "\033[38;5;95m"13RESET = "\033[0m"14DIM = "\033[2m"1516GREENS = [DARK_GREEN, BRIGHT_GREEN, "\033[38;5;28m", "\033[38;5;34m", "\033[38;5;71m"]17FLOWER_COLORS = [PINK, YELLOW, "\033[38;5;216m", "\033[38;5;183m"]181920def make_plant():21 W = 3222 lines = []23 mid = W // 22425 num_stems = random.randint(3, 5)26 stems = []2728 for _ in range(num_stems):29 height = random.randint(4, 8)30 drift = 031 stem = []32 for h in range(height):33 drift += random.choice([-1, 0, 0, 1])34 drift = max(-3, min(3, drift))35 has_left = random.random() < 0.45 and h > 036 has_right = random.random() < 0.45 and h > 037 stem.append((drift, has_left, has_right))38 stems.append(stem)3940 max_h = max(len(s) for s in stems)4142 # Render plant rows (top to bottom)43 plant_rows = []44 for row in range(max_h):45 chars = [' '] * W46 colors = [''] * W4748 for si, stem in enumerate(stems):49 spread = (si - num_stems // 2) * 250 h_from_bottom = max_h - 1 - row51 if h_from_bottom >= len(stem):52 continue53 drift, has_left, has_right = stem[h_from_bottom]54 col = mid + spread + drift5556 if col < 1 or col >= W - 1:57 continue5859 g = random.choice(GREENS)6061 # Stem character62 chars[col] = random.choice(['│', '┃', '╽']) if h_from_bottom > 0 else '│'63 colors[col] = g6465 # Top of stem: flower or leaf crown66 if h_from_bottom == len(stem) - 1:67 if random.random() < 0.35:68 chars[col] = random.choice(['✿', '❀', '✾', '❁'])69 colors[col] = random.choice(FLOWER_COLORS)70 else:71 chars[col] = random.choice(['♠', '♣', '❦', '✦'])72 colors[col] = BRIGHT_GREEN7374 # Leaves along the stem75 if has_left and col > 1:76 chars[col - 1] = random.choice([')', '}', '>', '⌉', '❧'[0]])77 colors[col - 1] = random.choice(GREENS)78 if has_right and col < W - 2:79 chars[col + 1] = random.choice(['(', '{', '<', '⌈'])80 colors[col + 1] = random.choice(GREENS)8182 rendered = ''83 for c, clr in zip(chars, colors):84 if c != ' ':85 rendered += clr + c + RESET86 else:87 rendered += c88 plant_rows.append(rendered)8990 # --- Pot ---91 pot_w = 1492 pot_left = mid - pot_w // 29394 pot_lines = []9596 # Soil rim97 soil = SOIL + '┌' + '─' * pot_w + '┐' + RESET98 pot_lines.append(' ' * (pot_left - 1) + soil)99100 # Soil surface101 soil_fill = SOIL + '│' + DARK_GREEN + ''.join(102 random.choice(['·', '.', ',', ':', ' ']) for _ in range(pot_w)103 ) + SOIL + '│' + RESET104 pot_lines.append(' ' * (pot_left - 1) + soil_fill)105106 # Pot body (tapered)107 for i in range(4):108 w = pot_w - (i * 2)109 if w < 4:110 break111 left = pot_left + i112 if i == 0:113 body = TERRACOTTA + '┃' + '▒' * w + '┃' + RESET114 else:115 body = TERRACOTTA + '┃' + '░' * w + '┃' + RESET116 pot_lines.append(' ' * left + body)117118 # Base119 base_w = pot_w - 6120 if base_w < 2:121 base_w = 2122 base_left = pot_left + 4123 base = TERRACOTTA + '╰' + '─' * base_w + '╯' + RESET124 pot_lines.append(' ' * base_left + base)125126 # Assemble127 lines.append('')128 lines.extend(plant_rows)129 lines.extend(pot_lines)130 lines.append('')131132 # Label, centered loosely133 label = DIM + ' a plant, because green is nice' + RESET134 lines.append(label)135 lines.append('')136137 return '\n'.join(lines)138139140if __name__ == '__main__':141 print(make_plant())142