我一直在遵循有关如何为pygame文件夹进行cx_freeze设置的教程。这是我想出的...
import cx_Freeze
executables = [cx_Freeze.Executable("mainGame - Copy.py")]
cx_Freeze.setup(
name = "Cave",
version = "1.0",
author = "Owen Pennington",
options = {"build_exe": {"packages":["pygame"], "include_files":["floor_heart.wav"]}},
executables = executables
)
但是我的其余文件都在文件夹中。然后在这些文件夹中有文件夹。例如,我有一个文件夹(路径目录)C:CaveGame\Sprites
,该文件夹包含许多其他文件夹,C:CaveGame\Sprites\Floors
,C:CaveGame\Sprites\Lava
等...然后我还有一个文件夹C:CaveGame\Music
,其中存放我所有的音乐文件和声音效果。我如何才能在设置中使用所有这些功能?
答案 0 :(得分:2)
您只需要在options
字典中包含上级目录项:
setup(name='Widgets Test',
version = '1.0',
description = 'Test of Text-input Widgets',
author = "Fred Nurks",
options = { "build_exe": {"packages":["pygame"], "include_files":["assets/", "music/"] } },
executables = executables
)
上面的示例将包含文件assets/images/blah.png
和music/sounds/sproiiiing.ogg
以及正确的目录。该顶级文件夹下的所有都被拉到lib/
中。
当您加载这些文件时,有必要计算出文件的确切路径。但是执行此操作的常规方法不适用于cxFreeze。在https://cx-freeze.readthedocs.io/en/latest/faq.html〜
引用FAQ。if getattr(sys, 'frozen', False):
EXE_LOCATION = os.path.dirname( sys.executable ) # frozen
else:
EXE_LOCATION = os.path.dirname( os.path.realpath( __file__ ) ) # unfrozen
显然,您需要为此使用模块sys
和os.path
。
然后在加载文件时,用os.path.join
确定完整路径:
my_image_filename = os.path.join( EXE_LOCATION, "assets", "images", "image.png" )
image = pygame.image.load( my_image_filename ).convert_alpha()
编辑:如果要在Windows下构建,则还需要包括Visual C运行时:https://cx-freeze.readthedocs.io/en/latest/faq.html#microsoft-visual-c-redistributable-package。将include_msvcr
添加到options
。
options = { "build_exe": { "include_msvcr", "packages":["pygame"] #...