블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
  • total
  • today
  • yesterday

Category

2007. 7. 12. 19:05 Code/NodeBox
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에서 선언된 변수는 외부에서 직접 호출할 수 없다. 이에 대해 더 확실히 정리할 것!

'Code > NodeBox' 카테고리의 다른 글

splash_class_multi  (0) 2007.07.13
splash_class  (0) 2007.07.13
popping_1.1  (0) 2007.07.09
popping_1.0 (수정 중)  (0) 2007.07.06
popping_0.0  (0) 2007.07.06
posted by maetel