import sys, random from pyglet.gl import * width = 200 height = 200 window = pyglet.window.Window(width,height) maxIter = 32 def mandelbrot(c, maxIter): iter = 0 z = c while iter < maxIter: z = z*z + c if abs(z) > 2: return iter iter = iter + 1 return -1 colormap = [] for i in range(0,256): colormap.append(((i%4)*75,(i%4)*75,(i%4)*75)) center = (-0.75, 0.25) size = 1 @window.event def on_draw(): glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-0.5, width+0.5, -0.5, height+0.5) glMatrixMode(GL_MODELVIEW) for i in range(0,width): x = (i/(width-1.0)*size) - size/2.0 + center[0] glBegin(GL_POINTS) for j in range(0,height): y = (j/(height-1.0)*size) - size/2.0 + center[1] m = mandelbrot(x+y*1j, maxIter) if m == -1: glColor3ub(255,255,255) else: glColor3ub(colormap[m][0], colormap[m][1], colormap[m][2]) glVertex2i(i,j) glEnd() @window.event def on_key_press(key, modifiers): global size, maxIter if key == pyglet.window.key.PLUS: size = size / 2.0 elif key == pyglet.window.key.MINUS: size = size * 2.0 elif key == pyglet.window.key.A: maxIter += 16 elif key == pyglet.window.key.S: maxIter -= 16 pyglet.app.run()