如何在 trie 结构中动态预加载 Pygame 游戏中我需要的所有声音?

How do i pre load all the sounds that I need in a Pygame game, dynamically in a trie structure?

Pygame 无法打开文件

我尝试打开文件 'with open as',使用 wave.open 作为打开单个文件而不创建字典。像 sound = Pygame.mixer.Sound('sounds/example.wav')Pygame.mixer.init()

之后

spy_vs_spy.py

import pygame
import settings
import game_functions as gf
import resources
import sounds
from spy_white import Spy_white
from spy_black import Spy_black
from settings import Settings


def run_game():

    # Initialize pygame, settings, and screen object.
    pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096)
    pygame.init() #turn all of pygame on.
    pygame.mixer.init()
    print(pygame.mixer.get_init())
    ai_settings = Settings()
    screen = pygame.display.set_mode(
            (ai_settings.screen_width, ai_settings.screen_height), 
            pygame.HWSURFACE|pygame.DOUBLEBUF)
    pygame.display.set_caption("Spy vs Spy 2019")
    resources.load_images(ai_settings, screen)
    resources.load_sounds()

    # Make Spy white.
    spy_white = Spy_white(ai_settings, screen)

    # Make Spy black.
    spy_black = Spy_black(ai_settings, screen)

    # Start music
    sounds.play_music()

    # Start the main loop for the game.
    while True:
        gf.check_events(spy_white, spy_black)
        spy_white.update()
        spy_black.update()
        gf.update_screen(ai_settings, screen, spy_white, spy_black)

run_game()

resources.py

sound_library = {}

def load_sounds():

    global sound_library

    """Load all sounds from subdirectories in a dictionary"""
    for dirpath, dirnames, filenames in os.walk('sounds'):
        for name in filenames:
            if name.endswith('.wav'):
                key = name[:-4]
                snd = pygame.mixer.Sound(os.path.join(dirpath, name))
                sound_library[key] = snd

    for sound in sound_library:
        print(sound)

    print(sound_library)

我打算将所有 wav 文件作为声音预加载到字典中,这样我就可以通过在声音库字典中提供键来按事件调用声音。然后执行 resources.sound_library['sound_to_play'].play()

Traceback(最近调用最后):文件“/Users/ruudkooistra/Desktop/project_folder/spy_vs_spy_2019/spy_vs_spy.py”,第 42 行,在 run_game() 文件“/Users/ruudkooistra/Desktop/project_folder/spy_vs_spy_2019/spy_vs_spy.py”,第 24 行,在run_game resources.load_sounds() 文件“/Users/ruudkooistra/Desktop/project_folder/spy_vs_spy_2019/resources.py”,第 36 行,在 load_sounds snd = pygame.mixer.Sound(os.path.join(dirpath, name) ) pygame.error: 无法打开文件 'sounds/spy_fly.wav' [在 1.3 秒内完成,退出代码为 1]
[cmd: ['/usr/local/bin/python3', '-u', '/Users/ruudkooistra/Desktop/project_folder/spy_vs_spy_2019/spy_vs_spy.py']] [目录: /Users/ruudkooistra/Desktop/project_folder/spy_vs_spy_2019] [路径: /Library/Frameworks/Python.[=83= .7/bin:/Users/ruudkooistra/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin]

当我如下更改代码时,pygame.mixer.music.load(file) 和 .play() 有效,但 Pygame.mixer.Sound(file) 仍然无效。顺便说一句,当我这样做时,有指向存储在字典中的文件的链接,我想将数据动态存储在 trie 结构的堆中。但我是新手,也许有更好的方法来处理 Pygame 游戏中的声音。它适用于图像。

def load_sounds():

global sound_library

"""Load all sounds from subdirectories in a dictionary"""
for dirpath, dirnames, filenames in os.walk('sounds'):
    for name in filenames:
        if name.endswith('.wav'):
            key = name[:-4]
            snd = os.path.join(dirpath, name)
            sound_library[key] = snd

for sound in sound_library:
    print(sound)

print(sound_library)

spy_fly 带电 time_bomb_countdown wave_stick search_01 time_bomb_explode 打 timer_01 pistol_trap_set pistol_trapped 打 bomb_explode 步行 spy_vs_spy_theme found_item trap_set lift_off 门 spy_angel {'spy_fly': 'sounds/spy_fly.wav', 'electrified': 'sounds/electrified.wav', 'time_bomb_countdown': 'sounds/time_bomb_countdown.wav', 'wave_stick': 'sounds/wave_stick.wav', 'search_01': 'sounds/search_01.wav', 'time_bomb_explode': 'sounds/time_bomb_explode.wav', 'beat': 'sounds/beat.wav', 'timer_01': 'sounds/timer_01.wav', 'pistol_trap_set': 'sounds/pistol_trap_set.wav', 'pistol_trapped': 'sounds/pistol_trapped.wav', 'hit': 'sounds/hit.wav', 'bomb_explode': 'sounds/bomb_explode.wav', 'walking' : 'sounds/walking.wav', 'spy_vs_spy_theme': 'sounds/spy_vs_spy_theme.wav', 'found_item': 'sounds/found_item.wav', 'trap_set': 'sounds/trap_set.wav', 'lift_off': 'sounds/lift_off.wav', 'door': 'sounds/door.wav', 'spy_angel': 'sounds/spy_angel.wav'}

是的,我找到了方法,现在一切正常,我可以在任何需要的地方使用 pygame.mixer.Sound().play() 或 pygame.mixer.music.load().play():

resources.py 导入 os 将 pygame 导入为 pg

sound_library = {}

def load_sounds():

global sound_library

"""Load all sounds from subdirectories in a dictionary"""
for dirpath, dirnames, filenames in os.walk('sounds'):
    for name in filenames:
        if name.endswith('.wav'):
            key = name[:-4]
            with open(os.path.join(dirpath, name), "rb") as file:
                buffer = file.read()  
                sound_library[key] = buffer

for sound in sound_library:
    print(sound)