Last active
December 24, 2015 10:39
-
-
Save alexdelorenzo/6785816 to your computer and use it in GitHub Desktop.
pyglet front end for game of life, runs in pypy
This file contains 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
from gol import Universe as U | |
import pyglet | |
# too lazy to make into a class, just pulled all functions under pyglet_way() | |
def pyglet_way(): | |
x, y = 150, 150 | |
u = U(x, y) | |
u.fill_random(.4) | |
scale = 5 | |
x *= scale | |
y *= scale | |
# eventually define a 255 aRGB to 1.0 aRGB function. | |
# but for now, since functional composition flows logically, have some lambdas: | |
red, blue, green, alpha = \ | |
( | |
lambda tup, magnitude: (tup[0] + magnitude, tup[1], tup[2], tup[3]), | |
lambda tup, magnitude: (tup[0], tup[1], tup[2] + magnitude, tup[3]), | |
lambda tup, magnitude: (tup[0], tup[1] + magnitude, tup[2], tup[3]), | |
lambda tup, magnitude: (tup[0], tup[1], tup[2], tup[3] + magnitude) | |
) | |
black = 0.0, 0.0, 0.0, 1.0 | |
white = red(green(blue(black, 1.0), 1.0), 1.0) | |
purple = red(blue(alpha(black, 0.5), 0.5), 0.5) | |
color = white | |
set_live_color = pyglet.gl.glColor4f | |
window = pyglet.window.Window(width=x, height=y) | |
set_live_color(*color) | |
gl_flag, indices = 'v2i', (0, 1, 2, 0, 2, 3) | |
def draw_square(point, size): | |
x, y = point[0] * size, point[1] * size | |
x_size, y_size = x+size, y+size | |
vertices = x,y, x_size,y, x_size,y_size, x,y_size | |
pyglet.graphics.draw_indexed(4, pyglet.gl.GL_TRIANGLES, indices, (gl_flag, vertices)) | |
def process_living(living): | |
for live in living: | |
if u.val(live): | |
draw_square(live, scale) | |
def on_draw(self): | |
#callback needs self | |
window.clear() | |
go() | |
def go(): | |
u.tick() | |
process_living(u.live_points) | |
pyglet.clock.schedule(on_draw) | |
pyglet.app.run() | |
if __name__ == "__main__": | |
pyglet_way() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment