6 Replies - 7233 Views - Last Post: 08 August 2012 - 08:19 AM Rate Topic: -----

#1 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Create executable file from pygame files

Posted 24 July 2012 - 12:19 AM

Hi,
i have been working on a 2d game using pygame. Now that i completed it,
i would like to share it , but the problem is that there is 5 .py files
and lots of images. And i can't ask everyone to install python and pygame to
run this game. So i tried to use py2exe to convert pygame files to .exe,
but it doesn't seem to work. Can anyone help me with the setup.py file?
The one i used is,
 

 
 
try:
    from distutils.core import setup
    import py2exe, pygame
    from modulefinder import Module
    import glob, fnmatch
    import sys, os, shutil
    import operator
except ImportError, message:
    raise SystemExit,  "Unable to load module. %s" % message
 
#hack which fixes the pygame mixer and pygame font
origIsSystemDLL = py2exe.build_exe.isSystemDLL # save the orginal before we edit it
def isSystemDLL(pathname):
    # checks if the freetype and ogg dll files are being included
    if os.path.basename(pathname).lower() in ("libfreetype-6.dll", "libogg-0.dll","sdl_ttf.dll"): # "sdl_ttf.dll" added by arit.
            return 0
    return origIsSystemDLL(pathname) # return the orginal function
py2exe.build_exe.isSystemDLL = isSystemDLL # override the default function with this one
 
class pygame2exe(py2exe.build_exe.py2exe): #This hack make sure that pygame default font is copied: no need to modify code for specifying default font
    def copy_extensions(self, extensions):
        #Get pygame default font
        pygamedir = os.path.split(pygame.base.__file__)[0]
        pygame_default_font = os.path.join(pygamedir, pygame.font.get_default_font())
 
        #Add font to list of extension to be copied
        extensions.append(Module("pygame.font", pygame_default_font))
        py2exe.build_exe.py2exe.copy_extensions(self, extensions)
 
class BuildExe:
    def __init__(self):
        #Name of starting .py
        self.script = "GAME STARTER.py"
 
        #Name of program
        self.project_name = "MyApps"
 
        #Project url
        self.project_url = "about:none"
 
        #Version of program
        self.project_version = "0.0"
 
        #License of the program
        self.license = "MyApps License"
 
        #Auhor of program
        self.author_name = "Me"
        self.author_email = "example@example.com"
        self.copyright = "Copyright (c) 2009 Me."
 
        #Description
        self.project_description = "MyApps Description"
 
        #Icon file (None will use pygame default icon)
        self.icon_file = None
 
        #Extra files/dirs copied to game
        self.extra_datas = []
 
        #Extra/excludes python modules
        self.extra_modules = []
        self.exclude_modules = []
        
        #DLL Excludes
        self.exclude_dll = ['']
        #python scripts (strings) to be included, seperated by a comma
        self.extra_scripts = []
 
        #Zip file name (None will bundle files in exe instead of zip file)
        self.zipfile_name = None
 
        #Dist directory
        self.dist_dir ='dist'
 
    ## Code from DistUtils tutorial at http://wiki.python.org/moin/Distutils/Tutorial
    ## Originally borrowed from wxPython's setup and config files
    def opj(self, *args):
        path = os.path.join(*args)
        return os.path.normpath(path)
 
    def find_data_files(self, srcdir, *wildcards, **kw):
        # get a list of all files under the srcdir matching wildcards,
        # returned in a format to be used for install_data
        def walk_helper(arg, dirname, files):
            if '.svn' in dirname:
                return
            names = []
            lst, wildcards = arg
            for wc in wildcards:
                wc_name = self.opj(dirname, wc)
                for f in files:
                    filename = self.opj(dirname, f)
 
                    if fnmatch.fnmatch(filename, wc_name) and not os.path.isdir(filename):
                        names.append(filename)
            if names:
                lst.append( (dirname, names ) )
 
        file_list = []
        recursive = kw.get('recursive', True)
        if recursive:
            os.path.walk(srcdir, walk_helper, (file_list, wildcards))
        else:
            walk_helper((file_list, wildcards),
                        srcdir,
                        [os.path.basename(f) for f in glob.glob(self.opj(srcdir, '*'))])
        return file_list
 
    def run(self):
        if os.path.isdir(self.dist_dir): #Erase previous destination dir
            shutil.rmtree(self.dist_dir)
        
        #Use the default pygame icon, if none given
        if self.icon_file == None:
            path = os.path.split(pygame.__file__)[0]
            self.icon_file = os.path.join(path, 'pygame.ico')
 
        #List all data files to add
        extra_datas = []
        for data in self.extra_datas:
            if os.path.isdir(data):
                extra_datas.extend(self.find_data_files(data, '*'))
            else:
                extra_datas.append(('.', [data]))
        
        setup(
            cmdclass = {'py2exe': pygame2exe},
            version = self.project_version,
            description = self.project_description,
            name = self.project_name,
            url = self.project_url,
            author = self.author_name,
            author_email = self.author_email,
            license = self.license,
 
            # targets to build
            windows = [{
                'script': self.script,
                'icon_resources': [(0, self.icon_file)],
                'copyright': self.copyright
            }],
            options = {'py2exe': {'optimize': 2, 'bundle_files': 1, 'compressed': True, \
                                  'excludes': self.exclude_modules, 'packages': self.extra_modules, \
                                  'dll_excludes': self.exclude_dll,
                                  'includes': self.extra_scripts} },
            zipfile = self.zipfile_name,
            data_files = extra_datas,
            dist_dir = self.dist_dir
            )
        
        if os.path.isdir('build'): #Clean up build dir
            shutil.rmtree('build')
 
if __name__ == '__main__':
    if operator.lt(len(sys.argv), 2):
        sys.argv.append('py2exe')
    BuildExe().run() #Run generation
    raw_input("Press any key to continue") #Pause to let user see that things ends 



The .exe is made, but after the first screen of the game it shows an error

File "GAME STARTER.py", line 41, in <module>
File "GAME STARTER.py", line 35, in main
File "menu.pyo", line 252, in main
pygame.error: Couldn't open E:\Python27\dist\GAME STARTER.exe\background.jpg
I have no idea why is it showing that error because i have added all the images, and the first image does
work out well, only to show the error for the second image.

Is This A Good Question/Topic? 0
  • +

Replies To: Create executable file from pygame files

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Create executable file from pygame files

Posted 24 July 2012 - 07:26 AM

You might want to try pygame2exe instead.

Actually it looks like you used that resource, I'll take a closer look...

EDIT:
So it says that it's looking for the file in E:\Python27\dist\GAME STARTER.exe\background.jpg

Does that mean that it's literally looking for a folder called "GAME STARTER.exe" and inside that directory it's looking for your background image? Try manually adding that folder and see what happens.

This post has been edited by atraub: 24 July 2012 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Create executable file from pygame files

Posted 24 July 2012 - 09:17 AM

Have you considered distributing your application with portable python rather than as an executable?
Was This Post Helpful? 0
  • +
  • -

#4 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Create executable file from pygame files

Posted 25 July 2012 - 07:19 AM

View Postatraub, on 24 July 2012 - 09:47 PM, said:

Have you considered distributing your application with portable python rather than as an executable?


How do i do that? Can you explain?

View Postatraub, on 24 July 2012 - 07:56 PM, said:

You might want to try pygame2exe instead.

Actually it looks like you used that resource, I'll take a closer look...

EDIT:
So it says that it's looking for the file in E:\Python27\dist\GAME STARTER.exe\background.jpg

Does that mean that it's literally looking for a folder called "GAME STARTER.exe" and inside that directory it's looking for your background image? Try manually adding that folder and see what happens.


Well, "GAME STARTER.exe" is the executable created, and as i said ,i have all the images kept in the same folder.
i think the setup file has to be edited, but i have no idea how.
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Create executable file from pygame files

Posted 25 July 2012 - 08:16 AM

View Postjephindavis, on 25 July 2012 - 10:19 AM, said:

View Postatraub, on 24 July 2012 - 09:47 PM, said:

Have you considered distributing your application with portable python rather than as an executable?


How do i do that? Can you explain?


If you want a step by step guide, I'd suggest you google pygame and portable python. The basic concept is that portable python is a fully functioning install of python that can be distributed with an application. I believe it's based on 2.6, and it does come with pygame. Your users won't need to install python, they'll have it with pp! Sorry, that's the best answer you'll receive while I'm posting from my phone.

Quote

View Postatraub, on 24 July 2012 - 07:56 PM, said:

You might want to try pygame2exe instead.

Actually it looks like you used that resource, I'll take a closer look...

EDIT:
So it says that it's looking for the file in E:\Python27\dist\GAME STARTER.exe\background.jpg

Does that mean that it's literally looking for a folder called "GAME STARTER.exe" and inside that directory it's looking for your background image? Try manually adding that folder and see what happens.


Well, "GAME STARTER.exe" is the executable created, and as i said ,i have all the images kept in the same folder.
i think the setup file has to be edited, but i have no idea how.


I'm aware of all that, but I'm interested in what will happen, can you try my suggestion regardless? It will give me a better idea of what's going on.

This post has been edited by atraub: 25 July 2012 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 jephindavis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 13-November 09

Re: Create executable file from pygame files

Posted 08 August 2012 - 03:11 AM

View Postatraub, on 25 July 2012 - 08:46 PM, said:

View Postjephindavis, on 25 July 2012 - 10:19 AM, said:

View Postatraub, on 24 July 2012 - 09:47 PM, said:

Have you considered distributing your application with portable python rather than as an executable?


How do i do that? Can you explain?


If you want a step by step guide, I'd suggest you google pygame and portable python. The basic concept is that portable python is a fully functioning install of python that can be distributed with an application. I believe it's based on 2.6, and it does come with pygame. Your users won't need to install python, they'll have it with pp! Sorry, that's the best answer you'll receive while I'm posting from my phone.

Quote

View Postatraub, on 24 July 2012 - 07:56 PM, said:

You might want to try pygame2exe instead.

Actually it looks like you used that resource, I'll take a closer look...

EDIT:
So it says that it's looking for the file in E:\Python27\dist\GAME STARTER.exe\background.jpg

Does that mean that it's literally looking for a folder called "GAME STARTER.exe" and inside that directory it's looking for your background image? Try manually adding that folder and see what happens.


Well, "GAME STARTER.exe" is the executable created, and as i said ,i have all the images kept in the same folder.
i think the setup file has to be edited, but i have no idea how.


I'm aware of all that, but I'm interested in what will happen, can you try my suggestion regardless? It will give me a better idea of what's going on.


I tried that but still no use. So i changed the setup.py after going through a book by Andy Harris,Distributing Modules and Programs.
But still my setup.py gives out error while compiling. The new setup.py is
from distutils.core import setup
import py2exe

setup(window = ["SHADOW.py"],
      author="Jephin Davis",
      author_email="jephindavis@gmail.com",
      data_files=[('.', ["11.ttf","13.ttf","menufont.ttf"]),
                  ('frames',["Frame0.png","Frame1.png","Frame2.png",
                             "Frame3.png","Frame4.png","Frame5.png",
                             "Frame6.png","Frame7.png","Frame8.png",
                             "Frame9.png","Frame10.png","Frame11.png",
                             "Frame12.png","Frame13.png","Jump0.png",
                             "Jump1.png","Jump2.png","Jump3.png",
                             "Jump4.png","Jump5.png","Jump6.png",
                             "Jump7.png","Jump8.png","Jump9.png",
                             "Jump10.png","Jump11.png","Jump12.png",
                             "Jump13.png","Shoot1.png","Shoot2.png",
                             "Shoot3.png","Shoot4.png","Shoot5.png",
                             "Shoot6.png","Shoot7.png","Shoot8.png",
                             "Shoot9.png","Shoot10.png","Shoot11.png",
                             "Shoot12.png","Shoot13.png","Shoot14.png",
                             "Shoot15.png","Shoot16.png","Shoot17.png",
                             "Shoot18.png","Shoot19.png","Shoot20.png",
                             "Shoot21.png","Shoot22.png","Shoot23.png",
                             "Stand1.png","Sword1.png","Sword2.png",
                             "Sword3.png","Sword4.png","Sword5.png",
                             "Sword6.png","Sword7.png","Sword8.png",
                             "Sword9.png"])
                 ]
      
      )



the error i am getting in the command prompt is

copying 11.ttf -> E:\Python27\dist\.
copying 13.ttf -> E:\Python27\dist\.
copying menufont.ttf -> E:\Python27\dist\.
creating E:\Python27\dist\frames
error: can't copy 'Frame0.png': doesn't exist or not a regular file
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Create executable file from pygame files

Posted 08 August 2012 - 08:19 AM

First off, that book is called Game Programming: The L Line, The Express Line to Learning, I read it years ago.

As for the other stuff... I'll need to do some reading, I'm afraid it might take me a few days as things have been getting busier at my office and I need to spend more time there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1