gpt4 book ai didi

python - 释放按钮时发出声音

转载 作者:行者123 更新时间:2023-12-04 07:44:09 24 4
gpt4 key购买 nike

我已经被这个问题困扰了很长时间了。我有一个按钮类。您已经可以选择按钮,单击按钮,然后释放按钮(这会导致事件发生)。我想在你松开按钮时发出声音,但我不能。我不明白你应该怎么做,我已经尝试了一切。换句话说,我想确保鼠标在已经选择按钮时被按住,然后如果它在那个位置释放,一个名为 release 的属性将在一帧内切换为 True。然后我可以检查 release 是否为 True,然后我会播放声音。我的问题:我需要检查什么条件才能拥有 release属性切换为 True 一帧?这是我的代码结构:

# "mouse_down" is a global for when the mouse is being held down
# "clicked" is a global variable for the first frame at which the mouse is held down
# I also have a "release" global variable for the frame that the mouse stops clicking, this variable isn't currently being used.

class Button:
def __init__(self, pos, size, font, text, colors):
"""
colors[n]

0: fill unselected
1: border unselected
2: text unselected
3: fill selected
4: border selected
5: text selected
6: fill clicked
7: border clicked
8: text
"""

self.pos = pos
self.size = size
self.font = font
self.text = text
self.colors = colors
self.selected = False
self.clicked = False
self.release= False

def update(self):
# If the mouse is hovering on the button
if (pygame.mouse.get_pos()[0] > button.pos[0] and pygame.mouse.get_pos()[0] < button.pos[0] + button.size[0]) and (pygame.mouse.get_pos()[1] > button.pos[1] and pygame.mouse.get_pos()[1] < button.pos[1] + button.size[1]):
self.selected = True

if clicked:
self.clicked = True

if not mouse_down:
self.clicked = False

else:
self.selected = False
self.clicked = False
现在只是为了澄清,让我们假设我用鼠标按住单击按钮,将其拖到按钮的位置,然后松开,释放属性不应打开,因为鼠标需要放在按钮上才能开始。

最佳答案

更好用 pygame.rect.collidepoint 获取鼠标是否与按钮碰撞而不是

if (pygame.mouse.get_pos()[0] > button.pos[0] and pygame.mouse.get_pos()[0] < button.pos[0] + button.size[0]) and (pygame.mouse.get_pos()[1] > button.pos[1] and pygame.mouse.get_pos()[1] < button.pos[1] + button.size[1]):
这个怎么运作:
  • 主循环:获取 MOUSEBUTTONUP事件(释放鼠标按钮时发生)
  • 将此事件发送到按钮
  • 如果实际鼠标也发送点击 ( MOUSEBUTTONDOWN ) 发生在按钮
  • 如果两个事件都为 True,则播放声音

  • 在按钮类中:
  • __init__ : 生成碰撞区域+文本Surface
  • update : 绘制矩形,然后将文本居中,然后检查是否碰撞 + 事件 + 按下按钮

  • 类(class) Button :
    class Button:
    def __init__(self, pos, size, font, text, color):
    self.rect = Rect(pos, size) #
    self.text = font.render(text, 0, color) # text surface
    self.button_down_on = False

    def update(self, release_event):
    # draw the button
    pygame.draw.rect(screen, (100, 100, 100), self.rect)
    width, height = self.text.get_size()
    screen.blit(self.text, (int(self.rect.x + self.rect.width / 2 - width / 2),
    int(self.rect.y + self.rect.height / 2 - height / 2)))

    # check collisions
    if release_event and self.rect.collidepoint(pygame.mouse.get_pos()):
    if self.button_down_on: # if the mouse began to press on this button
    print('play sound')
    脚本
    import pygame
    from pygame.locals import *

    pygame.init()
    screen = pygame.display.set_mode((640, 480))

    button = Button((10, 10), (200, 40), pygame.font.SysFont('consolas', 20), 'Test', (127, 255, 255))

    running = True
    while running:
    release_event = False
    for event in pygame.event.get():
    if event.type == QUIT:
    pygame.quit()
    exit()
    elif event.type == MOUSEBUTTONDOWN:
    # check if the click begin on a button
    button.button_down_on = button.rect.collidepoint(event.pos)
    elif event.type == MOUSEBUTTONUP:
    release_event = True # send this event to Button.update

    screen.fill((255, 255, 255))
    button.update(release_event)
    pygame.display.flip()

    关于python - 释放按钮时发出声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67287901/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com