How to get pygame to recgonize only a single mouse click?

Viewed 34

I have a simple game and I want my mouse click (button doesnt matter) to only register once per click. Right now it works even if it is held down.

def drawer(x, y):
    pygame.draw.circle(screen, (0, 0, 0), [int(x), int(y)],20)

def main():
 running = True
    while running:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                playerY_change = -player.vy
            if event.type == pygame.MOUSEBUTTONUP:
                playerY_change = player.vy
   player.y += PlayerY_change
   drawer(player.x, player.y)
   
   # Other game code
   pygame.display.update()
    

Right now the players movement keeps changing as long as you hold down the button where I want it to change only per click. How do I go about this?

1 Answers

Once the playerY_change is set, the player moves continuously. Therefore do not set playerY_change, but change the player.y when you click with the mouse:

def main():
 running = True
    while running:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                player.y -= player.vy
            
   drawer(player.x, player.y)
   
   # Other game code
   pygame.display.update()

pygame.quit()
exit()
Related