首页 > 解决方案 > 需要帮助获取向上滚动事件

问题描述

我正在使用 Pygame 在 Python 中创建一个程序,但我找不到一种工作方法来检测我何时向上滚动。

我正在尝试重新制作一个名为“星际鼠标”的流行视频 https://www.youtube.com/watch?v=aANF2OOVX40

我试图让它在你滚动时播放相同的音乐(做同样的事情而不必编辑它)。

通过执行以下操作,我已经能够使其与按键一起使用:

keys = pygame.key.get_pressed()

if keys[pygame.K_KEYNAME]:
     pygame.mixer.music.unpause()

仅使用键似乎可以正常工作,但我需要使用鼠标滚轮。

这是主循环:

run = True

while run:
    #quit game after pressing the close button
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    #exit game by pressing ESC
    if keys[pygame.K_ESCAPE]:
        run = False

    #scroll to activate
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 4:
                pygame.mixer.music.unpause()
            else:
                pygame.music.pause()


    #update background
    win.blit(bg, (width / 4, height / 4))
    pygame.display.update()



pygame.quit()

结果非常不一致。有时,如果我滚动得非常快,它会在大约一秒钟后开始播放,但永远不会停止。有时它根本不播放。

标签: pythoneventsscrollpygamemouseevent

解决方案


这部分在这里:

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 4:
            pygame.mixer.music.unpause()
        else:
            pygame.music.pause()

每次你有event.type == pygame.MOUSEBUTTONDOWN你要么暂停或取消暂停音乐。鼠标点击和向下滚动将暂停您的音乐。我会将其更改为特定的暂停选项,if event.button == 2而不是您的else以便更好地控制。


推荐阅读