Skip to main content
 首页 » 编程设计

python之函数覆盖另一个函数

2025年05月04日9lonelyxmas

我的程序有问题,基本上我们必须将机器人讨价还价作为一个项目,并在最后添加一个单独的功能。我的想法是添加一个选项来更改 ai 的模型(我知道很简单,但我还是个菜鸟)。有 3 个主要文件,一个用于主菜单,一个用于 Sprite 选择,一个用于实际游戏。我正在使用 pygame,问题出在 Sprite 选择 GUI 上,您可以在其中按 1、2 或 3 来加载模型并开始游戏。问题是当按下 1、2 或 3 时没有任何反应。

class Environment(): 
 
    def __init__(self): 
        self.surface = graphics.SCREEN 
        self.gui_group = pygame.sprite.Group() 
        bckImg = pygame.image.load('map1.png') # loading an image of the map 
        pygame.font.init()   # font initialisation 
        myfont = pygame.font.Font('font1.ttf', 65)  # choosing the font 
        myfont1 = pygame.font.Font('font1.ttf', 40) 
        myfont2 = pygame.font.Font('font1.ttf', 30) 
        myfont3 = pygame.font.Font('font1.ttf', 32) 
        title = myfont.render("IT BELONGS IN THE MUSEUM !", 1, (0,0,0)) 
        startTheGame = myfont1.render("PRESS 'ENTER' TO START", 1, (0,200,0)) 
        closeTheMenu = myfont2.render("PRESS 'Q' TO QUIT", 1, (180,0,0)) 
        chooseTheSprite = myfont3.render("PRESS 'C' TO CHANGE THE AI MODEL", 1, (0,100,0)) 
        self.surface.blit(bckImg, (0,0))  #blitting the background 
        self.surface.blit(title, (375,80)) #blitting the title 
        self.surface.blit(startTheGame, ((500),700)) 
        self.surface.blit(closeTheMenu, ((635),885)) 
        self.surface.blit(chooseTheSprite, ((450,800))) 
 
 
    def process_input(self): 
        for event in pygame.event.get(): # Menu control 
            if event.type == pygame.KEYDOWN:  
                if (event.key == K_RETURN): 
                    print("START GAME") 
                    main.start() 
                elif (event.key == K_c): 
                    spriteGUI.SpriteGui() 
 
                elif (event.key == K_q): # Quit game 
                    pygame.display.quit() 
                    pygame.quit() 
                    sys.exit() 
 
    def update(self): 
        pass 
 
    def render(self): 
        self.gui_group.draw(self.surface) 
        pygame.display.flip() 

这是主菜单按键事件函数

class SpriteGui(): 
 
    def __init__(self): 
        self.surface = graphics.SCREEN 
        self.gui_group = pygame.sprite.Group() 
        bckImg = pygame.image.load('map1.png') # loading an image of the map 
        player1 = pygame.image.load('player1.png') 
        player2 = pygame.image.load('player2.png') 
        player3 = pygame.image.load('player3.png') 
        pygame.font.init()   # font initialisation 
        myfont = pygame.font.Font('font1.ttf', 65)  # choosing the font 
        title = myfont.render("CHOOSE THE MODEL ", 1, (0,0,0)) 
        choosePlayer1 = myfont.render("[1]", 1, (0,51,0)) 
        choosePlayer2 = myfont.render("[2]", 1, (0,51,0)) 
        choosePlayer3 = myfont.render("[3]", 1, (0,51,0)) 
        self.surface.blit(bckImg, (0,0))  #blitting the background 
        self.surface.blit(title, (375,80)) #blitting the title 
        self.surface.blit(choosePlayer1, (315, 700)) 
        self.surface.blit(choosePlayer2, (745, 700)) 
        self.surface.blit(choosePlayer3,(1165, 700)) 
        self.surface.blit(player1, (270,400)) 
        self.surface.blit(player2, (700,400)) 
        self.surface.blit(player3, (1120, 400)) 
 
 
    def process_inputs(self): 
        for event in pygame.event.get(): # Menu control 
            if event.type == pygame.KEYDOWN: 
                if (event.key == K_1): 
                    config.playerCh = 'player1.1.png' 
                    print("Yo") 
                elif (event.key == K_2): 
                    config.playerCh = 'player2.1.png' 
                    print("Yo2") 
                elif (event.key == K_3): 
                    config.playerCh = 'player3.1.png' 
                    print("yo3") 
                elif (event.key == K_q): 
                    pygame.display.quit() 
                    pygame.quit() 
                    sys.exit() 
 
    def update(self): 
        pass 
    def render(self): 
        self.gui_group.draw(self.surface) 
        pygame.display.flip() 

这是 Sprite 选择键事件函数。 一旦进入 sprite 选择菜单,顶部功能的关键事件仍然有效,但第二个功能的关键事件由于某种原因不起作用。我的想法是第一个功能以某种方式覆盖了第二个功能,但不确定如何解决。只是说这两个函数在两个不同的文件中的两个不同的类中。非常感谢您的帮助,正如我之前所说,我是一名菜鸟,所以我的编程理解不是最好的。

谢谢

请您参考如下方法:

pygame.event.get() 将从事件队列中移除所有事件。这意味着当您第一次调用它时,它会删除这些事件,然后您会迭代它们。如果事件类型不在您的 if/elif 部分中,它将跳过它。

随后,当您第二次调用 pygame.event.get() 时,所有先前的事件(即使它们未被执行)都已被删除,因此,只有自那以后发生的新事件将在队列。

我建议使用可选参数类型或类型列表来仅指定您当时想要解析的事件。

参见:http://www.pygame.org/docs/ref/event.html#pygame.event.get