Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

python - Health Width Stops Adding When I Move My Mouse

I am having a problem where I have a button when I click the button I increase my health rect width + 1 the problem is when I am holding the button with my mouse I want health rect to keep adding its width even if I am moving my mouse around the window but as soon as I move my mouse around my window the health rect stops adding

VIDEO I made it so if I stop holding my mouse to should stop adding it but it some how detects when I move my mouse as well how would I fix that

here in my main loop I said if my mouse is over the speedbutton then it will keep adding else it should stop adding when I am not clicking the button anymore but when I move my mouse it stops adding my health as well

# our main loop
ble = False
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            if speedbutton.isOver(pos):
                ble = True

                
        else:
           #[...]
            ble = False



   # if my ble is True then I will keep adding the health bar
    if ble:
        if health1.health < 70:
            health1.health += 0.4





与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...