![]() |
Problems with executing .exe file (was .py) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Problems with executing .exe file (was .py) (/thread-17410.html) |
Problems with executing .exe file (was .py) - SheeppOSU - Apr-10-2019 I turned a .py file into .exe file. It works just fine on python but won't run as a .exe file. I don't know why. When I click on it, it opens up a screen, but says "Failed to execute script HyperSpace!" I don't know what to do. I used the method - https://www.youtube.com/watch?v=OZSZHmWSOeM - because I just could get cx_Freeze to work. Here's the code for my game. Quick disclaimer - If you run it... brace yourself to become blind. #Imports import pygame import time import random from functools import partial pygame.init() #Variables/Shortcuts pd = pygame.display fps = 100 display_width = 1000 display_height = 600 black = (0,0,0) white = (255,255,255) red = (255,0,0) darker_red = (200,0,0) green = (0,255,0) blue = (0,0,255) ColorList = [black, red, green, blue] CustomColor1 = (48,150,140) CustomColor2 = (36,112.5,105) CustomColor3 = (0,0,0) Unique_Color = (190,140,210) Score = 0 highscore = 8050 carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCar.png') carImgLeft = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarLeft.png') carImgRight = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarRight.png') car_width = 55 car_height = 50 #Game Start up gD = pygame.display.set_mode((display_width,display_height)) #Game Conifgurations pd.set_caption('HyperSpace!') clock = pygame.time.Clock() gD.fill(white) #Functions def car(img, x, y): gD.blit(img, (x,y)) def CrashDisplay(score, val): gD.fill(random.choice(ColorList)) if val == True: message_display('You Crashed. Your Score was %s' %score) time.sleep(10) else: message_display2('Your score is - %s - Highscore is %s' %(score, highscore)) def message_display(text): font = pygame.font.Font('freesansbold.ttf', 50) textSurf, TextRect = text_objects(text, font) TextRect.center = ((display_width/2),(display_height/2)) gD.blit(textSurf, TextRect) pygame.display.update() time.sleep(10) def message_display2(text): font = pygame.font.Font('freesansbold.ttf', 30) textSurf, TextRect = text_objects(text, font) TextRect.center = ((display_width/2),(20)) gD.blit(textSurf, TextRect) pygame.display.update() def ScoreKeeper(pts): global Score Score += pts def text_objects(text, font): textSurface = font.render(text, True, Unique_Color) return textSurface, textSurface.get_rect() def obstacle(obstX, obstY, obstW, obstH, color): pygame.draw.rect(gD, color, [obstX, obstY, obstW, obstH]) def StartScreen(): intro = True while intro == True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() gD.fill(white) font = pygame.font.Font('freesansbold.ttf', int(display_width/10)) textSurf, TextRect = text_objects('HyperSpace!', font) TextRect.center = ((display_width/2),(display_height/2)) gD.blit(textSurf, TextRect) Button(round(display_width * .3), round(display_height * .7), round(display_width * .4), round(display_height * .2), CustomColor1, CustomColor2, 'Start!', 50, blue, 'freesansbold.ttf', game_loop) Button(round(display_width * .8), round(display_height * .8), round(display_width * .2), round(display_height * .1), red, darker_red, "Quit", 20, blue, 'freesansbold.ttf', quit, pygame.quit) pygame.display.update() clock.tick(15) def Button(Butx, Buty, Butx2, Buty2, Butcolor, ShadowColor, text, textsize, textcolor, textFont, command=None, command2=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() smallText = pygame.font.Font(textFont, textsize) textSurf, textRect = text_objects(text, smallText) textRect.center = ((Butx + (Butx2/2)), Buty + (Buty2/2)) pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) if Butx + Butx2 > mouse[0] > Butx and Buty + Buty2 > mouse[1] > Buty: pygame.draw.rect(gD, ShadowColor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) if click[0] == 1: if command != None: command() if command2 != None: command2() else: pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2)) gD.blit(textSurf, textRect) def Pause(): pause = True message_display('Game is Paused') while pause: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: pause = False #Main def game_loop(): global carImg play = True global Score x = (display_width * 0.5) y = (display_height * 0.8) x_change = 0 obst_startX = random.randint(0, display_width) obst_startY = -600 obst_speed = 2 min_width = 100 max_width = 100 obst_width = 0 obst_height = 0 obst_height = random.randint(round(display_width/8), round(display_width/2)) obst_color = random.choice(ColorList) while play: for event in pygame.event.get(): if event.type == pygame.QUIT: StartScreen() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -5 carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarLeft.png') elif event.key == pygame.K_RIGHT: x_change = +5 carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarRight.png') elif event.key == pygame.K_p: Pause() if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: x_change = 0 carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCar.png') Score += 1 x += x_change CrashDisplay(Score, False) obstacle(obst_startX, obst_startY, random.randint(min_width, max_width), obst_height, obst_color) obst_startY += obst_speed car(carImg, x, y) if x > display_width - 76 or x < -5: play = False if obst_startY > display_height: obst_startY = 0 - obst_height obst_startX = random.randint(0, display_width) obst_width = random.randint(min_width, max_width) obst_height = random.randint(round(display_width/8), round(display_width/2)) obst_color = random.choice(ColorList) min_width += 10 max_width += 11 if y <= obst_startY + obst_height: if x > obst_startX and x < obst_startX + obst_width or x + car_width > obst_startX and x + car_width < obst_startX + obst_width: CrashDisplay(Score, True) StartScreen() if Score == 2000: obst_speed = 3 elif Score == 4000: obst_speed = 4 elif Score == 6000: obst_speed = 5 elif Score == 8000: obst_speed = 6 elif Score == 10000: obst_speed = 7 elif Score == 12000: obst_speed = 8 elif Score == 14000: obst_speed = 9 elif Score == 16000: obst_speed = 10 elif Score == 18000: obst_speed = 11 elif Score == 20000: obst_speed = 12 pygame.display.update() clock.tick(fps) StartScreen() pygame.quit() quit() RE: Problems with executing .exe file (was .py) - j.crater - Apr-10-2019 The error message you got is a generic one, some error/exception happened. It could be something like a missing DLL, or script not able to find some graphics library (path...) etc. Logging to file or console is one way to attempt debugging. But I don't really know PyGame, there may be more appropriate ways for this. RE: Problems with executing .exe file (was .py) - metulburr - Apr-10-2019 pyinstaller is now the preferred method of building an exe. This is more updated and you probably would not have so many problems. Since that program uses pyinstaller, i guess its OK. I never heard of it before now. Did you include additional directories for you font directory and image directories? Before you hit convert .py to .exe button, post what is in that current command (all of it). We need to know how you are building your exe. Also post your output that you get from that exe being built (all of it). You can give the argument --debug to write progress messages in the output. Do this before copying the output for us. I would start small and work your way up when building exe files form py. For example write a basic hello world and see if that works. If all goes well, then try to import pygame and make a window. If that builds into an exe, then add directories for resources. Form there try your full program. This will easily and quickly pinpoint if there is an issue with python building into an exe or pyinstaller config, pygame into an exe, or a directory conflict issue. There are a lot of issues that can arise when building exe's from python. Its not a one problem fix. There is also a section for when things go wrong in pyinstaller that you can read through https://pythonhosted.org/PyInstaller/when-things-go-wrong.html I had a py2exe setup script ready to go for pygame specifically. It had some special things i had to do with the setup script though. However apparently py2exe is no longer updated past python3.4-ish. You could try it for later versions, but not sure if there would be problems or not now. I havent done it in a long time. RE: Problems with executing .exe file (was .py) - SheeppOSU - Apr-10-2019 The Current Command says - pyinstaller -y -F -w "C:\Users\Chadd\Desktop\HyperSpace!\HyperSpace!.py" - before I push it. I picked "One File" and "Window Based". The output when I click "Convert .PY to .EXE" is -
RE: Problems with executing .exe file (was .py) - metulburr - Apr-10-2019 There is an option in that program to add a directory. I would add this C:/Users/Chadd/Desktop/Ayden Stuff/ to your project so that it can include those resources. That or move those resources to C:\Users\Chadd\Desktop\HyperSpace!/ and load them in your script as carImg = pygame.image.load('RaceCar.png') instead of carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCar.png') I would also remove the -w flag during the debugging process and see what the console says of possible errors when you actually run the exe. RE: Problems with executing .exe file (was .py) - SheeppOSU - Apr-10-2019 Taking away the '-w' changes it to console based and i added a folder with all the pictures for the gam. Current command now looks like this -
I found an error after editing a few things and it's line 92. The error is - The error only diaplays for a second so I snipped it and hand-typed it. Hope I didn''t type any mistakes. The actual lines that it references from HyperSpace!.py are different than what i typed above. I wrote the lines that matched my code above
RE: Problems with executing .exe file (was .py) - metulburr - Apr-11-2019 are you expecting freesansbold.ttf to be on your system or do you actually have a freesansbold.ttf font file in the same directory of your startup script? As is you are trying to load freesansbold.ttf file from your root directory. I am not sure if this is what you intended or not? pygame.font.Font is for loading your own font files taking a path to the font file, where pygame.font.SysFont is for loading system fonts and usually does not need a path. Either way, due to the fact this seems to be a resource/font issue....I would simply try a different font. You can get a list of system wide fonts via sorted(pygame.font.get_fonts())Make sure to use SysFont and Arial is usually a safe one as some systems dont have freesansbold but only freesans. Or the safest approach is to use your own font. RE: Problems with executing .exe file (was .py) - SheeppOSU - Apr-11-2019 Thank you RE: Problems with executing .exe file (was .py) - metulburr - Apr-11-2019 which was the issue? RE: Problems with executing .exe file (was .py) - SheeppOSU - Apr-11-2019 Sorry for late reply. It wasn't loading the font I was using and didn't worked. But it loaded the arial font |