2

I have been following a tutorial on how to make a cx_freeze setup for pygame folders. Here is what I have come up with...

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
    )

However the rest of my files are in folders. Then there are folders inside these folders. For example I have a folder (path directory) C:CaveGame\Sprites and this folder holds many other folders, C:CaveGame\Sprites\Floors, C:CaveGame\Sprites\Lava ect... Then I also have a folder C:CaveGame\Music which holds all my music files and sound effects. How can I get these all to work inside the setup???

Owen Penn
  • 163
  • 9
  • Does this answer your question? [How can I include a folder with cx\_freeze?](https://stackoverflow.com/questions/15079268/how-can-i-include-a-folder-with-cx-freeze) – Kingsley Jul 05 '20 at 23:21
  • I don't know the exact code to include the files whilst including the pygame package :/ But it helps – Owen Penn Jul 06 '20 at 07:28

1 Answers1

3

You just need to include the upper-level directory item in your options dictionary:

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
      )

The above example will include the files assets/images/blah.png and music/sounds/sproiiiing.ogg along with their correct directories. Everything under that top-level folder is pulled into the lib/.

When you go to load these files, it's necessary to work out the exact path to the files. But the normal method to do this does not work with cxFreeze. Referencing the FAQ at https://cx-freeze.readthedocs.io/en/latest/faq.html ~

if getattr(sys, 'frozen', False):
    EXE_LOCATION = os.path.dirname( sys.executable ) # frozen
else:
    EXE_LOCATION = os.path.dirname( os.path.realpath( __file__ ) ) # unfrozen

Obviously you need the modules sys and os.path for this.

Then when loading a file, determine the full path with os.path.join:

my_image_filename = os.path.join( EXE_LOCATION, "assets", "images", "image.png" )
image = pygame.image.load( my_image_filename ).convert_alpha()

EDIT: If you're building under Windows, you will need to also include the Visual C runtime: https://cx-freeze.readthedocs.io/en/latest/faq.html#microsoft-visual-c-redistributable-package . Add include_msvcr to options.

options = { "build_exe": { "include_msvcr", "packages":["pygame"] #... 
Kingsley
  • 14,398
  • 5
  • 31
  • 53