【问题标题】:Blitting an image in pygame immediately disappears after I release my mouse释放鼠标后,pygame中的图像立即消失
【发布时间】:2020-07-25 09:14:13
【问题描述】:
def options():
options = True

while options:
    for event in pygame.event.get():
        win.fill(WHITE)
        win.blit(background, (0, 0))

        ... # Blitting text and switch buttons

        options_x, options_y = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            # Exit button
            pygame.QUIT()
            quit()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Sound effect tuning
            if 470 > options_x > 390 and 220 > options_y > 185: 
                # Checking if mouse click is on the ON SWITCH
                mouse_click.play()
                screen.blit(off_switch, (off_switch_x, off_switch_y))

                pygame.mixer.stop() 
                # But doesn't stop sound from playing when I quit options section

            # Music effect tuning
            elif 470 > options_x > 390 and 300 > options_y > 260:
                # Checking if mouse click is on the ON SWITCH
                mouse_click.play()
                screen.blit(off_switch, (off_switch_x, off_switch_y))
                pygame.mixer.music.stop()

            # Interactive BACK button
            elif 90 > options_x > 42 and 75 > options_y > 25:
                mouse_click.play()
                options = False

        pygame.display.update()

所以这是我的 HANGMAN 游戏的一部分,我在其中尝试设置 OPTIONS 部分,以便您配置音量。

问题在于“音乐”和“声音”效果调整。
按下“音乐”和“声音”的 ON SWITCH 按钮将显示 OFF SWITCH,但只要我松开鼠标,它们就会恢复到原来的状态。
音乐停止,但音效停止(鼠标点击、扑通声等)。

我想保存图像块传输,并停止声音效果。 我该如何解决这个问题?

【问题讨论】:

    标签: python-3.x pygame


    【解决方案1】:

    您在每个循环中撤消 blit。在顶部,我看到清除\重置所有内容的代码。

    win.fill(WHITE)
    win.blit(background, (0, 0))
    ....
    

    您在事件处理程序中对更改进行 blit:

    screen.blit(off_switch, (off_switch_x, off_switch_y))
    

    事件开关 blit 将在下一个事件循环(可能是鼠标移动)时被清除。

    将游戏视为一系列状态。循环顶部的代码应该绘制游戏的当前状态。

    for event in pygame.event.get():
        win.fill(WHITE)
        win.blit(background, (0, 0))
        if SwitchIsOn: 
            screen.blit(on_switch, (on_switch_x, on_switch_y))
        else:
            screen.blit(off_switch, (off_switch_x, off_switch_y)) 
        ....
    

    应该使用事件处理程序来改变游戏状态。

    if 470 > options_x > 390 and 220 > options_y > 185: 
        # Checking if mouse click is on the ON SWITCH
        mouse_click.play()
        SwitchIsOn = not SwitchIsOn # reverse switch position
    

    这将防止您的事件更改被清除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多