Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_twigs.py
- import math
- import random
- from Tkinter import *
- from PIL import Image, ImageDraw
- ww = hh = 680
- ctr = ww/2
- root = Tk()
- root.title("Tk_twigs")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- xy = []
- def e(n):
- return n if n else 1
- def oRGB(rgb): # pass
- r,g,b = rgb
- return "#%02x%02x%02x" % (r,g,b)
- def draw():
- canvas.delete('all')
- canvas.create_oval(xy, fill='red', width=1)
- canvas.update()
- class Circle:
- def __init__(self, radius, location, color):
- """
- :param radius: radius of circle in pixels
- :param location: two tuple of x,y
- """
- self.radius = radius
- self.location = location
- self.angle = random.randint(0, 36000)*0.01
- self.curve = random.randint(15, 100)/100.0*random.choice([-1,1])
- self.active = True
- self.color = color
- @property
- def x(self):
- return self.location[0]
- @property
- def y(self):
- return self.location[1]
- def draw(self):
- if not self.active:
- return
- canvas.create_oval((self.x - self.radius,
- self.y - self.radius,
- self.x + self.radius,
- self.y + self.radius),
- fill=oRGB(self.color))
- def push(self):
- if not self.active:
- return
- if self.should_make_baby() and self.radius > 7 and len(circles) < 180:
- circles.append(self.make_baby())
- # Let's step by 1/4 of the radius each time
- step = self.radius * 0.4
- rad_angle = math.radians(self.angle)
- next_step = (self.x + step*math.cos(rad_angle),
- self.y + step*math.sin(rad_angle))
- # Stepping by 1/4 of the radius will put us still inside our current radius, so let's look a bit further ahead
- big_step = (self.x + self.radius*2*math.cos(rad_angle),
- self.y + self.radius*2*math.sin(rad_angle))
- if self.within_bounds(next_step) and self.free_spot(big_step):
- # if self.within_bounds(next_step):
- self.location = next_step
- else:
- self.active = False
- if self in circles:
- circles.remove(self)
- self.angle = (self.angle + self.curve) % 360
- @staticmethod
- def within_bounds(location):
- t = 60
- if location[0] < t or location[0] > ww-t or location[1] < t or location[1] > hh-t:
- return False
- return True
- def free_spot(self, spot):
- # Simply check the canvas to see if the passed spot is white
- return self.within_bounds(spot) and image.getpixel(spot) == (0,0,0)
- @staticmethod
- def should_make_baby():
- # chance to make a baby
- if not babies:
- t = [1]+[0]*30
- babies.extend(t)
- random.shuffle(babies)
- return babies.pop()
- def make_baby(self):
- return Circle(self.radius*0.8, self.location, self.color)
- image_bounds = (ww, hh)
- image = Image.new('RGB', image_bounds, 'black')
- t = 270
- sq = (hh-(t*2))/5
- while 1:
- canvas.delete('all')
- circles = []
- spots = [(x*sq+t,y*sq+t) for x in range(6) for y in range(6)]
- random.shuffle(spots)
- forRGB = [z for z in range(15,255,30)]
- for _ in range(8):
- random.shuffle(forRGB)
- circles.append(Circle(random.randint(10,16),
- (spots.pop()),
- (forRGB[:3])))
- babies = [0]
- for n in range(120):
- for circle in circles:
- circle.draw()
- circle.push()
- # print n, len(circles)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement