Skip to content

Instantly share code, notes, and snippets.

@miikka
Last active October 17, 2016 10:08
Show Gist options
  • Save miikka/74a81e4c9d495c2cefbb875084ec4a31 to your computer and use it in GitHub Desktop.
Save miikka/74a81e4c9d495c2cefbb875084ec4a31 to your computer and use it in GitHub Desktop.
Generate random symmetric sprites in the terminal
#!/usr/bin/env python
# Inspired by this tweet: https://twitter.com/AtticusBones/status/787936821892317184
from __future__ import division
import atexit
import curses
import math
import random
import sys
import time
W = 8
H = 8
COLOR = random.randint(1, 7)
MUTATION_COUNT = random.randint(1, 5)
c = int(math.ceil(W/2))
d = int(W/2)
sprite = {(x, y): random.randint(0, 1) for x in range(c) for y in range(H)}
mutations = [(random.randint(0, d), random.randint(0, H-1)) for x in range(MUTATION_COUNT)]
def mirror(x):
return d - (x - c + 1)
for y in range(H):
for x in range(c, W):
sprite[(x,y)] = sprite[(mirror(x), y)]
stdscr = None
def pr(sprite):
for y in range(H):
for x in range(W):
if sprite[(x,y)] > 0:
stdscr.addstr(" ", curses.color_pair(1))
else:
stdscr.addstr(" ", curses.color_pair(0))
stdscr.addstr("\n")
stdscr.refresh()
def mutate(sprite):
for mutation in mutations:
x, y = mutation
sprite[(x, y)] = 1 - sprite[(x, y)]
sprite[(mirror(x), y)] = 1 - sprite[(mirror(x), y)]
@atexit.register
def goodbye():
curses.nocbreak()
if stdscr:
stdscr.keypad(0)
curses.echo()
curses.endwin()
stdscr = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, COLOR)
curses.curs_set(0)
while True:
stdscr.clear()
pr(sprite)
mutate(sprite)
time.sleep(0.3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment