Code/NodeBox

particle(rect+oval)_class

maetel 2007. 6. 20. 18:08
Compare a) with b).

a)
class Oval:
    def __init__(self, x, y, vx, vy):
        self.x=x
        self.y=y
        self.vx=vx
        self.vy=vy
        
    def render(self):
        c=0.001
        r=1.1
        
        ax=-self.vx*c
        ay=-self.vy*c
        self.vx+=ax
        self.vy+=ay
        self.x+=self.vx
        self.y+=self.vy
                
        if (self.x>=WIDTH):
            self.vx*=-r
            self.x=WIDTH
            
        if (self.x<=0):
            self.vx*=-r
            self.x=0
            
        if (self.y>=HEIGHT):
            self.vy*=-r
            self.y=HEIGHT
            
        if (self.y<=0):
            self.vy*=-r
            self.y=0
                        
        oval(self.x, self.y, 20, 20)
            
class Rect:
    def __init__(self, x, y, vx, vy):
        self.x=x
        self.y=y
        self.vx=vx
        self.vy=vy
        
    def render(self):
        c=0.001
        r=1.1
        
        ax=-self.vx*c
        ay=-self.vy*c
        self.vx+=ax
        self.vy+=ay
        self.x+=self.vx
        self.y+=self.vy
                
        if (self.x>=WIDTH):
            self.vx*=-r
            self.x=WIDTH
            
        if (self.x<=0):
            self.vx*=-r
            self.x=0
            
        if (self.y>=HEIGHT):
            self.vy*=-r
            self.y=HEIGHT
            
        if (self.y<=0):
            self.vy*=-r
            self.y=0

        rect(self.x,self.y, 25, 25)
                 
        
size(300,300)
speed(100)    
    
def setup():
    global ball
    global cell
    
    ball=range(5)
    cell=range(5)
    
    for i in range(5):
        ball[i]=Oval(i*20+150,i*5+150,i*5+1, i*5+1)
        cell[i]=Rect(i*25+50,i*25+150, i*5-1, i*5-1)    
        
def draw():
    global ball
    global cell
                
    for i in ball:
        i.render()
    
    for i in cell:
        i.render()

            
b)
class Particle:
    def __init__(self, x, y, vx, vy):
        self.x=x
        self.y=y
        self.vx=vx
        self.vy=vy
           
    def dynamics(self):
        c=0.001
        r=1.1

        ax=-self.vx*c
        ay=-self.vy*c
        self.vx+=ax
        self.vy+=ay
        self.x+=self.vx
        self.y+=self.vy
               
        if (self.x>=WIDTH):
            self.vx*=-r
            self.x=WIDTH
           
        if (self.x<=0):
            self.vx*=-r
            self.x=0
           
        if (self.y>=HEIGHT):
            self.vy*=-r
            self.y=HEIGHT
           
        if (self.y<=0):
            self.vy*=-r
            self.y=0

    def oval(self):
        oval(self.x+30, self.y-30, 20, 20)

    def rect(self):
        rect(self.x-30, self.y+30, 30, 30)
                
       
size(300,300)
speed(100)   
   
def setup():
    global ping
   
    ping=range(5)
   
    for i in range(5):
        ping[i]=Particle(i*20+150, i*20+150, i*5+2, i*5+1)
       
       
def draw():
    global ping
               
    for i in ping:
        i.dynamics()
        i.oval()
        i.rect()