Menu
CoddyTech

暂停音乐

本课属于 Coddy Pygame 学习旅程的“音效与游戏体验”单元。第 7 课,共 23 课。

游戏暂停时,音乐也应该暂停;恢复游戏时,音乐应从暂停的位置继续播放。音乐播放器有两个用于此操作的调用:

pygame.mixer.music.pause()
pygame.mixer.music.unpause()

pause()会让曲目停在当前位置。unpause()会从那里继续播放。不要用stop()和play()来实现这一点:play()会从头重新播放曲目,这样每次暂停时,玩家都会听到开头的几个小节。

在暂停状态发生变化,也就是 P 切换paused时调用它们:

paused = not paused
if paused:
    pygame.mixer.music.pause()
else:
    pygame.mixer.music.unpause()

暂停时正在播放的音效会自行播放完,它们都很短。游戏暂停后不会播放新的音效,因为播放音效的代码不会运行。

challenge icon

挑战

中等

让音乐随游戏一起暂停。

  1. 按下 P 暂停时,调用 pygame.mixer.music.pause()。
  2. 再次按下 P 恢复时,调用 pygame.mixer.music.unpause()。

按下“运行”,再按 P:音乐会停止,再按一次 P 即可继续播放。然后点击“检查”。

亲手试试

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Lava Run")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)


def play_music(path, loops=-1):
    pygame.mixer.music.load(path)
    pygame.mixer.music.play(loops)


play_music("assets/music/theme.ogg")

player = pygame.Rect(40, 160, 30, 30)
goal = pygame.Rect(420, 160, 30, 30)
lava = pygame.Rect(0, 320, 480, 40)
state = "menu"
paused = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_p:
            if state == "play":
                paused = not paused
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
            if state == "menu":
                state = "play"
                play_music("assets/music/level.ogg")
                player.topleft = (40, 160)
            elif state in ("win", "lose"):
                state = "menu"
                play_music("assets/music/theme.ogg")

    if state == "play" and not paused:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            player.x -= 4
        if keys[pygame.K_RIGHT]:
            player.x += 4
        if keys[pygame.K_UP]:
            player.y -= 4
        if keys[pygame.K_DOWN]:
            player.y += 4
        player.clamp_ip(screen.get_rect())
        if player.colliderect(goal):
            state = "win"
            play_music("assets/music/win.ogg", 0)
        elif player.colliderect(lava):
            state = "lose"
            play_music("assets/music/lose.ogg", 0)

    screen.fill((30, 30, 46))
    if state == "menu":
        title = font.render("Press Enter to start", True, (240, 240, 240))
        screen.blit(title, title.get_rect(center=(240, 180)))
    else:
        pygame.draw.rect(screen, (230, 90, 40), lava)
        pygame.draw.rect(screen, (90, 200, 120), goal)
        pygame.draw.rect(screen, (80, 160, 255), player)
        if state == "win":
            message = font.render("You win!", True, (240, 240, 240))
            screen.blit(message, message.get_rect(center=(240, 100)))
        elif state == "lose":
            message = font.render("Game over", True, (240, 240, 240))
            screen.blit(message, message.get_rect(center=(240, 100)))
        elif paused:
            message = font.render("Paused", True, (240, 240, 240))
            screen.blit(message, message.get_rect(center=(240, 100)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

音效与游戏体验中的所有课

自己练习: Pygame 在线编辑器