Skip to content

Instantly share code, notes, and snippets.

@rameshvarun
Created June 2, 2013 16:45
Show Gist options
  • Save rameshvarun/5694091 to your computer and use it in GitHub Desktop.
Save rameshvarun/5694091 to your computer and use it in GitHub Desktop.
A python+pygame script that draws the Mandelbrot fractal from right to left. Sample result - http://i.imgur.com/LrtPv09.png
import pygame
import sys
SCREENWIDTH = 800
SCREENHEIGHT = 600
max_iteration = 255
pygame.init()
screen = pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT),pygame.DOUBLEBUF | pygame.HWSURFACE)
pygame.display.set_caption("Mandelbrot Fractal")
fractal = screen.copy()
pygame.mixer.init()
fractal.fill((0,0,0))
for i in range(SCREENWIDTH):
print i
for j in range(SCREENHEIGHT):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
x0 = (float(i)/SCREENWIDTH)*3.5 - 2.5
y0 = (float(j)/SCREENHEIGHT)*2 - 1
x = 0
y = 0
iteration = 0
while x*x + y*y < 2*2 and iteration < max_iteration:
xtemp = x*x - y*y + x0
y = 2*x*y + y0
x = xtemp
iteration = iteration + 1
fractal.set_at((i,j), (iteration,iteration,iteration))
screen.blit(fractal, (0,0))
pygame.display.flip()
running = True
while running:
#Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment