# # Tk_flocking_boidz.py

Apr 11th, 2021 (edited)
622
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # Tk_flocking_boidz.py
2.
3. from Tkinter import *
4. from PIL import Image, ImageTk
5. from math import sin, cos, radians, atan2, pi, sqrt
6. from random import randint as rnd
7. import time
8.
9. ww = 1200
10. hh = 680
11.
12. def rgb2hex(rgb):
13.     r,g,b = rgb
14.     return "#%02x%02x%02x" % (r,g,b)
15.
16. root = Tk()
17. root.title("Tk_flocking_boidz")
18. root.geometry("%dx%d+0+0"%(ww,hh))
19. canvas = Canvas(root, width=ww, height=hh)
20. canvas.grid()
21.
22. of_BOIDZ = 50
23.
24. __boid = ((6,-6), (15,15), (-6,6), (4,4))
25.
26. colors = [  (255,0,0),
27.             (255,165,0),
28.             (255,255,0),
29.             (0,128,0),
30.             (0,0,255),
31.             (0,255,255),
32.             (128,0,128),
33.             (0,0,139),
34.             (0,100,0),
35.             (255,20,147),
36.             (75,0,130),
37.             (204,153,255),
38.             (173,216,230),
39.             (178,255,102),
40.             (255,255,102),
41.             (0,255,0),
42.             (107,142,35),
43.             (139,69,19),
44.             (255,215,0),
45.             (65,105,225) ]
46.
47. COLORS = [rgb2hex(z) for z in colors]
48. Lc = len(COLORS)
49.
50. boidz = {}
51. boidz['x'] = []
52. boidz['y'] = []
53. ySector = {}
54. for i in range(of_BOIDZ):
55.     x,y = (rnd(0,ww)),(rnd(0,hh))
56.     boidz['x'] += [(x)]
57.     boidz['y'] += [(y)]
58.     try:
59.         ySector[y] += [(x,y,i)]
60.     except:
61.         ySector[y] = [(x,y,i)]
62.     boidz[i,'a'] = rnd(0, 3600)*0.1
63.     boidz[i,'c'] = COLORS[i%Lc]
64.     boidz[i,'s'] = rnd(4999,5000)*0.001
65.
66. def distance(x,y,x2,y2):
67.     t = (x - x2)**2.0 + (y - y2)**2.0
68.     return sqrt(t)
69.
70. def angle_obj(angle=0):
71.     angle=angle%360
72.     VERTEX = __boid
73.
74.     # rotation
76.               for (x,y) in VERTEX]
77.     return VERTEX
78.
79. def move_obj(VERTEX,angle):
80.     angle=angle%360
81.     sp = boidz[i,'s']
82.     # move obj
84.
85.     VERTEX = [(x + cx - 50, y + cy - 50) for (x,y) in VERTEX]
86.     x,y = cx%(ww+100),cy%(hh+100)
87.     next_xboidz[i] = (x)
88.     next_yboidz[i] = (y)
89.     try:
90.         next_ySector[y] += [(x,y,i)]
91.     except:
92.         next_ySector[y] = [(x,y,i)]
93.     return VERTEX
94.
95. def flock(k,dist=120):
96.     x,y = boidz['x'][k],boidz['y'][k]
97.     ySector[y].remove((x,y,k))
98.     angle = boidz[k,'a']
100.     d = dist/2
101.     ttt = yS[:]+[cy-d]+[cy+d]
102.     ttt.sort()
103.     a = int(ttt.index(cy-d))+1
104.     b = int(ttt.index(cy+d))
105.     nn = sum([ySector[i] for i in ttt[a:b]],[])
106.     ttt = nn[:]+[(cx-d,)]+[(cx+d,)]
107.     ttt.sort()
108.     a = int(ttt.index((cx-d,)))+1
109.     b = int(ttt.index((cx+d,)))
110.     nn = [i for i in ttt[a:b]]
111.     p = []
112.     rotate = 9
113.     if nn:
114.         for z in nn:
115.             try:
116.                 dx,dy,i = z
117.                 p += [(distance(cx,cy,dx,dy),dx,dy)]
118.             except:
119.                 0
120.         d,dx,dy = min(p)
121.         if d < dist:
122.             a = atan2(dx,dy)/pi*180
123.             LR = abs(angle-a) < 180
124.             if distance(x,y,dx,dy) < 40:
125.                 LR = False if LR else True
126.                 rotate = 0.1
127.     else:
128.         LR = 0 if x > y else 1
129.         rotate = 1
130.     if LR:
131.         boidz[k,'a'] = (angle+rotate)%360
132.     else:
133.         boidz[k,'a'] = (angle-rotate)%360
134.
135. next_ySector = {}
136. next_xboidz = boidz['x'][:]
137. next_yboidz = boidz['y'][:]
138. while 1:
139.     canvas.delete('all')
140.     yS = list(ySector)
141.     for i in range(of_BOIDZ):
142.         flock(i)
143.         OBJ = angle_obj(boidz[i,'a']+315)
144.         OBJ = move_obj(OBJ, boidz[i,'a'])
145.         canvas.create_polygon(OBJ,fill=boidz[i,'c'],outline='black')
146.     ySector = next_ySector
147.     boidz['x'] = next_xboidz[:]
148.     boidz['y'] = next_yboidz[:]
149.     next_ySector = {}
150.     root.update()
151.
152.
RAW Paste Data