Code/NodeBox

ping_class

maetel 2007. 6. 20. 12:01
class를 사용하여 ping_multi 코딩하기

class Ping:
    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)
   
       
size(300,300)
speed(100)   
   
def setup():
    global particle
   
    particle=range(5)
    for i in range(5):
        particle[i]=Ping(i*20+150,i*5+150,i*5+1, i*5+1)
        #print particle[i]
   
       
def draw():
    global particle
           
    for i in particle:
        print i
        i.render()