Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_flocking_boidz.py
- from Tkinter import *
- from PIL import Image, ImageTk
- from math import sin, cos, radians, atan2, pi, sqrt
- from random import randint as rnd
- import time
- ww = 1200
- hh = 680
- def rgb2hex(rgb):
- r,g,b = rgb
- return "#%02x%02x%02x" % (r,g,b)
- root = Tk()
- root.title("Tk_flocking_boidz")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.grid()
- of_BOIDZ = 50
- __boid = ((6,-6), (15,15), (-6,6), (4,4))
- colors = [ (255,0,0),
- (255,165,0),
- (255,255,0),
- (0,128,0),
- (0,0,255),
- (0,255,255),
- (128,0,128),
- (0,0,139),
- (0,100,0),
- (255,20,147),
- (75,0,130),
- (204,153,255),
- (173,216,230),
- (178,255,102),
- (255,255,102),
- (0,255,0),
- (107,142,35),
- (139,69,19),
- (255,215,0),
- (65,105,225) ]
- COLORS = [rgb2hex(z) for z in colors]
- Lc = len(COLORS)
- boidz = {}
- boidz['x'] = []
- boidz['y'] = []
- ySector = {}
- for i in range(of_BOIDZ):
- x,y = (rnd(0,ww)),(rnd(0,hh))
- boidz['x'] += [(x)]
- boidz['y'] += [(y)]
- try:
- ySector[y] += [(x,y,i)]
- except:
- ySector[y] = [(x,y,i)]
- boidz[i,'a'] = rnd(0, 3600)*0.1
- boidz[i,'c'] = COLORS[i%Lc]
- boidz[i,'s'] = rnd(4999,5000)*0.001
- def distance(x,y,x2,y2):
- t = (x - x2)**2.0 + (y - y2)**2.0
- return sqrt(t)
- def angle_obj(angle=0):
- angle=angle%360
- VERTEX = __boid
- # rotation
- VERTEX = [(x*cos(radians(angle)) + y*sin(radians(angle)), y*cos(radians(angle)) - x*sin(radians(angle)) )
- for (x,y) in VERTEX]
- return VERTEX
- def move_obj(VERTEX,angle):
- angle=angle%360
- sp = boidz[i,'s']
- # move obj
- cx, cy = sin(radians(angle))*sp+boidz['x'][i], cos(radians(angle))*sp+boidz['y'][i]
- VERTEX = [(x + cx - 50, y + cy - 50) for (x,y) in VERTEX]
- x,y = cx%(ww+100),cy%(hh+100)
- next_xboidz[i] = (x)
- next_yboidz[i] = (y)
- try:
- next_ySector[y] += [(x,y,i)]
- except:
- next_ySector[y] = [(x,y,i)]
- return VERTEX
- def flock(k,dist=120):
- x,y = boidz['x'][k],boidz['y'][k]
- ySector[y].remove((x,y,k))
- angle = boidz[k,'a']
- cx, cy = sin(radians(angle))*(dist/2)+x, cos(radians(angle))*(dist/2)+y
- d = dist/2
- ttt = yS[:]+[cy-d]+[cy+d]
- ttt.sort()
- a = int(ttt.index(cy-d))+1
- b = int(ttt.index(cy+d))
- nn = sum([ySector[i] for i in ttt[a:b]],[])
- ttt = nn[:]+[(cx-d,)]+[(cx+d,)]
- ttt.sort()
- a = int(ttt.index((cx-d,)))+1
- b = int(ttt.index((cx+d,)))
- nn = [i for i in ttt[a:b]]
- p = []
- rotate = 9
- if nn:
- for z in nn:
- try:
- dx,dy,i = z
- p += [(distance(cx,cy,dx,dy),dx,dy)]
- except:
- 0
- d,dx,dy = min(p)
- if d < dist:
- a = atan2(dx,dy)/pi*180
- LR = abs(angle-a) < 180
- if distance(x,y,dx,dy) < 40:
- LR = False if LR else True
- rotate = 0.1
- else:
- LR = 0 if x > y else 1
- rotate = 1
- if LR:
- boidz[k,'a'] = (angle+rotate)%360
- else:
- boidz[k,'a'] = (angle-rotate)%360
- next_ySector = {}
- next_xboidz = boidz['x'][:]
- next_yboidz = boidz['y'][:]
- while 1:
- canvas.delete('all')
- yS = list(ySector)
- for i in range(of_BOIDZ):
- flock(i)
- OBJ = angle_obj(boidz[i,'a']+315)
- OBJ = move_obj(OBJ, boidz[i,'a'])
- canvas.create_polygon(OBJ,fill=boidz[i,'c'],outline='black')
- ySector = next_ySector
- boidz['x'] = next_xboidz[:]
- boidz['y'] = next_yboidz[:]
- next_ySector = {}
- root.update()
Add Comment
Please, Sign In to add comment