Code/NodeBox

arrive

maetel 2007. 6. 25. 18:50
flock

class Boid:
    def __init__(self,x,y,vx,vy):
        self.x=x
        self.y=y
        self.vx=vx
        self.vy=vy
        self.tx=0
        self.ty=0
        
    def target(self,tx,ty):
        self.tx=tx
        self.ty=ty
#        oval(tx,ty,30,30)
        
    def chase(self):
        dx=self.tx-self.x
        dy=self.ty-self.y
        sx=dx-self.vx
        sy=dy-self.vy   
        ax=0
        ay=0
        ax+=sx/random(10,100)
        ay+=sy/random(10,100)
        self.vx+=ax
        self.vy+=ay
        self.x+=self.vx
        self.y+=self.vy
        
        r=10
        maxs=10

        if (dx==0) & (dy==0):
            self.vx=0
            self.vy=0
            
        if (dx>-r) & (dy<r):
            self.vx/=2
            self.vy/=2

        if (self.vx>maxs):
            self.vx=maxs
        if (self.vx<-maxs):
            self.vx=-maxs
        if (self.vy>maxs):
            self.vy=maxs
        if (self.vy<-maxs):
            self.vy=-maxs
            
    def render(self):
        fill(0.5)
        ovalc(self.x, self.y, 10, 10)
            
    #def update(self,ax,ay):
    #    self.ax=0
    #    self.ay=0    
    def boarders(self):
        r=10
        if (self.x<-r):
            self.x=WIDTH+r
        if (self.y<-r):
            self.y=HEIGHT+r
        if (self.x>WIDTH+r):
            self.x=-r
        if (self.y>HEIGHT+r):
            self.y=-r
            
def ovalc(x,y,w,h):
    x=x-w/2
    y=y-h/2
    oval(x,y,w,h)            
        
size(300,300)
speed(100)

def setup():
    global b
    b=range(20)
    
    for i in range(20):
        b[i]=Boid(i*random(100), i*random(100), i*random(10), i*random(10))
        
def draw():
    global b

    fill(0)
    ovalc(MOUSEX,MOUSEY,30,30)
    
    for i in b:
        i.target(MOUSEX,MOUSEY)
        i.chase()
        i.boarders()
        i.render()