Last active
July 7, 2020 14:09
-
-
Save timurbakibayev/0a40daa54ed329e0695e3b6067fa1746 to your computer and use it in GitHub Desktop.
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 PIL import Image, ImageDraw | |
import random | |
import math | |
images = [] | |
w, h = 500, 300 | |
class Particle: | |
_x = 0 | |
_y = 0 | |
x = 0 | |
y = 0 | |
angle = 0 | |
speed = 0 | |
color = (0,0,0) | |
turn_skip = 0 | |
turn_speed = 1 | |
counter = 0 | |
def __init__(self, x, y): | |
self.x = x + random.randint(-3, 3) | |
self.y = y | |
self._x = x | |
self._y = y | |
self.speed = random.randint(0, 200)/100 | |
self.color = (0, 255, random.randint(0, 50)) # start color | |
self.angle = -math.pi/2 | |
self.turn_skip = random.randint(20,60) | |
self.turn_speed = 1 | |
def turn(self): | |
self.counter += 1 | |
if self.counter > self.turn_skip: | |
self.counter = 0 | |
self.angle = self.angle + random.randint(-10, 10)/100*self.turn_speed | |
self.turn_skip /= 2 | |
self.turn_speed *= 1.1 | |
r,g,b = self.color | |
g -= 2 | |
if g < 0: | |
g = 0 | |
r += 2 | |
if r > 255: | |
r = 255 | |
self.color = (r,g,b) | |
def move(self): | |
dx = self.speed*math.cos(self.angle) | |
dy = self.speed*math.sin(self.angle) | |
self._x = self.x | |
self._y = self.y | |
self.x += dx | |
self.y += dy | |
each_third = 0 | |
def draw_particles(flowers): | |
global each_third | |
if len(images) > 0: | |
each_third = (each_third + 1) % 3 | |
if each_third == 0: | |
im = images[-1].copy() | |
else: | |
im = images[-1] | |
else: | |
im = Image.new('RGB', (w,h), (130, 200, 250)) | |
draw = ImageDraw.Draw(im) | |
for fl in flowers: | |
draw.line((fl._x, fl._y, fl.x, fl.y), fill=fl.color, width=1) | |
images.append(im) | |
grass = [] | |
for i in range(500): | |
grass.append(Particle(random.randint(0, w), h)) | |
for i in range(35): | |
for j in range(len(grass)): | |
grass[j].turn() | |
grass[j].move() | |
draw_particles(grass) | |
flowers = [] | |
for i in range(50): | |
flowers.append(Particle(w / 2, h)) | |
flowers.append(Particle(w / 4, h)) | |
flowers.append(Particle(w * 3 / 4, h)) | |
for i in range(300): | |
for flower in flowers: | |
flower.turn() | |
flower.move() | |
draw_particles(flowers) | |
for i in range(30): | |
images.append(images[-1].copy()) | |
images[0].save('flowers.gif', | |
save_all=True, append_images=images[1:], | |
optimize=True, duration=20, loop=0) | |
images[-1].save("flower.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment