1

game.py:

import pygame

pygame.init()
if pygame.get_init():
  print("pygame successfully initialized")
else:
  print("pygame failed to initialize")

background_color = (255,0,0)
(width, height) = (800, 800)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Game')

x = int(width / 2)
y = int(height / 2)
speed = 10


running = True
while running:
  screen.fill(background_color)

  # drawing
  pygame.draw.circle(screen, (0,0,255), (x, y), 50)

  #input
  keys = pygame.key.get_pressed()
  if keys[97]:
    x -= speed
  elif keys[100]:
    x += speed
  if keys[119]:
    y -= speed
  elif keys[115]:
    y += speed

  pygame.display.flip()

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

setup.py:

from cx_Freeze import setup, Executable

includefiles = ['PNG']
includes = []
excludes = []
packages = []

setup(
    name = 'game',
    version = '0.1',
    description = 'IDk',
    options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}}, 
    executables = [Executable("game.py")]
)

When I run the setup.py script it makes an executable that does run, however, it just creates an empty pygame window. If I run the script with 'python game.py' in the terminal it works fine. I have seen many people who can't get pygame programs to run with cx_Freeze and the fix is to add import re but that does not fix my problem. I also do not get any errors and the terminal pops up and says the usual 'welcome to pygame' message.

swishy_fishy
  • 13
  • 1
  • 4
  • 1
    There's no call to `pygame.init()` in your sample. Perhaps you've missed this when copying your code to this question. To help you understand what's going on with your executable, you could try running the executable you have generated from a command prompt, then the standard error might print some useful information – import random Oct 03 '19 at 01:50
  • I added pygame.init() (see the edited post) but nothing changed, running the python script does and always has worked as expected and the executable does not. I have always been running the executable from the command line and still get no errors, I'm not sure if it changes anything but I am doing this on mac. I also added debug statements that are left out of the post in various parts of the code and they all get hit but nothing displays on the screen, it's just a blank window. I appreciate your help though. – swishy_fishy Oct 03 '19 at 20:54
  • I've run your `setup.py` on my machine and generated an executable that runs without any errors. Are all of your modules up to date? I'm using `cx-freeze-6.0` and `pygame 1.9.6`. – import random Oct 03 '19 at 23:47
  • @Eric yep I have the same versions of modules and am using python 3, as I'm pretty sure you have to with `cx_freeze`, is that right? I am also able to use my `setup.py` to create executables of other scripts but nothing with pygame. – swishy_fishy Oct 04 '19 at 19:38
  • The next thing I'd try is setting up a clean virtual environment, [e.g. venv](https://docs.python.org/3/tutorial/venv.html). Then only install the modules you require. Otherwise you could look at [PyInstaller](https://www.pyinstaller.org/) as an alternative to cx_freeze. – import random Oct 08 '19 at 22:54

0 Answers0