You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将带音乐的Python 3.6.1俄罗斯方块游戏打包为.exe遇阻求指导

Fixing .exe Packaging for Python 3.6.1 Tetris Game with Audio

Hey there! Let’s work through this packaging headache together—adding audio resources to a Python-to-exe build is one of the most common pain points, especially with an older Python version like 3.6.1. I’ve debugged plenty of these issues, so here’s a tailored guide for both PyInstaller and cx_Freeze that should get your Tetris game running as a standalone .exe.

First, Critical Pre-Checks

Before diving into tools, make sure you’ve covered these basics:

  • Use relative paths for audio: In your game code, avoid hardcoding absolute paths (like C:\Games\music.mp3). Instead, use os.path to reference files relative to your script. For example:
    import os
    audio_path = os.path.join(os.path.dirname(__file__), "music.mp3")
    
  • Match tool versions to Python 3.6.1: Newer versions of PyInstaller/cx_Freeze drop support for older Python releases. Stick to:
    • PyInstaller 3.4 or 3.5
    • cx_Freeze 5.1.1

PyInstaller Step-by-Step Fix

PyInstaller often fails with audio because it doesn’t automatically include non-Python files. Here’s how to force it to bundle your music:

  1. Generate a spec file: Instead of packing directly, create a configuration file first. Run this in your terminal:

    pyi-makespec --onefile your_tetris_game.py
    

    This creates a your_tetris_game.spec file.

  2. Edit the spec file to include audio: Open the spec file and find the datas list. Add your music file(s) here using the format (source_path, destination_folder). For example:

    datas=[
        ("path/to/your/music.mp3", "."),  # Puts music in the same folder as the exe
        # Add other audio files here if needed
    ]
    

    If your game uses pygame’s mixer, also add hidden imports to avoid missing modules:

    hiddenimports=["pygame.mixer", "pygame.mixer_music"]
    
  3. Build using the spec file: Run this command to start the packaging process:

    pyinstaller your_tetris_game.spec
    
  4. Post-build check: Navigate to the dist folder—your .exe and music file should be there. If the music still doesn’t play, run the exe from the command line to see error messages (this will tell you if the path is wrong or a module is missing).


cx_Freeze Step-by-Step Fix

cx_Freeze requires a setup script to define which files to include. Here’s a foolproof setup:

  1. Create a setup.py file: Save this in the same folder as your game script:

    import sys
    import os
    from cx_Freeze import setup, Executable
    
    # List all audio files you need to bundle
    include_files = ["music.mp3"]  # Add more if you have sound effects
    
    # Configure the executable (use Win32GUI if you don't want a console window)
    base = "Win32GUI" if sys.platform == "win32" else None
    game_exe = Executable("your_tetris_game.py", base=base)
    
    setup(
        name="TetrisWithMusic",
        version="1.0",
        description="Your custom Tetris game",
        options={
            "build_exe": {
                "include_files": include_files,
                "packages": ["pygame"]  # Explicitly include your game's dependencies
            }
        },
        executables=[game_exe]
    )
    
  2. Update your game’s audio path: Since cx_Freeze changes the working directory when running the exe, update your code to find the audio file correctly:

    import sys
    import os
    
    if getattr(sys, 'frozen', False):
        # Running as bundled exe
        audio_path = os.path.join(sys._MEIPASS, "music.mp3")
    else:
        # Running as regular script
        audio_path = os.path.join(os.path.dirname(__file__), "music.mp3")
    
  3. Build the exe: Run this command in your terminal:

    python setup.py build
    

    Your bundled game will be in the build folder under a subdirectory named after your OS and Python version.


Troubleshooting Common Issues

  • Audio file missing in build: Double-check the datas (PyInstaller) or include_files (cx_Freeze) lists—typos in file paths are the #1 culprit.
  • Module not found errors: Add missing packages to hiddenimports (PyInstaller) or packages (cx_Freeze). Running the exe from the command line will show exactly which module is missing.
  • Music plays but cuts out: Make sure your audio file is in a format that’s supported by your library (e.g., pygame works best with MP3/WAV). Avoid high-bitrate files that might cause loading issues.

内容的提问来源于stack exchange,提问作者šm98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:11:38