微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我移动鼠标时,健康宽度停止增加

如何解决当我移动鼠标时,健康宽度停止增加

我遇到了一个问题,当我点击按钮时我有一个按钮我增加了我的健康矩形宽度 + 1 问题是当我用鼠标按住按钮时我希望健康矩形保持即使我在窗口周围移动鼠标,也会增加它的宽度,但是一旦我在窗口周围移动鼠标,健康矩形就会停止添加

VIDEO 我做到了,所以如果我停止按住鼠标应该停止添加它,但它会检测到我何时移动鼠标以及如何解决这个问题

在我的主循环中,我说如果我的鼠标在速度按钮上,那么它会继续添加,否则当我不再点击按钮时它应该停止添加但是当我移动鼠标时它也会停止添加我的健康

>
# 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




解决方法

当鼠标未在按钮上或松开按钮时,您必须重置 bleMOUSEBUTTONUP - 参见 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

或者,您可以使用 pygame.mouse.get_pressed()pygame.mouse.get_pressed() 返回一个布尔值列表,代表所有鼠标按钮的状态(TrueFalse)。只要按钮被按住,按钮的状态就是 True

使用 any 评估是否按下了任何鼠标按钮:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。