python - 需要帮助获取向上滚动事件
问题描述
我正在使用 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()
结果非常不一致。有时,如果我滚动得非常快,它会在大约一秒钟后开始播放,但永远不会停止。有时它根本不播放。
解决方案
这部分在这里:
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
以便更好地控制。
推荐阅读
- ruby-on-rails - Rails / PostGIS / RGeo 错误:NoMethodError:未定义方法“工厂”用于 RGeo::GeoJSON::Feature
- javascript - Promise 循环泄漏内存
- rest - REST API 设计 - 获取所有用户,如何处理私人信息?
- android - Android 应用 gradle“'com.android.support.test:runner:1.0.2'”问题
- api - 使用 POSTMAN 提交表单
- python - 无法在 anaconda 3 (python 3)、linux 中安装 pygame
- python - 如何在当前命令行目录中运行 Python 脚本?
- html - 是否可以使 popover 等于变量或函数?
- javascript - 试图让我的输出显示在正文部分,格式包括页眉、正文(带背景)和页脚
- html - 如何将图像幻灯片放在 div 框中?