我最近一直在使用的游戏在使用cx_freeze将其转换为.msi或.exe后停止了工作。第一个版本工作得很好,但现在当我尝试打开应用程序时,它只是不起作用。
它也没有显示错误代码。它只是显示黑屏并关闭。我的代码可以在下面找到:
SpaceDodge.py
"""
Game Developer: Austin H.
Game Owner: Austin H.
Licensed Through: theoiestinapps
Build: 2
Version: 1.0.1
"""
import os
import pygame as pygame
import random
import sys
import time
pygame.init()
pygame.mixer.init()
pygame.font.init()
left = False
right = False
playerDead = False
devMode = False
musicStopped = False
game_completed = False
game_level_score = (0)
game_display_score = (0)
deaths_this_session = (0)
user_teleport_active = False
user_health_active = False
user_health_inactive = False
user_teleport_display_active = ("False")
user_health_display_active = ("False")
display_width = 1280
display_height = 650
customOrange = (210, 121, 19)
customBlue = (17, 126, 194)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 225, 0)
red = (250, 0, 0)
white = (255, 255, 255)
bright_red = (255,0,0)
bright_green = (0,255,0)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Space Dodge")
clock = pygame.time.Clock()
backgroundMusic = pygame.mixer.music.load('C:/Program Files/Space Dodge/game_background_music.mp3')
enemyImg = pygame.image.load('C:/Program Files/Space Dodge/enemy_image.png')
enemytwoImg = pygame.image.load('C:/Program Files/Space Dodge/enemy_image_two.png')
backgroundImg = pygame.image.load('C:/Program Files/Space Dodge/background_image.png')
rocketImg = pygame.image.load('C:/Program Files/Space Dodge/player_image.png')
injuredSound = pygame.mixer.Sound('C:/Program Files/Space Dodge/player_hurt_sound.wav')
errorSound = pygame.mixer.Sound('C:/Program Files/Space Dodge/game_error_sound.wav')
#Player Powerups
def teleport_powerup(user_teleport_display_active):
font = pygame.font.SysFont(None, 25)
text = font.render("Teleport Powerup: " + str(user_teleport_display_active), True, red)
gameDisplay.blit(text, (display_width - 205, 5))
def ehealth_powerup(user_health_display_active):
font = pygame.font.SysFont(None, 25)
text = font.render("Ehealth Powerup: %s" % user_health_display_active, True, red)
gameDisplay.blit(text, (display_width - 205, 25))
#Game Stats
def enemies_dodged(enemy_objects_dodged):
font = pygame.font.SysFont(None, 25)
text = font.render("Dodged UIO: " + str(enemy_objects_dodged), True, green)
gameDisplay.blit(text, (5, 5))
def game_level(game_display_score):
font = pygame.font.SysFont(None, 25)
game_display_score = game_level_score + 1
text = font.render("Game Level: " + str(game_display_score), True, green)
gameDisplay.blit(text, (5, 25))
def session_deaths(deaths_this_session):
font = pygame.font.SysFont(None, 25)
text = font.render("Session Deaths: " + str(deaths_this_session), True, green)
gameDisplay.blit(text, (display_width - 150, 630))
def bot_speed(enemy_speed):
font = pygame.font.SysFont(None, 25)
text = font.render("Bot Speed: " + str(enemy_speed), True, green)
gameDisplay.blit(text, (5, 560))
def clock_speed(clockTick):
font = pygame.font.SysFont(None, 25)
text = font.render("Clock Tick: " + str(clockTick), True, green)
gameDisplay.blit(text, (5, 630))
def clock_speed_intro(clockTick):
font = pygame.font.SysFont(None, 25)
text = font.render("Clock Tick: " + str(clockTick), True, green)
gameDisplay.blit(text, (5, 630))
#Sprite Definitions
def enemies(enemyx, enemyy):
gameDisplay.blit(enemyImg, (enemy_startx, enemy_starty))
def enemies_two(enemy_twox, enemy_twoy):
gameDisplay.blit(enemytwoImg, (enemy_two_startx, enemy_two_starty))
def rocket(x, y):
gameDisplay.blit(rocketImg, (x, y))
def background(cen1, cen2):
gameDisplay.blit(backgroundImg, (cen1, cen2))
#Message Display
def text_objects(text, font):
textSurface = font.render(text, True, blue)
return textSurface, textSurface.get_rect()
def message_display(text):
global game_completed
largeText = pygame.font.SysFont(None, 70)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
if game_completed == True:
time.sleep(60)
game_intro()
else:
time.sleep(5)
if game_level_score > 0:
pass
else:
pygame.mixer.music.play()
game_loop()
#User Crash
def crash():
injuredSound.play()
message_display("You Died. Game Over!")
#Button Usage
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont(None,20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
gameDisplay.blit(textSurf, textRect)
#Quit
def quitgame():
pygame.quit()
sys.exit()
#Game Intro
def game_intro():
global clockTick
global game_level_score
global user_teleport_active
global user_teleport_display_active
global user_health_active
global user_health_inactive
global user_health_display_active
global enemy_speed
global enemy_two_speed
global enemies_per_level
intro = True
cen1 = (0)
cen2 = (0)
enemy_speed = (random.randrange(5, 10))
enemy_two_speed = (random.randrange(5, 10))
enemies_per_level = (5)
game_level_score = (0)
user_teleport_active = False
user_teleport_display_active = ("False")
user_health_active = False
user_health_inactive = False
user_health_display_active = ("False")
while intro:
clockTick = clock.get_fps()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitgame()
gameDisplay.fill(white)
largeText = pygame.font.Font(None, 115)
TextSurf, TextRect = text_objects("Space Dodge", largeText)
TextRect.center = ((display_width / 2),(display_height / 2))
background(cen1, cen2)
clock_speed_intro(clockTick)
gameDisplay.blit(TextSurf, TextRect)
button("Start", display_width / 2.2, 370, 100, 50, green, bright_green, game_loop)
button("Quit", display_width / 2.2, 445, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
#Game Loop
def game_loop():
global left
global right
global playerDead
global musicStopped
global game_level_score
global enemy_speed
global enemy_two_speed
global game_completed
global user_teleport_active
global user_teleport_display_active
global user_health_active
global user_health_display_active
global user_health_inactive
global deaths_this_session
global enemies_per_level
global enemy_startx
global enemy_starty
global enemy_two_startx
global enemy_two_starty
x = (display_width * 0.43)
y = (display_height * 0.74)
cen1 = (0)
cen2 = (0)
x_change = 0
rocket_width = (86)
game_score = (0)
enemy_objects_dodged = (0)
enemy_startx = random.randrange(0, display_width)
enemy_starty = -600
enemy_width = 75
enemy_height = 75
enemy_two_startx = random.randrange(0, display_width)
enemy_two_starty = -600
enemy_two_width = 125
enemy_two_height = 125
while not playerDead:
clockTick = clock.get_fps()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.mixer.music.stop()
quitgame()
if devMode == True:
print(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
left = True
if event.key == pygame.K_d:
right = True
if event.key == pygame.K_LEFT:
left = True
if event.key == pygame.K_RIGHT:
right = True
if event.key == pygame.K_KP4:
left = True
if event.key == pygame.K_KP6:
right = True
if event.key == pygame.K_ESCAPE:
game_intro()
if event.key == pygame.K_SPACE:
pass
if event.key == pygame.K_m:
if musicStopped == False:
musicStopped = True
pygame.mixer.music.stop()
elif musicStopped == True:
musicStopped = False
pygame.mixer.music.play()
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or pygame.K_d:
left = False
if event.key == pygame.K_d:
right = False
if event.key == pygame.K_LEFT:
left = False
if event.key == pygame.K_RIGHT:
right = False
if event.key == pygame.K_KP4:
left = False
if event.key == pygame.K_KP6:
right = False
if event.key == pygame.K_SPACE:
pass
if left and right:
x_change *= 1
elif left and x > -86:
x_change = -10
elif right and x < (display_width - 89):
x_change = 10
else:
x_change = 0
if game_level_score > 999:
quitgame()
if enemy_objects_dodged == enemies_per_level:
enemy_speed += 0.5
enemy_two_speed + 0.5
game_level_score += 1
enemies_per_level += 3
if game_level_score == 999:
game_completed = True
message_display('Game Complete! You Win! :D')
else:
message_display('You Completed Level: ' + str(game_level_score))
if user_health_inactive == False:
if game_level_score > 9:
user_health_active = True
user_health_display_active = ("True")
if game_level_score > 4:
user_teleport_active = True
user_teleport_display_active = ("True")
if user_teleport_active == True:
if x < -0:
x = 1330
if enemy_starty > display_height:
enemy_starty = 0 - enemy_height
enemy_startx = random.randrange(0, display_width)
game_score += 1
enemy_objects_dodged += 1
if enemy_two_starty > display_height:
enemy_two_starty = 0 - enemy_two_height
enemy_two_startx = random.randrange(0, display_width)
game_score += 1
enemy_objects_dodged += 1
if y < enemy_starty + enemy_height:
if x > enemy_startx and x < enemy_startx + enemy_width or x + rocket_width > enemy_startx and x + rocket_width < enemy_startx + enemy_width:
if user_health_active:
x = (display_width * 0.43)
y = (display_height * 0.74)
user_health_active = False
user_health_display_active = False
user_health_inactive = True
else:
pygame.mixer.music.stop()
deaths_this_session += 1
crash()
if y < enemy_two_starty + enemy_two_height:
if x > enemy_two_startx and x < enemy_two_startx + enemy_two_width or x + rocket_width > enemy_two_startx and x + rocket_width < enemy_two_startx + enemy_two_width:
if user_health_active:
x = (display_width * 0.43)
y = (display_height * 0.74)
user_health_active = False
user_health_display_active = False
user_health_inactive = True
else:
pygame.mixer.music.stop()
deaths_this_session += 1
crash()
x += x_change
background(cen1, cen2)
enemies(enemy_startx, enemy_starty)
enemies_two(enemy_two_startx, enemy_two_starty)
enemy_starty += enemy_speed
enemy_two_starty += enemy_two_speed
rocket(x, y)
enemies_dodged(enemy_objects_dodged)
game_level(game_display_score)
clock_speed(clockTick)
session_deaths(deaths_this_session)
teleport_powerup(user_teleport_display_active)
ehealth_powerup(user_health_display_active)
pygame.display.update()
clock.tick(90)
if __name__ == "__main__":
pygame.mixer.music.set_volume(0.20)
pygame.mixer.music.play(-1)
game_intro()
Setup.py
from cx_Freeze import *
import sys
base = None
if sys.platform == 'win32':
base = "Win32GUI"
setup(
name = "SpaceDodge",
author = "theoiestinapps",
options = {"build_exe": {"packages": ["pygame"], "include_files": ["game_background_music.mp3", "background_image.png", "enemy_image.png",
"player_image.png", "player_hurt_sound.wav", "game_error_sound.wav"]},
"bdist_msi": {"upgrade_code": "{9d3d322e74744a1282ce1ea2c5af2676}"}},
executables = [Executable("SpaceDodge.py", shortcutName = "SpaceDodge", shortcutDir = "DesktopFolder", base = base)]
)
答案 0 :(得分:0)
N.B。:我没有使用cx_Freeze
的经验,也没有pygame的经验,所以我的回答只是一种&#34;感觉&#34;什么可能是可疑的,如果我实际调试代码,我首先看的地方:
在代码中的许多地方,您可以使用以下方法更改字体大小:
font = pygame.font.SysFont(None, 25)
当您将None
作为字体名称的参数时,文档说:
如果找不到合适的系统字体,这将在加载默认的pygame字体时回退。
当您重新打包应用程序以使其成为自包含的.exe时,cx_Freeze可能会尝试获取&#34;默认&#34;字体,并假定(错误地)它是
freesansbold.ttf
它无法找到或放入.exe资源。然后,它可能会在.exe中相对于/内部引用该字体,而不会将其复制,从而导致代码崩溃。
要验证我的理论,您可以删除/注释所有对字体更改的引用(使您的代码看起来看起来很丑),尝试再次执行。然后查看它是否有效。你也可以打印输出:
pygame.font.get_default_font()
在两个上下文中,看看它是否显示相同的文件。
这是我的两分钱,希望它能帮助