使用游戏手柄在 pacman pygame 中需要按住键才能移动

Require holding key down for movement in pacman pygame with game pad

我一直在修改来自 here 的 PACMAN pygame 代码。目前,我有一个功能,要求玩家按住方向键才能继续朝那个方向移动。代码是:

def process_events(self):
        for event in pygame.event.get(): # User did something
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE: # exit program entirely
                    self.run_over = True
                    return True
                elif event.key == pygame.key_r:
                    self.player.move_right()

                elif event.key == pygame.key_l:
                    self.player.move_left()

                elif event.key == pygame.key_u:
                    self.player.move_up()

                elif event.key == pygame.key_d:
                    self.player.move_down()

            elif event.type == pygame.KEYUP:
                if event.key == pygame.key_r:
                    self.player.stop_move_right()
                elif event.key == pygame.key_l:
                    self.player.stop_move_left()
                elif event.key == pygame.key_u:
                    self.player.stop_move_up()
                elif event.key == pygame.key_d:
                    self.player.stop_move_down()

        return False

虽然这对于按下键盘按钮非常有效,但我正在尝试整合一个游戏手柄,它使用大写字母来指定按键按下按钮按下,小写字母用于指定按键按下。例如,向下移动时,keypress down 为“D”,keypress up/release 为“d”。在游戏手柄上使用上面的相同功能导致我不得不按下 mash 按钮才能勉强移动。因此,我将函数修改为:

def process_events(self):
        for event in pygame.event.get(): # User did something
            try:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE: # exit program entirely
                        self.run_over = True
                        return True
                    elif event.key == pygame.key_r:
                        self.player.move_right()
    
                    elif event.key == pygame.key_l:
                        self.player.move_left()

    
                    elif event.key == pygame.key_u:
                        self.player.move_up()
    
                    elif event.key == pygame.key_d:
                        self.player.move_down()
            except:   
                self.player.stop_move_right()
                self.player.stop_move_left()
                self.player.stop_move_up()
                self.player.stop_move_down()                

        return False

但是,使用这个新代码,我只需轻按一个方向按钮,然后无限期地朝那个方向移动,直到我按下另一个按钮。有没有办法修改我的代码,让玩家需要按住游戏手柄方向按钮才能移动,一旦松开按钮他们就会停止?

感谢@qouify,我意识到我修改的功能设置不正确。因此,我将其调整为此,现在执行正确的功能:

def process_events(self):
        counter = []
        for event in pygame.event.get(): # User did something
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE: # exit program entirely
                    self.run_over = True
                    return True
                else: # direction movement
                    if event.key in [pygame.K_LSHIFT, pygame.K_r, pygame.K_l, pygame.K_u, pygame.K_d]:
                        counter.append(pygame.key.name(event.key))
                    # right
                    if len(counter) == 2 and counter[0] == "left shift" and counter[1] == "r":
                        self.player.move_right()
                    elif len(counter) == 1 and counter[0] == "r":
                        self.player.stop_move_right()
                    # left
                    if len(counter) == 2 and counter[0] == "left shift" and counter[1] == "l":
                        self.player.move_left()
                    elif len(counter) == 1 and counter[0] == "l":
                        self.player.stop_move_left()
                    # up
                    if len(counter) == 2 and counter[0] == "left shift" and counter[1] == "u":
                        self.player.move_up()
                    elif len(counter) == 1 and counter[0] == "u":
                        self.player.stop_move_up()
                    # down
                    if len(counter) == 2 and counter[0] == "left shift" and counter[1] == "d":
                        self.player.move_down()
                    elif len(counter) == 1 and counter[0] == "d":
                        self.player.stop_move_down()
        return False

我意识到这可能不是最优雅或“pythonic”的解决方案,在这种情况下,我很高兴任何人都可以提供建议。否则,我会认为这已经解决了。