목록 내에 특정 클래스 변수를 배치하려고했습니다. 각 클래스에는 업데이트라고하는 메소드가 있습니다. 각 클래스는 똑같은 일을한다고 생각합니다. 간단히 목록에 배치하고 그 방법을 호출하는 것이 더 편리 할 것입니다.목록을 통해 클래스 함수 호출
내가 업데이트로 알려져 호출하려고하는 방법이 내가 얻을 오류입니다 :
여기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)
그냥 분명하다. 기능은 존재한다. 나는리스트/배열을 통해 메소드를 호출하는 방법에 대해 갈 것인지 궁금했다. 그렇지 않다면 누군가 올바른 방향으로 나를 가리켜 주시겠습니까? :)
일부 가상 코드는 어떨까요? –
그 코드는 그 오류를주지 않을 것입니다. 실제 코드와 추적 코드를 함께 제공하십시오. –
내가 실제로 사용하고있는 코드를 추가했습니다. 방금 길었을 거라 생각 했어요 – ReallyGoodPie