转换后的 pygame 立即关闭
Posted
技术标签:
【中文标题】转换后的 pygame 立即关闭【英文标题】:converted pygame closes immediately 【发布时间】:2018-01-05 15:41:21 【问题描述】:我使用 cx_Freeze 将一个 pygame 文件转换为 exe,过程很顺利。但是,当我单击可执行文件时,它会打开,然后立即关闭。我想知道是主文件还是我的 setup.py 有问题?
主要:
import pygame
pygame.init()
white = 255,255,255 #This block defines all the colors in (R,G,B) format
black = 0,0,0
green = 0,255,0
blue = 0,76,153
red = 255,0,0
orange = 255,128,0
yellow = 255,255,0
indigo = 204,0,204
pink = 255,51,255
score_1=0
score_2=0
score_3=0
score_4=0
score_5=0
score_6=0
score_7=0
height = 1366 #The height and width of our window
width = 768
window = pygame.display.set_mode((height,width))
pygame.display.set_caption("Score") #Edit the text between quotes to change window title
title_font = pygame.font.SysFont('Comic Sans MS', 70) #This font is for title in (font,size) format
button_font = pygame.font.SysFont('Comic Sans MS', 12)
title = title_font.render("Chem Rush", True, (black))
clock = pygame.time.Clock()
crashed = False
flask_img_right = pygame.image.load('flask.jpg').convert() #This block is for loading all images
flask_img_left = pygame.image.load('flask1.jpg').convert()
danger_sign = pygame.images.load('danger.jpg').convert()
def flask_right(x_fr,y_fr):
window.blit(flask_img_right,(x_fr,y_fr))
x_fr = (1000)
y_fr = (200)
def flask_left(x_fl,y_fl):
window.blit(flask_img_left,(x_fl,y_fl))
x_fl = (100)
y_fl = (200)
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def things_dodged(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Dodged: "+str(count), True, black)
gameDisplay.blit(text,(0,0))
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
#Increasing the first number moves text to the right
#Increasing the second number moves text lower
#Assume this for everything below unless otherwise stated
window.fill(white)
flask_right(x_fr,y_fr)
flask_left (x_fl, y_fl)
window.blit(title,(500,50))
mouse = pygame.mouse.get_pos()
b1_surf, b1_rect = text_objects("", button_font)
b1_rect.center = ((550),(220))
window.blit(b1_surf, b1_rect)
pygame.draw.rect(window, blue, (500,200,100,50))
b1_text, b1t_rect = text_objects("Team 1", button_font)
b1t_rect.center = ((550),(220))
window.blit(b1_text, b1t_rect)
pygame.draw.rect(window,white,(700,200,50,50))
new_score1 = button_font.render(str(score_1), True, (black))
window.blit(new_score1,(725,225))
if event.type == pygame.MOUSEBUTTONDOWN:
if 500+100>mouse[0]>500 and 200+50>mouse[1]>200:
if event.button == 1: #Left mouse click
score_1+=1
window.blit(new_score1,(725,225))
elif event.button == 3: #Right mouse click
score_1-=1
window.blit(new_score1,(725,225))
b2_surf, b2_rect = text_objects("",button_font)
b2_rect.center = ((550,270))
window.blit(b2_surf, b2_rect)
pygame.draw.rect(window, red,(500,250,100,50))
b2_text, b2t_rect = text_objects("Team 2", button_font)
b2t_rect.center = ((550,270))
window.blit(b2_text, b2t_rect)
pygame.draw.rect(window,white,(700,250,50,50))
new_score2 = button_font.render(str(score_2), True, (black))
window.blit(new_score2,(725,275))
if event.type == pygame.MOUSEBUTTONDOWN:
if 500+100>mouse[0]>500 and 250+50>mouse[1]>250:
if event.button == 1:
score_2+=1
window.blit(new_score2,(725,275))
elif event.button == 3:
score_2-=1
window.blit(new_score2,(725,275))
b3_surf, b3_rect = text_objects("",button_font)
b3_rect.center = ((550,320))
window.blit(b3_surf, b3_rect)
pygame.draw.rect(window, orange,(500,300,100,50))
b3_text, b3t_rect = text_objects("Team 3", button_font)
b3t_rect.center = ((550,320))
window.blit(b3_text, b3t_rect)
pygame.draw.rect(window,white,(700,300,50,50))
new_score3 = button_font.render(str(score_3), True, (black))
window.blit(new_score3,(725,325))
if event.type == pygame.MOUSEBUTTONDOWN:
if 500+100>mouse[0]>500 and 300+50>mouse[1]>300:
if event.button == 1:
score_3+=1
window.blit(new_score3,(725,325))
elif event.button == 3:
score_3-=1
window.blit(new_score3,(725,325))
b4_surf, b4_rect = text_objects("",button_font)
b4_rect.center = ((550,370))
window.blit(b4_surf, b4_rect)
pygame.draw.rect(window, yellow,(500,350,100,50))
b4_text, b4t_rect = text_objects("Team 4", button_font)
b4t_rect.center = ((550,370))
window.blit(b4_text, b4t_rect)
pygame.draw.rect(window,white,(700,350,50,50))
new_score4 = button_font.render(str(score_4), True, (black))
window.blit(new_score4,(725,375))
if event.type == pygame.MOUSEBUTTONDOWN:
if 500+100>mouse[0]>500 and 350+50>mouse[1]>350:
if event.button == 1:
score_4+=1
window.blit(new_score4,(725,375))
elif event.button == 3:
score_4-=1
window.blit(new_score4,(725,375))
b5_surf, b5_rect = text_objects("",button_font)
b5_rect.center = ((550,420))
window.blit(b5_surf, b5_rect)
pygame.draw.rect(window, indigo,(500,400,100,50))
b5_text, b5t_rect = text_objects("Team 5", button_font)
b5t_rect.center = ((550,420))
window.blit(b5_text, b5t_rect)
pygame.draw.rect(window,white,(700,400,50,50))
new_score5 = button_font.render(str(score_5), True, (black))
window.blit(new_score5,(725,425))
if event.type == pygame.MOUSEBUTTONDOWN:
if 500+100>mouse[0]>500 and 400+50>mouse[1]>400:
if event.button == 1:
score_5+=1
window.blit(new_score5,(725,425))
elif event.button == 3:
score_5-=1
window.blit(new_score5,(725,425))
b6_surf, b6_rect = text_objects("",button_font)
b6_rect.center = ((550,470))
window.blit(b6_surf, b6_rect)
pygame.draw.rect(window, green,(500,450,100,50))
b6_text, b6t_rect = text_objects("Team 6", button_font)
b6t_rect.center = ((550,470))
window.blit(b6_text, b6t_rect)
pygame.draw.rect(window,white,(700,450,50,50))
new_score6 = button_font.render(str(score_6), True, (black))
window.blit(new_score6,(725,475))
if event.type == pygame.MOUSEBUTTONDOWN:
if 500+100>mouse[0]>500 and 450+50>mouse[1]>450:
if event.button == 1:
score_6+=1
window.blit(new_score6,(725,475))
elif event.button == 3:
score_6-=1
window.blit(new_score6,(725,475))
b7_surf, b7_rect = text_objects("",button_font)
b7_rect.center = ((550,520))
window.blit(b7_surf, b7_rect)
pygame.draw.rect(window, pink,(500,500,100,50))
b7_text, b7t_rect = text_objects("Team 7", button_font)
b7t_rect.center = ((550,520))
window.blit(b7_text, b7t_rect)
pygame.draw.rect(window,white,(700,500,50,50))
new_score7 = button_font.render(str(score_7), True, (black))
window.blit(new_score7,(725,525))
if event.type == pygame.MOUSEBUTTONDOWN:
if 500+100>mouse[0]>500 and 500+50>mouse[1]>500:
if event.button == 1:
score_7+=1
window.blit(new_score7,(725,525))
elif event.button == 3:
score_7-=1
window.blit(new_score7,(725,525))
pygame.display.update()
clock.tick(10)
pygame.quit()
quit()
end_command = input("any: ")
设置脚本:
from cx_Freeze import setup, Executable
setup(name = "Score Board" ,
version = "0.1" ,
description = "" ,
executables = [Executable("Score_Keeper.py")])
对于我的程序,我只导入了 pygame.我也只使用了我加载的 3 个图像中的 2 个。当我单击 exe 时,它会在终端上显示一个命令终端、一个黑色窗口和一些文本。但是,它会在我阅读之前关闭。
编辑: 我修复了它,如果你想知道如何,只需检查该帖子的 cmets。
【问题讨论】:
在控制台/终端/cmd.exe 中手动运行它,看看是否收到错误消息。也许它需要其他模块/库才能工作。在控制台/终端/cmd.exe 中,您应该会看到更多信息。 谢谢,原来它无法打开 flask.jpg 图像。有没有什么办法解决这一问题?所有图像都与主程序和设置位于同一文件夹中。 您可能必须将图像添加到setup
中的资源 - 我记得它会创建扩展名为 .exe 的 ZIP 文件,并且当您运行 .exe 时,它会将所有文件解压缩为临时文件。您还可以使用方法查找包含应用程序的文件夹并应用于图像名称以仅使用完整路径。
我修复了它,我所做的只是不加载“危险”图像,并将两个烧瓶图像与exe放在同一目录中!
@ZayedAzam 您知道如何使用安装脚本执行此操作,还是您手动执行此操作?如果您想知道如何在安装脚本中包含文件,请告诉我,我会回答。
【参考方案1】:
从上面的 cmets 中您得出结论,图像 flask.jpg、flask1.jpg 和 danger.jpg 丢失。尽管将它们复制到您的构建文件夹中很好并且一切正常,但如果您想创建一个安装程序(IE bdist_msi
或 bdist_mac
),这将不起作用。您需要告诉安装脚本包含这三个文件。
这很简单,您只需要使用include_files
参数即可。不幸的是,文档并没有告诉我们太多关于这个多功能功能的信息,但可以像这样(如here 所述)完成:
from cx_Freeze import setup, Executable
files = ["flask.jpg", "flask1.jpg", "danger.jpg"]
setup(name = "Score Board" ,
version = "0.1" ,
description = "" ,
options = 'build_exe': 'include_files':files
executables = [Executable("Score_Keeper.py")])
【讨论】:
以上是关于转换后的 pygame 立即关闭的主要内容,如果未能解决你的问题,请参考以下文章