Write a Python program to generate 1000+ images of Mandelbrot Set with ever zooming ranges. Please note that Turtle is not used in this project for speed consideration. Then use an external software to combine the images into a video:
Source Code:
from PIL import Image
import colorsys
import math
px, py = -0.7746806106269039, -0.1374168856037867 #Tante Renate
R = 3
max_iteration = 2500
w, h = 1024,1024
mfactor = 0.5
def Mandelbrot(x,y,max_iteration,minx,maxx,miny,maxy):
zx = 0
zy = 0
RX1, RX2, RY1, RY2 = px-R/2, px+R/2,py-R/2,py+R/2
cx = (x-minx)/(maxx-minx)*(RX2-RX1)+RX1
cy = (y-miny)/(maxy-miny)*(RY2-RY1)+RY1
i=0
while zx**2 + zy**2 <= 4 and i < max_iteration:
temp = zx**2 - zy**2
zy = 2*zx*zy + cy
zx = temp + cx
i += 1
return i
def gen_Mandelbrot_image(sequence):
bitmap = Image.new("RGB", (w, h), "white")
pix = bitmap.load()
for x in range(w):
for y in range(h):
c=Mandelbrot(x,y,max_iteration,0,w-1,0,h-1)
v = c**mfactor/max_iteration**mfactor
hv = 0.67-v
if hv<0: hv+=1
r,g,b = colorsys.hsv_to_rgb(hv,1,1-(v-0.1)**2/0.9**2)
r = min(255,round(r*255))
g = min(255,round(g*255))
b = min(255,round(b*255))
pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
bitmap.save("Mandelbrot_"+str(sequence)+".jpg")
R=3
f = 0.975
RZF = 1/1000000000000
k=1
while R>RZF:
if k>100: break
mfactor = 0.5 + (1/1000000000000)**0.1/R**0.1
print(k,mfactor)
gen_Mandelbrot_image(k)
R *= f
k+=1
This program generates over 1000 images and may be quite slow. You can make it faster by dividing the work to multiple programs each doing a portion of those images.
Write a Python program to generate hundreds of images of Julia Set with ever zooming ranges. Please note that Turtle is not used in this project for speed consideration. Then use an external software to combine the images into a video:
The video above zooms into the center at 0.0958598997051 + 1.1501990019993i with c = 0.355 + 0.355i. In each iteration the range shrinks by 5% until the range becomes 1/10^13 the size of the original range. You can modify the code to try different centers and c. The following is the source code:
from PIL import Image
import colorsys
cx = -0.7269
cy = 0.1889
R = (1+(1+4*(cx**2+cy**2)**0.5)**0.5)/2+0.001
max_iteration = 512
print(R)
w, h, zoom = 1500,1500,1
def julia(cx,cy,RZ,px,py,R,max_iteration,x,y,minx,maxx,miny,maxy):
zx = (x-minx)/(maxx-minx)*2*RZ-RZ+px
zy = (y-miny)/(maxy-miny)*2*RZ-RZ+py
i=0
while zx**2 + zy**2 < R**2 and i < max_iteration:
temp = zx**2 - zy**2
zy = 2*zx*zy + cy
zx = temp + cx
i += 1
return i
def gen_julia_image(sequence,cx,cy,RZ):
bitmap = Image.new("RGB", (w, h), "white")
pix = bitmap.load()
for x in range(w):
for y in range(h):
# smllaer: right,down
c=julia(cx,cy,RZ,0.0958598997051,1.1501990019993,R,max_iteration,x,y,0,w-1,0,h-1)
v = c/max_iteration
hv = 0.67-v*0.67
r,g,b = colorsys.hsv_to_rgb(hv,1,v/2+0.45)
r = min(255,round(r*255))
g = min(255,round(g*255))
b = min(255,round(b*255))
pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
bitmap.save("julia_"+str(sequence)+".jpg")
# bitmap.show()
f = 0.95
RZF = 1/1000000000000
RZ = 1
k=1
while RZ>RZF:
print(k,RZ)
gen_julia_image(k,0.355,0.355,RZ)
RZ *= f
k+=1
In this project, we are going to animate the Julia Set by varying the constant c in the normal Julia Set function. Turtle will be too slow to do this work. Instead we are going to use the PIL library to generate 360 images. Then, a video editing software can be used to combine 360 images into a video file. The following is the source code:
from PIL import Image
import colorsys
import math
cx = 0.7885
cy = 0
R = (1+(1+4*(cx**2+cy**2)**0.5)**0.5)/2+0.001
max_iteration = 200
w, h, zoom = 1000,1000,1
def julia(cx,cy,R,max_iteration,x,y,minx,maxx,miny,maxy):
zx = (x-minx)/(maxx-minx)*2*R-R
zy = (y-miny)/(maxy-miny)*2*R-R
i=0
while zx**2 + zy**2 < R**2 and i < max_iteration:
temp = zx**2 - zy**2
zy = 2*zx*zy + cy
zx = temp + cx
i += 1
return i
def gen_julia_image(sequence,cx,cy):
bitmap = Image.new("RGB", (w, h), "white")
pix = bitmap.load()
for x in range(w):
for y in range(h):
c=julia(cx,cy,R,max_iteration,x,y,0,w-1,0,h-1)
r,g,b = colorsys.hsv_to_rgb(c/max_iteration,1,0.9)
r = min(255,round(r*255))
g = min(255,round(g*255))
b = min(255,round(b*255))
pix[x,y] = int(b) + (int(g) << 8) + (int(r) << 16)
bitmap.save("julia_"+str(sequence)+".jpg")
for i in range(360):
cx = 0.7885*math.cos(i*math.pi/180)
cy = 0.7885*math.sin(i*math.pi/180)
gen_julia_image(i,cx,cy)
Develop a typing game to improve keyboard skill as demonstrated in the following YouTube video.
At any moment ten random letters fall from the top of screen. When you hit a correct letter from keyboard, that letter disappears and is replaced by a new random letter dropping from the top. Also, your score will increase by 1. When you hit an incorrect letter, your score will decrease by 1.
Make these letters fall at random speeds and also let the speed increase gradually as time progresses. The game will end when a letter falls to the bottom of the screen.
You will need to use Turtle’s ontimer() and onkey() events to implement this project.
Source Code:
import turtle
import random
screen = turtle.Screen()
screen.setup(1000,1000)
screen.title('Typing Game - PythonTurtle.Academy')
screen.bgcolor('blue')
screen.tracer(0,0)
turtle.hideturtle()
turtle.up()
turtle.color('red')
score_turtle = turtle.Turtle()
score_turtle.color('red')
score_turtle.up()
score_turtle.hideturtle()
turtle.goto(350,400)
turtle.write('Score: ', align='center', font=('Courier',25,'normal'))
min_speed = 5
max_speed = 30
letters = []
speeds = []
pos = []
lts = []
n = 10
game_over = False
score = 0
def increase_difficulty():
global min_speed, max_speed
min_speed += 1
max_speed += 1
screen.ontimer(increase_difficulty,5000)
def draw_game_over():
turtle.goto(0,0)
turtle.color('red')
turtle.write('GAME OVER', align='center', font=('Courier',50,'normal'))
turtle.goto(0,-150)
turtle.color('orange')
turtle.write('Your Score is {}'.format(score), align='center', font=('Courier',40,'normal'))
screen.update()
def draw_score():
score_turtle.clear()
score_turtle.goto(420,400)
score_turtle.write('{}'.format(score),align='center',font=('Courier',25,'normal'))
screen.update()
def draw_letters():
global game_over
for i in range(len(letters)):
lts[i].clear()
lts[i].goto(pos[i])
lts[i].write(letters[i],align='center',font=('courier',20,'normal'))
pos[i][1] -= speeds[i]
if pos[i][1]<-500:
game_over = True
draw_game_over()
return
screen.update()
screen.ontimer(draw_letters,50)
def f(c): # handle keyboard press
global score
if c in letters:
score += 1
k = letters.index(c)
while True:
l = chr(ord('a')+random.randrange(26))
if l not in letters:
letters[k] = l
break
pos[k] = [random.randint(-450,450),500]
speeds[k] = random.randint(min_speed,max_speed)
else: score -= 1
draw_score()
for _ in range(n):
lts.append(turtle.Turtle())
while True:
l = chr(ord('a')+random.randrange(26))
if l not in letters:
letters.append(l)
break
speeds.append(random.randint(min_speed,max_speed))
pos.append([random.randint(-450,450),500])
for i in range(n):
lts[i].speed(0)
lts[i].hideturtle()
lts[i].up()
lts[i].color('yellow')
draw_letters()
increase_difficulty()
screen.onkey(lambda: f('a'), 'a')
screen.onkey(lambda: f('b'), 'b')
screen.onkey(lambda: f('c'), 'c')
screen.onkey(lambda: f('d'), 'd')
screen.onkey(lambda: f('e'), 'e')
screen.onkey(lambda: f('f'), 'f')
screen.onkey(lambda: f('g'), 'g')
screen.onkey(lambda: f('h'), 'h')
screen.onkey(lambda: f('i'), 'i')
screen.onkey(lambda: f('j'), 'j')
screen.onkey(lambda: f('k'), 'k')
screen.onkey(lambda: f('l'), 'l')
screen.onkey(lambda: f('m'), 'm')
screen.onkey(lambda: f('n'), 'n')
screen.onkey(lambda: f('o'), 'o')
screen.onkey(lambda: f('p'), 'p')
screen.onkey(lambda: f('q'), 'q')
screen.onkey(lambda: f('r'), 'r')
screen.onkey(lambda: f('s'), 's')
screen.onkey(lambda: f('t'), 't')
screen.onkey(lambda: f('u'), 'u')
screen.onkey(lambda: f('v'), 'v')
screen.onkey(lambda: f('w'), 'w')
screen.onkey(lambda: f('x'), 'x')
screen.onkey(lambda: f('y'), 'y')
screen.onkey(lambda: f('z'), 'z')
screen.listen()
screen.mainloop()