Advertisement
here2share

# Tk_twigs.py

Oct 21st, 2021
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. # Tk_twigs.py
  2.  
  3. import math
  4. import random
  5. from Tkinter import *
  6. from PIL import Image, ImageDraw
  7.  
  8. ww = hh = 680
  9. ctr = ww/2
  10.  
  11. root = Tk()
  12. root.title("Tk_twigs")
  13.  
  14. root.geometry("%dx%d+0+0"%(ww,hh))
  15.  
  16. canvas = Canvas(root, width=ww, height=hh)
  17. canvas.pack()
  18.  
  19. xy = []
  20.  
  21. def e(n):
  22.     return n if n else 1
  23.    
  24. def oRGB(rgb): # pass
  25.     r,g,b = rgb
  26.     return "#%02x%02x%02x" % (r,g,b)
  27.  
  28. def draw():
  29.     canvas.delete('all')
  30.     canvas.create_oval(xy, fill='red', width=1)
  31.     canvas.update()
  32.  
  33. class Circle:
  34.     def __init__(self, radius, location, color):
  35.         """
  36.         :param radius: radius of circle in pixels
  37.         :param location: two tuple of x,y
  38.         """
  39.         self.radius = radius
  40.         self.location = location
  41.         self.angle = random.randint(0, 36000)*0.01
  42.         self.curve = random.randint(15, 100)/100.0*random.choice([-1,1])
  43.         self.active = True
  44.         self.color = color
  45.  
  46.     @property
  47.     def x(self):
  48.         return self.location[0]
  49.  
  50.     @property
  51.     def y(self):
  52.         return self.location[1]
  53.  
  54.     def draw(self):
  55.         if not self.active:
  56.             return
  57.         canvas.create_oval((self.x - self.radius,
  58.                             self.y - self.radius,
  59.                             self.x + self.radius,
  60.                             self.y + self.radius),
  61.                            fill=oRGB(self.color))
  62.  
  63.     def push(self):
  64.         if not self.active:
  65.             return
  66.  
  67.         if self.should_make_baby() and self.radius > 7 and len(circles) < 180:
  68.             circles.append(self.make_baby())
  69.  
  70.         # Let's step by 1/4 of the radius each time
  71.         step = self.radius * 0.4
  72.         rad_angle = math.radians(self.angle)
  73.         next_step = (self.x + step*math.cos(rad_angle),
  74.                      self.y + step*math.sin(rad_angle))
  75.         # Stepping by 1/4 of the radius will put us still inside our current radius, so let's look a bit further ahead
  76.         big_step = (self.x + self.radius*2*math.cos(rad_angle),
  77.                     self.y + self.radius*2*math.sin(rad_angle))
  78.         if self.within_bounds(next_step) and self.free_spot(big_step):
  79. #        if self.within_bounds(next_step): 
  80.             self.location = next_step
  81.         else:
  82.             self.active = False
  83.             if self in circles:
  84.                 circles.remove(self)
  85.  
  86.         self.angle = (self.angle + self.curve) % 360
  87.  
  88.     @staticmethod
  89.     def within_bounds(location):
  90.         t = 60
  91.         if location[0] < t or location[0] > ww-t or location[1] < t or location[1] > hh-t:
  92.             return False
  93.         return True
  94.  
  95.     def free_spot(self, spot):
  96.         # Simply check the canvas to see if the passed spot is white
  97.         return self.within_bounds(spot) and image.getpixel(spot) == (0,0,0)
  98.    
  99.     @staticmethod
  100.     def should_make_baby():
  101.         # chance to make a baby
  102.         if not babies:
  103.             t = [1]+[0]*30
  104.             babies.extend(t)
  105.             random.shuffle(babies)
  106.         return babies.pop()
  107.  
  108.     def make_baby(self):       
  109.         return Circle(self.radius*0.8, self.location, self.color)
  110.      
  111. image_bounds = (ww, hh)
  112. image = Image.new('RGB', image_bounds, 'black')
  113.  
  114. t = 270
  115. sq = (hh-(t*2))/5
  116.  
  117. while 1:
  118.     canvas.delete('all')
  119.     circles = []
  120.     spots = [(x*sq+t,y*sq+t) for x in range(6) for y in range(6)]
  121.     random.shuffle(spots)
  122.  
  123.     forRGB = [z for z in range(15,255,30)]
  124.  
  125.     for _ in range(8):
  126.         random.shuffle(forRGB)
  127.         circles.append(Circle(random.randint(10,16),
  128.                               (spots.pop()),
  129.                              (forRGB[:3])))
  130.  
  131.     babies = [0]
  132.     for n in range(120):
  133.         for circle in circles:
  134.             circle.draw()
  135.             circle.push()
  136.  
  137.         # print n, len(circles)
  138.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement