You have to reset ble
when the mouse is not over the button or if the mouse is button is released (MOUSEBUTTONUP
- see pygame.event
):
ble = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if speedbutton.isOver(event.pos):
ble = True
elif event.type == pygame.MOUSEBUTTONUP:
ble = False
# if my ble is True then I will keep adding the health bar
if not speedbutton.isOver(pygame.mouse.get_pos()):
ble = False
if ble:
if health1.health < 70:
health1.health += 0.4
Alternatively you can use pygame.mouse.get_pressed()
. pygame.mouse.get_pressed()
returns a list of Boolean values ??that represent the state (True
or False
) of all mouse buttons. The state of a button is True
as long as a button is held down.
Use any
to evaluate whether any mouse button is pressed:
ble = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
mouse_buttons = pygame.mouse.get_pressed()
pos = pygame.mouse.get_pos()
ble = any(mouse_buttons) and speedbutton.isOver(pos)
# if my ble is True then I will keep adding the health bar
if ble:
if health1.health < 70:
health1.health += 0.4
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…