import pyglet from pyglet.window import key window = pyglet.window.Window(200, 200) get = lambda w, i: w[i] if 0 <= i < len(w) else 0 next_wire = lambda w: [get(w, i-1) ^ get(w, i+1) for i in range(len(w))] def init(n): global wire_stack, label wire_stack = [[1] + [0]*(n-1)] h = 30 + 20 + 20*36 window.set_size(20 + 20*n, h) label = pyglet.text.Label("N = " + str(n), font_name='Times New Roman', font_size=20, x=10, y=h-10, anchor_y="top") def update_wire(dt): wire_stack.insert(0, next_wire(wire_stack[0])) if len(wire_stack) > 36: wire_stack.pop() pyglet.clock.schedule_interval(update_wire, 0.05) def draw_rectangle(x, y, w, h): pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, x+w, y, x+w, y+h, x, y+h])) @window.event def on_draw(): window.clear() label.draw() for y, wire in enumerate(wire_stack): for x, c in enumerate(wire): if c: draw_rectangle(10 + 20*x, 10 + 20*y, 10, 10) @window.event def on_key_press(symbol, modifiers): if symbol == key.MINUS: init(len(wire_stack[0])-1) elif symbol == key.EQUAL: init(len(wire_stack[0])+1) init(36) pyglet.app.run()