Skip to content

Instantly share code, notes, and snippets.

@jsundram
Last active Dec 17, 2015
Embed
What would you like to do?
implementation of BoxyLady2 aimed at pypy

To get pypy installed:

  • curl -O https://bitbucket.org/pypy/pypy/downloads/pypy-2.0.1-osx64.tar.bz2
  • tar -xvf pypy-2.0.1-osx64.bz2
  • cd ./pypy-2.0.1/bin
  • curl -O http://python-distribute.org/distribute_setup.py
  • curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
  • ./pypy distribute_setup.py
  • ./pypy get-pip.py
  • export PIP_REQUIRE_VIRTUALENV=false; ./pypy get-pip.py
  • ./pip install hg+https://pyglet.googlecode.com/hg/
  • ./pip install git+https://github.com/nodebox/nodebox-opengl
  • replace sys.getrefcount in nodebox/graphics/context.py to work with pypy
try:
      from sys import getrefcount
  except ImportError: #pypy
      import gc
      getrefcount = lambda x: len(gc.get_referrers(x)) - 1
from nodebox.graphics import *
from random import randint
import os
MINS, MAXS = 1, 40
grow = False
box_size = MAXS
counter = 1
output_dir = '/Users/%s/Downloads/nb/' % os.getlogin()
# Use a flat array of pixels because pic.get is too slow.
pic = Pixels(Image('pic.jpg'))
pixels = [pic.get(x,y) for y in range(0, pic.height) for x in range(0, pic.width)]
def setup():
canvas.fps = 2
canvas.size = pic.size
nostroke()
canvas.run(draw)
def draw(canvas):
global box_size, grow
make(box_size)
box_size += 1 if grow else -1
if box_size <= MINS:
grow = True
elif MAXS <= box_size:
grow = False
canvas.stop()
def make(i):
global counter, pixels
w, h = pic.width, pic.height
for cc in xrange(w * h / 2):
x = randint(0, w-1)
y = randint(0, h-1)
a = randint(MINS, i)
b = randint(MINS, i)
rect(x, y, a, b, fill=pixels[w*y + x])
# Would love to use canvas.frame, but it doesn't advance properly
# WTF? removing this save() call makes the code ~10x SLOWER???
canvas.save(output_dir + 'image%03d.png' % counter)
print canvas.profiler.framerate
counter += 1
if __name__ == '__main__':
setup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment