首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我释放鼠标后,游戏中的图片立即消失。

问在我释放鼠标后,游戏中的图片立即消失。
EN

Stack Overflow用户
提问于 2020-07-25 09:14:13
回答 1查看 107关注 0票数 0
代码语言:javascript
复制
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()

这是我的绞刑游戏的一部分,我尝试设置选项部分,允许您配置音量。

问题在于“音乐”和“声音”的效果调整。

按下开关按钮的“音乐”和“声音”将显示关闭开关,但一旦我释放我的鼠标,他们回到原来的状态。

音乐停止了,但没有声音效果(鼠标点击,嗡嗡作响,等等)。

我希望图像闪烁被保存,声音效果被停止。我怎么才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-28 01:04:04

你在每一个循环中撤销blit。在顶部,我看到了清除\重置一切的代码。

代码语言:javascript
复制
win.fill(WHITE)
win.blit(background, (0, 0))
....

您可以在事件处理程序中删除更改:

代码语言:javascript
复制
screen.blit(off_switch, (off_switch_x, off_switch_y))

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

把这个游戏想象成一系列的州。循环顶部的代码应该绘制游戏的当前状态。

代码语言:javascript
复制
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)) 
    ....

事件处理程序应用于更改游戏状态。

代码语言:javascript
复制
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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63086541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档