2013-10-11 4 views
1

목록 내에 특정 클래스 변수를 배치하려고했습니다. 각 클래스에는 업데이트라고하는 메소드가 있습니다. 각 클래스는 똑같은 일을한다고 생각합니다. 간단히 목록에 배치하고 그 방법을 호출하는 것이 더 편리 할 것입니다.목록을 통해 클래스 함수 호출

내가 업데이트로 알려져 호출하려고하는 방법이 내가 얻을 오류입니다 :

여기
Traceback (most recent call last): 
    File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 177, in <module> 
    initialize_game() 
    File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 61, in initialize_game 
    start_menu(debugging, screen, clock, image_cache) 
    File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 88, in __init__ 
    self.init_start_screen() 
    File "C:\Users\GoodPie\Desktop\New Game Dev\main.py", line 115, in init_start_screen 
    self.update_group.append[New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button] 
TypeError: 'builtin_function_or_method' object is not subscriptable 

내가

Entities.py를 사용하려고하고있는 코드는 다음과 같습니다

class Quit_Game_Button(Entity): 

    def __init__(self, x, y, image_cache): 
     Entity.__init__(self) 
     self.image_cache = image_cache 
     self.image = function.get_image("images/Quit_Game.png", self.image_cache) 
     self.image.convert() 
     self.rect = Rect(x, y, 150, 30) 

    def update(self, mouse_position): 
     if self.rect.collidepoint(mouse_position): 
      self.image = function.get_image("images/Quit_Game_Hover.png", self.image_cache) 
     else: 
      self.image = function.get_image("images/Quit_Game.png", self.image_cache) 

    def check_click(self, clicked, mouse_position): 
     quit = False 
     if self.rect.collidepoint(mouse_position): 
      if clicked: 
       quit = True 
     return quit 

class Settings_Button(Entity): 

    def __init__(self, x, y, image_cache): 
     Entity.__init__(self) 
     self.image_cache = image_cache 
     self.image = function.get_image("images/Settings_Button.png", self.image_cache) 
     self.image.convert() 
     self.rect = Rect(x, y, 50, 50) 

    def update(self, mouse_position): 
     if self.rect.collidepoint(mouse_position): 
      self.image = function.get_image("images/Settings_Button_Hover.png", self.image_cache) 
     else: 
      self.image = function.get_image("images/Settings_Button.png", self.image_cache) 

main.py

self.update_group.append[New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button] 

mouse_position = pygame.mouse.get_pos() 

#Updating Entities on the screen 
for entity in self.update_group: 
    entity.update(mouse_position) 

그냥 분명하다. 기능은 존재한다. 나는리스트/배열을 통해 메소드를 호출하는 방법에 대해 갈 것인지 궁금했다. 그렇지 않다면 누군가 올바른 방향으로 나를 가리켜 주시겠습니까? :)

+0

일부 가상 코드는 어떨까요? –

+0

그 코드는 그 오류를주지 않을 것입니다. 실제 코드와 추적 코드를 함께 제공하십시오. –

+0

내가 실제로 사용하고있는 코드를 추가했습니다. 방금 길었을 거라 생각 했어요 – ReallyGoodPie

답변

0

문제가를 호출 루프의 실제 내에 있지 않았다 클래스 내에서 update 함수를 호출하면 클래스 인스턴스를 목록에 추가하는 과정이 발생했습니다. 따라서이 문제를 해결하기 위해 시도 :

self.update_group = [New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button] 

그리고 완벽하게 작동합니다. 배열에 추가 할 때 문제가 무엇인지 모르겠지만 고정 된 목록/배열을 갖는 것은 나에게 잘 작동하는 것처럼 보입니다. @Matthias에 대한 크레디트는 실제로 배열에 인스턴스를 추가하는 방법을 보여줍니다.

1

이것은 간단한 오류입니다. 당신은 인덱스 append에 노력하고

self.update_group.extend([New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]) 

당신이 그것을 호출해야하는 위치

self.update_group.append[New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button] 

이 있어야한다. 두 번째 요점은 append에 하나의 매개 변수 만 사용되므로 extend을 사용해야한다는 것입니다.

하지만 여전히 클래스에서 작동하고 있으며 클래스의 인스턴스가 아닙니다.

+0

흠, 내가 처음에 그랬던 것과 그렇지 않다고 믿는다. 그러나이 오류를 반환합니다 :'TypeError : append()는 정확히 하나의 인수 (4가 주어진)를 취합니다. '그래서 self.update_group.append ([New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button])'로 변경했지만이 오류를 반환했습니다. :'AttributeError : 'list'객체에 'update'' 속성이 없습니다. – ReallyGoodPie

+0

죄송합니다. 답변을 수정하십시오. – Matthias

1

당신은 referenced before assignment 오류가 발생 그래서 One

Two 같은 더 적합한 뭔가 클래스 이름을 변경하는 것이, two()one(1)twoone를 할당하려고하면 그것은 엄격recommended입니다 (CapWords 규칙은 pg4 here 참조) 항상 파이썬 클래스 이름을 대문자로 시작하려면

같은

수정 코드가 보일 것이다 : 가 (필자는 STR 방법을 구현했습니다)

class One: 
    def __init__(self, x): 
     self.x = x 

    def update(self): 
     self.x += 1 

    def __str__(self): 
     return str(self.x) 

class Two: 
    def __init__(self, x): 
     self.x = x 

    def update(self): 
     self.x += 1 

    def __str__(self): 
     return str(self.x) 

def test(): 
    one = One(1) 
    two = Two(2) 

    update_list = [one, two] 

    for i in update_list: 
     i.update() 
     print i 

test() 

출력 :

2 
3 
관련 문제