如何使用鼠标左键而不是 space 键来触发此事件?

How do I cause this event to occur using the left mouse button instead of the space key?

我目前正在我的游戏中实现一项功能,即武器在消失之前短暂出现在角色面前,作为实现近战的一种方式。我一直在使用 space 栏来召唤武器,但我想改用鼠标左键。我一直无法找到如何做到这一点。

代码如下:

if event.type == pygame.KEYDOWN and self.hasShovel == True:
            if event.key == pygame.K_SPACE:
                if self.player.facing == 'up':
                    shovel(self, self.player.rect.x, self.player.rect.y - TileSize)
                    
                if self.player.facing == 'down':
                    shovel(self, self.player.rect.x, self.player.rect.y + TileSize)
                    
                if self.player.facing == 'left':
                    shovel(self, self.player.rect.x - TileSize, self.player.rect.y)
                    
                if self.player.facing == 'right':
                    shovel(self, self.player.rect.x + TileSize, self.player.rect.y)

我假设我需要使用 pygame.mouse.get_pressed(),我通过删除“if event.key == pygame.K_SPACE”而不是“[=23=”来做到这一点]" 我做了 "pygame.mouse.get_pressed()" 但那没有做任何事情。代码仍然 运行 但武器不见了。

综上所述,如何使用鼠标左键输入来执行上面的代码?

不使用 pygame.KEYDOWN,而是使用 pygame.MOUSEBUTTONDOWN。您还必须将 event.key == pygame.K_SPACE 更改为 event.button == 1。其他鼠标操作是:

1 - left click
2 - middle click
3 - right click
4 - scroll up
5 - scroll down

如果需要鼠标位置,可以用event.pos获取。 其他活动信息可以在 pygame docs

找到