我试图将某些类变量放在列表中。每个类都有一个名为update的方法,并且考虑到它们都做了完全相同的事情。我认为,简单地将它们放在列表中并以这种方式调用方法会更方便。
我试图调用的方法称为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)
很明显,这些功能确实存在。我只是想知道我是否正在通过一个列表/数组来调用方法?如果没有,可以请人指出正确的方向吗?)
发布于 2013-10-11 13:39:57
这个问题并不存在于调用类中update函数的实际for循环中,而是通过将类实例附加到列表中发生的。因此,为了解决这个问题,我尝试了:
self.update_group = [New_Game_Button, Load_Game_Button, Quit_Game_Button, Settings_Button]
而且效果很好。我不知道附加到数组中的问题是什么,但是有一个固定的列表/数组似乎对我来说很好。@Matthias实际上让我查看了将实例添加到数组的方法。
发布于 2013-10-11 13:06:39
这是一个简单的错误。
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
。
但是您仍然在对类进行操作,而不是对类的实例进行操作。
发布于 2013-10-11 13:10:48
您正在尝试将one
分配给one(1)
,将two
分配给two()
,这会导致referenced before assignment
错误,因此将类名更改为更合适的类,如One
和Two
。
此外,严格来说,推荐( CapWords约定见pg4 http://msdl.cs.mcgill.ca/people/shahla/misc/PythonConventions.pdf)总是以大写字符启动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
https://stackoverflow.com/questions/19318833
复制相似问题