1

I created python game with pygame from book called 'Python crash course'. It has 9 .py file (1 to run game, other 8 for modules like: settings, game_functions, etc.) and one folder where are 2 image files. I want to share this project with my friends who don't have python and pygame, so I want to convert entire project to .exe (windows executable)

I have tried cx_Freeze, py2exe and pyinstaller. Pyinstaller was the most successful one, It created .exe file but however when I run it it's getting black screen and getting closed. I think the reason is that it can't reach modules (8 .py files) and/or image files that I'm using.

Here is main .py code, whats it importing and stuff..

import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button


def run_game():
        pygame.init()
...

If you need any additional information feel free to ask!

HK boy
  • 1,398
  • 11
  • 17
  • 25

1 Answers1

1

First of all, let me recommand you to use Markdown whenever you have code to show; it is really simple, and makes it really easier to read. For example, add ```python before your code and ``` at the end. Code will be formatted and will have syntax highlighting. For example, instead of being shown on one single line, your code becomes:

import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button

def run_game(): pygame.init() ...

Now, onto your issue. According to the cx_Freeze documentation:

Different bases serve for different types of application on Windows (GUI, console application or service).

With cx_Freeze, you need to specify the application is a GUI (Graphical User Interface) in the setup.py file, like:

import sys
from cx_Freeze import setup, Executable

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "guifoo",
        version = "0.1",
        description = "My GUI application!",
        executables = [Executable("guifoo_main.py", base=base)])

I haven't checked all of the other "python to standalone app converters", but I guess there might be something similar that needs to be changed for the app to be executed correctly, and not as a console app. When this kind of issues happen, I suggest you to take the time to read it's documentation carefully.

Hope that helps!

  • 1
    Thanks for your response! It helped me a lot. Sorry for making this post uncomfortable to read, I'm new to stackoverflow and did not know Markdown function. The only thing I needed to add to your code is including image files since cx_Freeze could not reach these files by itself so included files with options={'build_exe': {'include_files': include_files}}, – Giorgi Papava Jun 07 '19 at 17:30