Pygame将菜单项绑定到函数(通过单击)

时间:2014-05-06 21:57:04

标签: pygame

为一个课程项目修改Squirrel Eat Squirrel Pygame。尝试使用基本的“开始”,“退出”和“设置”按钮添加菜单。我有按钮,当鼠标滚过它们时它们会亮起来。但是,当我点击它们时,我无法弄清楚如何调用它们各自的功能。我想通过这个来打电话给他们:

def runMenu(self):
    mainloop = True
    while mainloop:

        self.__clock.tick(50)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                mainloop == False
            if event.type == MOUSEBUTTONDOWN:
                for item in self.__items:
                    #print(item)
                    if item.isMouseSelecting(pygame.mouse.get_pos()):
                        print(self.__functions)
                        self.__functions[item]() #HERE <----

我相信这是因为我在if 名称 ==“ main ”(见下文)中使用字典因此它实际上不是字典,而是它的引用它的位置?如果这是有道理的。 (我不太擅长解释它,很抱歉)

if __name__ == "__main__":
    screen = pygame.display.set_mode((640, 480), 0 , 32)

    menuItems = ('Start', 'Quit', 'Settings')
    functions = {'Start': main, 'Quit': terminate, 'Settings': None}

    pygame.display.set_caption('Main Menu')
    game = MainMenu(screen, functions, menuItems)
    game.runMenu()

它在shell中给出的错误:

>>> ================================ RESTART ================================
>>> 
({'Start': <function main at 0x2969ab0>, 'Settings': None, 'Quit': <function terminate at 0x2969cf0>}, '#')
{'Start': <function main at 0x2969ab0>, 'Settings': None, 'Quit': <function terminate at 0x2969cf0>}

Traceback (most recent call last):
  File "/Users/tjleggz/Documents/CS 110/squirrel/squirrel.py", line 501, in <module>
    game.runMenu()
  File "/Users/tjleggz/Documents/CS 110/squirrel/squirrel.py", line 138, in runMenu
    self.__functions[item]() #cant call bc its not same object, just a ref to object or something???@
KeyError: <__main__.MenuItem object at 0x29758c8>
>>> 

谢谢!

编辑:

class MenuItem(pygame.font.Font):
def __init__(self, text, font=None, fontSize=30,
             fontColor=(255, 255, 255), (posX, posY)=(0, 0)):
    pygame.font.Font.__init__(self, font, fontSize) #initializes font module
    self.__text = text
    self.__fontSize = fontSize
    self.__fontColor = fontColor
    self.label = self.render(self.__text, 1, self.__fontColor) #not private??
    self.width = self.label.get_rect().width
    self.height = self.label.get_rect().height
    self.__posX = posX
    self.__posY = posY
    self.position = posX, posY

def set_position(self, x, y):
    self.position = (x, y)
    self.__posX = x
    self.__posY = y

def isMouseSelecting(self, (posX, posY)): #change to conditional?!
    if (posX >= self.__posX and posX <= self.__posX + self.width) and \
    (posY >= self.__posY and posY <= self.__posY + self.height):
        return True
    else:
        return False

def setFontColor(self, rgbTuple):
    self.fontColor = rgbTuple
    self.label = self.render(self.__text, 1, self.fontColor)

1 个答案:

答案 0 :(得分:0)

您的self.items包含MenuItem个对象,而不是字典中键的字符串。您可以通过几种方式解决此问题,例如,您可以使用__text的{​​{1}}属性,我认为您正在寻找它。

更好的方法(而不是像现在这样使用函数字典)可能是将MenuItem函数传递给您制作的每个on_click实例,然后您只需要写

MenuItem

但是你似乎对编程比较陌生,所以你可能只想使用第一个建议。