在我释放鼠标后,在pygame中弹出图像会立即消失


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,但一旦我释放鼠标,它们就会返回到原始状态
音乐停止,但没有声音效果(鼠标点击、扑通一声等(。

我想保存图像闪电战,并停止声音效果。我该怎么解决这个问题?

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

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

您在事件处理程序中闪电式地更改:

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

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

最新更新