Code/NodeBox

progress_class

maetel 2007. 7. 12. 19:05
progress 예제를 class를 써서 코딩하기

#1. 클래스 멤버를 동적으로 호출한다.
class Progress:
    def __init__(self):
        self.f= 0
        self.x= 0
        self.y= 0
                    
    def march(self):
        self.f+=1
        self.x = self.f*10
        
        if self.x > WIDTH:
            self.f = 0
            self.y += 50
        if self.y > HEIGHT:
            self.f = 0    
            self.y = 0
        
    def x(self):
        return self.x

    def y(self):
        return self.y
        
p = Progress()
#print p.march()

size(200,200)
speed(20)

def setup():
    global p
    p = Progress()
     
def draw():    
    global p
    p.march()

    fill(0)
    rect (p.x, p.y, 10, 10)          
 

#2. draw()에서 Progress.march()를 호출하므로 y의 초기값을  밖으로 빼 주어야 한다.
이 때, x는 self.f를 매개변수로 하므로 method 안에서 선언해도 상관이 없다.
class Progress:
    def __init__(self):
        self.f = 0
        self.y = 0
        
    def march(self):
        x = self.f*10
        self.f += 1
       
        if x > WIDTH:
            self.f = 0
            self.y+=50
           
        if self.y > HEIGHT:
            self.f = 0   
            self.y=0
           
        rect (x, self.y, 10, 10)


p = Progress()

size(200,200)
speed(20)

def setup():
    global p
    p = Progress()
    
def draw():   
    global p

    fill(0)     
    p.march()

class 안의 method에서 선언된 변수는 외부에서 직접 호출할 수 없다. 이에 대해 더 확실히 정리할 것!