2

I'm trying to make a pygame file executable with py2exe (I have Python 2.7). The program is composed of 3 scripts.

I wrote the setup as:

from distutils.core import setup
import py2exe
setup(console=["main.py"])

From the command window, I ran the setup:

setup.py py2exe

and I got my program .exe in the dist folder. The problem is, when I run it, a black window opens and immediately closes.

Now, I know that this is normal if the program does not wait for any input, but in my program there is a while loop that should go on until the user closes the game window. I also tried to put at the beginning of the program a raw_input but it changes nothing.

Then I tried to create a very simple file to test:

a = raw_input("hello")
print(a)

I converted it to .exe and it works. So, I guess that the problem is in my pygame file. Here is the code of the main (functions and classes are the other 2 scripts of the game):

import pygame, sys, functions
from classes import *

pygame.init()
pygame.font.init()

display_width = 600
display_height = 600
screen = pygame.display.set_mode((display_width,display_height))
menuImage = pygame.image.load("images/menu_t.jpg")

# colors
backgroundColor = (255,255,102)
buttonColor = (153,76,0)
buttonColorBright = (204,102,0)

# create tiles
for y in range(0,screen.get_height(),Tile.WIDTH):
    for x in range(0,screen.get_width(),Tile.HEIGHT):
        Tile(x,y)

# starting menu
while True:
    screen.blit(menuImage,(0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()

    functions.button(screen,"Play",display_width/2-50,display_height/2,100,50,buttonColor,buttonColorBright,"play_game")
    functions.button(screen,"Quit",display_width/2-50,display_height/2+75,100,50,buttonColor,buttonColorBright,"quit")

    pygame.display.flip()

This is the main, it should load a background image and 2 buttons. What could be the reason the executable program doesn't work properly?

dda
  • 5,760
  • 2
  • 24
  • 34
Adrift
  • 111
  • 1
  • 4
  • Try opening Command Prompt, navigating to the location of your **.exe**, and running it. Then, the window will not immediately close and you can see what errors you are getting. – Remolten Apr 04 '17 at 18:34
  • Ok thanks. I found out that there was an error importing the mixer module. Then I used the setup script I found here: http://www.pygame.org/wiki/Pygame2exe But now there is another problem: when I open the executable program, I get: "Runtime error: This application has requested the Runtime to terminate it in an unusual way. " How can I fix this? – Adrift Apr 04 '17 at 20:28
  • From a quick Google search, that error appears to be specifically related to Windows. I can't really say exactly what is causing it. I would try placing print statements throughout the code, and trying to approximate what line is causing the program to close. – Remolten Apr 05 '17 at 00:33
  • I put a print at the first line of the script, but it is useless. The program just does not open... – Adrift Apr 05 '17 at 20:02
  • Ok, I understood where the error comes from. The problem is related to the font import. Now I will try to manage this. – Adrift Apr 05 '17 at 20:16
  • Try adding **import pygame._view** to your code. This is a common problem when freezing pygame programs into executables. – Remolten Apr 05 '17 at 20:38

0 Answers0