I'm currently implementing a feature in my game where a weapon briefly appears in front of the character before disappearing as a means of implementing melee combat. I've been using the space bar to summon the weapon but I'd like to use the left mouse button instead. I have been unable to find how to do that.
Here is the code:
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)
I would assume I need to use pygame.mouse.get_pressed(), which I did by removing the "if event.key == pygame.K_SPACE" and instead of having "pygame.KEYDOWN" I did "pygame.mouse.get_pressed()" but that did not do anything. The code still ran but the weapon was absent.
In conclusion, how do I use left mouse button input to execute the above code?
instead of using pygame.KEYDOWN
, use pygame.MOUSEBUTTONDOWN
. You also have to change event.key == pygame.K_SPACE
to event.button == 1
. Other mouse actions are:
1 - left click
2 - middle click
3 - right click
4 - scroll up
5 - scroll down
If you need the mouse position, you can get it with event.pos
.
Other event information can be found at the pygame docs