2013-10-11 30 views
1

我试图将某些类变量放在列表中。每个类都有一个称为update的方法,并且考虑到它们每个都执行完全相同的事情,尽管将它们放在列表中并以此方式调用方法会更方便。通过列表调用类功能

的方法,我试图调用被称为更新,这是我的错误:

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

问题并未在实际撒谎for循环调用更新函数,它通过将类实例追加到列表中而发生。因此为了解决这个问题我尝试过:

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

它的工作很完美。我不知道附加到数组的问题是什么,但是有一个固定的列表/数组似乎对我来说工作得很好。感谢@Matthias实际上让我看到了将实例添加到数组的方法。

1

这是一个简单的错误。

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

应该

self.update_group.extend([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:'列表'对象没有属性'更新' – ReallyGoodPie

+0

对不起,只是纠正答案。 – Matthias

1

您试图分配oneone(1)twotwo(),这导致referenced before assignment错误,以便改变类名称的东西更适合喜欢OneTwo

而且它是严格recommended (对于CapWords约定,请参阅页面here始终以大写字符开始python类名称

更正代码是这样:(我已经实现了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