Downloading pygame games without pygame?

前端 未结 5 418
既然无缘
既然无缘 2021-01-02 19:05

This is a simple question. I have been making a game, and I wanted some of my friends to be able to download it online. Do they have to have pygame and python installed on t

相关标签:
5条回答
  • 2021-01-02 19:21

    Another way to compile your pygame code into .exe would be to use pyg.exe.

    Hope this helps.

    0 讨论(0)
  • 2021-01-02 19:22

    Same idea as @Kovi Bodek, to try and make Python install pygame. But cleaner.

    try:
        import pygame
    except ImportError:
        print("Attempting to install pygame...")
        try:
            import pip
        except ImportError:
            print("pip not present to install pygame. Exiting...")
            exit()
        else:
            try:
                pip.main(["install", "pygame"])
            except:
                print("Failed to install pygame with pip. Exiting...")
                exit()
            else:
                print("Installed pygame...")
                import pygame
                del pip # Don't contaminate namespace.
    
    0 讨论(0)
  • 2021-01-02 19:30

    Well, you can convert it into a .exe file that will work independently on windows operating systems.

    The .exe file will contain the python interpreter, all the modules like pygame, and if there are any external files like images, sounds, 3d objects etc, they will be embedded inside the .exe file so that the general audience wont need to install any external softwares and this also makes your game hack proof.

    You can see the method of converting it quickly, easily, for free, step-by-step, without learning anything on the answer that I gave here :-

    https://stackoverflow.com/a/65035225/13700467

    Hope you are successful in compiling it and I would like to try your game.

    0 讨论(0)
  • 2021-01-02 19:45

    Yes users need a Python interpreter and a reference to Pygame to execute your program – since your game is written in Python and uses the third party library Pygame.

    That said, you could consider to use

    • cx_Freeze,
    • pyInstaller or for example
    • py2app (for Mac users)

    to freeze your Python scripts into an executable and to make a standalone application which your friends could download and execute without having Python and / or Pygame installed on their computers.

    The Pygame wiki gives you an example how to compile a Pygame app to a standalone windows application using py2exe.

    0 讨论(0)
  • 2021-01-02 19:47

    I'm not sure how to deal with installing python, but I added this bit of code to the beginning of my most recent program which is designed to automatically install pygame on launch through cmd using pip.

    import os
    try:
        import pygame
    except:
        try:
            print("Attempting to install pygame...")
            os.system('py -m pip install pygame')
            import pygame
        except:
            try:
                os.system('python -m pip install pygame')
                import pygame
            except:
                print("Error, failed to install pygame libraries")
                input("Press enter to exit...")
                quit()
    
    0 讨论(0)
提交回复
热议问题