Python 我无法修复的Pygame2Exe错误
我做了一个游戏。我喜欢玩它,我想把它分发给我的朋友,而不必在他们的计算机上安装Python和Pygame 我对Py2Exe和Pyinstaller做了很多研究。我看了很多教程、修复和错误,但似乎没有一个对我有帮助 Pyinstaller是无用的,因为它不喜欢Pygame中的字体,并且Py2exe不会编译内置模块,所以我发现Pygame2exe只是一个用于Py2exe的预制安装脚本,其中包括Pygame和字体。它应该构建良好,但exe无法使用。。。我得到一个错误:Python 我无法修复的Pygame2Exe错误,python,python-2.7,pygame,runtime-error,py2exe,Python,Python 2.7,Pygame,Runtime Error,Py2exe,我做了一个游戏。我喜欢玩它,我想把它分发给我的朋友,而不必在他们的计算机上安装Python和Pygame 我对Py2Exe和Pyinstaller做了很多研究。我看了很多教程、修复和错误,但似乎没有一个对我有帮助 Pyinstaller是无用的,因为它不喜欢Pygame中的字体,并且Py2exe不会编译内置模块,所以我发现Pygame2exe只是一个用于Py2exe的预制安装脚本,其中包括Pygame和字体。它应该构建良好,但exe无法使用。。。我得到一个错误: 微软Visual C++运行库<
微软Visual C++运行库< /P> 运行时错误
程序C:…\dist\Worm Game.exe 此应用程序已请求运行时在异常情况下终止 方法请联系应用程序的支持团队以了解更多信息 信息 我就是不明白。。。为什么我不能编译这个游戏 以下是使用Python 2.7制作的游戏代码:import pygame
import random
import os
pygame.init()
class Worm:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width() / 2
self.y = surface.get_height() / 2
self.length = 1
self.grow_to = 50
self.vx = 0
self.vy = -1
self.body = []
self.crashed = False
self.color = 255, 255, 0
def event(self, event):
if event.key == pygame.K_UP:
if self.vy != 1:
self.vx = 0
self.vy = -1
else:
a = 1
elif event.key == pygame.K_DOWN:
if self.vy != -1:
self.vx = 0
self.vy = 1
else:
a = 1
elif event.key == pygame.K_LEFT:
if self.vx != 1:
self.vx = -1
self.vy = 0
else:
a = 1
elif event.key == pygame.K_RIGHT:
if self.vx != -1:
self.vx = 1
self.vy = 0
else:
a = 1
def move(self):
self.x += self.vx
self.y += self.vy
if (self.x, self.y) in self.body:
self.crashed = True
self.body.insert(0, (self.x, self.y))
if (self.grow_to > self.length):
self.length += 1
if len(self.body) > self.length:
self.body.pop()
def draw(self):
x, y = self.body[0]
self.surface.set_at((x, y), self.color)
x, y = self.body[-1]
self.surface.set_at((x, y), (0, 0, 0))
def position(self):
return self.x, self.y
def eat(self):
self.grow_to += 25
class Food:
def __init__(self, surface):
self.surface = surface
self.x = random.randint(10, surface.get_width()-10)
self.y = random.randint(10, surface.get_height()-10)
self.color = 255, 255, 255
def draw(self):
pygame.draw.rect(self.surface, self.color, (self.x, self.y, 3, 3), 0)
def erase(self):
pygame.draw.rect(self.surface, (0, 0, 0), (self.x, self.y, 3, 3), 0)
def check(self, x, y):
if x < self.x or x > self.x +3:
return False
elif y < self.y or y > self.y +3:
return False
else:
return True
def position(self):
return self.x, self.y
font = pygame.font.Font(None, 25)
GameName = font.render("Worm Eats Dots", True, (255, 255, 0))
GameStart = font.render("Press Any Key to Play", True, (255, 255, 0))
w = 500
h = 500
screen = pygame.display.set_mode((w, h))
GameLoop = True
while GameLoop:
MenuLoop = True
while MenuLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
MenuLoop = False
screen.blit(GameName, (180, 100))
screen.blit(GameStart, (155, 225))
pygame.display.flip()
screen.fill((0, 0, 0))
clock = pygame.time.Clock()
score = 0
worm = Worm(screen)
food = Food(screen)
running = True
while running:
worm.move()
worm.draw()
food.draw()
if worm.crashed:
running = False
elif worm.x <= 0 or worm.x >= w-1:
running = False
elif worm.y <= 0 or worm.y >= h-1:
running = False
elif food.check(worm.x, worm.y):
score += 1
worm.eat()
print "Score %d" % score
food.erase()
food = Food(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
worm.event(event)
pygame.display.flip()
clock.tick(200)
if not os.path.exists("High Score.txt"):
fileObject = open("High Score.txt", "w+", 0)
highscore = 0
else:
fileObject = open("High Score.txt", "r+", 0)
fileObject.seek(0, 0)
highscore = int(fileObject.read(2))
if highscore > score:
a = 1
else:
fileObject.seek(0, 0)
if score < 10:
fileObject.write("0"+str(score))
else:
fileObject.write(str(score))
highscore = score
fileObject.close()
screen.fill((0, 0, 0))
ScoreBoarda = font.render(("You Scored: "+str(score)), True, (255, 255, 0))
if highscore == score:
ScoreBoardb = font.render("NEW HIGHSCORE!", True, (255, 255, 0))
newscore = 1
else:
ScoreBoardb = font.render(("High Score: "+str(highscore)), True, (255, 255, 0))
newscore = 0
Again = font.render("Again?", True, (255, 255, 0))
GameOver = font.render("Game Over!", True, (255, 255, 0))
screen.blit(GameName, (180, 100))
screen.blit(GameOver, (200, 137))
screen.blit(ScoreBoarda, (190, 205))
if newscore == 0:
screen.blit(ScoreBoardb, (190, 235))
elif newscore == 1:
screen.blit(ScoreBoardb, (175, 235))
screen.blit(Again, (220, 365))
pygame.draw.rect(screen, (0, 255, 0), (200, 400, 40, 40), 0)
pygame.draw.rect(screen, (255, 0, 0), (260, 400, 40, 40), 0)
LEFT = font.render("L", True, (0, 0, 0))
RIGHT = font.render("R", True, (0, 0, 0))
screen.blit(LEFT, (215, 415))
screen.blit(RIGHT, (275, 415))
pygame.display.flip()
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if x > 200 and x < 240 and y > 400 and y < 440:
loop = False
elif x > 260 and x < 300 and y > 400 and y < 440:
GameLoop = False
loop = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
loop = False
elif event.key == pygame.K_RIGHT:
GameLoop = False
loop = False
screen.fill((0, 0, 0))
pygame.quit()
我认为你的代码没有任何问题。事实上,它编译得很好,我也玩得很好。打得好 我建议您在计算机中查看以下内容: 您是否安装了所有Microsoft更新 查看程序控制面板-程序和特性,看看是否有最新的微软Visual C++库。 我认为,如果上述两项都适当到位,它应该可以正常工作 我在具有以下配置的机器上进行了测试:
1.更新了所有安全补丁的Windows 7。我也遇到了这个问题。经过调查,我发现运行时错误是由字体引起的。我注意到您也使用了None作为字体名称。请记住,使用pygame2exe时有一个关于字体的通知,就在arit:at更改的下面,我们应该使用fontname.ttf替换None,并将该fontname.ttf放在exe可以找到的正确文件夹下。例如,您可以在创建字体时使用freesansbold.ttf替换None,并将freesansbold.ttf放在exe文件所在的文件夹下。希望能有所帮助。我的答案: 几个星期后,我甚至在高兴地说我解决了这个问题之前就遇到了这个问题!: 我问题的第一部分: 我通过编辑setup.py脚本并在其中添加排除部分解决了这个问题。这导致了可执行文件的成功制作 修改的setup.py脚本:
from distutils.core import setup
import py2exe
setup(windows=['source_static.py'], options={
"py2exe": {
"excludes": ["OpenGL.GL", "Numeric", "copyreg", "itertools.imap", "numpy", "pkg_resources", "queue", "winreg", "pygame.SRCALPHA", "pygame.sdlmain_osx"],
}
}
)
所以,如果您有类似的问题,只需将这些缺少的模块放到这一行
第二部分:
在成功生成可执行文件之后,我遇到了下一个问题:应用程序请求运行时以一种不寻常的方式终止它。请联系。。。。经过一天又一天的探索和思考,我找到了解决这个问题的方法。我不敢相信这个问题如此荒谬。问题出在我的代码中,字体定义:
font1 = pygame.font.SysFont(None, 13)
在将“无”更改为某个系统字体名称(例如,Arial必须是字符串)并进行编译之后,我简直不敢相信我的.exe文件能够正常工作
font1 = pygame.font.SysFont("Arial", 13)
当然,您可以使用自己的字体,但必须指定其路径并在程序中定义
所以,对于所有经历过这些问题的人,尝试一下这些步骤,我希望你们会成功。
我真的希望这会对你有所帮助,因为我已经浪费了几天和几周的时间来解决这些问题。我甚至试着用所有版本的python和pygame制作我的.exe文件,还有许多其他的.exe构建器和安装脚本,但是运气不好。除了这些问题,我以前还有很多其他问题,但我在stackoverflow.com上找到了答案
我很高兴我找到了解决这些问题的方法,如果你遇到同样的问题,我也会帮助你
小提示我也做过的事情:
第一:将微软Visual C++库更新为最新的.c/p> 第二:如果您的可执行程序需要类似的图像或字体,请将它们包含到创建.exe文件的dist文件夹中
第三:制作.exe文件时,将所有需要的文件包括到setup.py脚本所在的文件夹中,该文件夹是主脚本使用的所有文件和目录使用Python 2.7 x64、pygame和py2exe。如果需要任何其他信息,请询问。今年早些时候,他似乎遇到了完全相同的问题——尽管还不清楚他的问题是如何解决的。可能也有关系。谢谢,但我已经找到并尝试了这两种方法。我希望我知道Paul是如何让他的工作的,因为当我把所有列出的DLL复制到.exe文件夹时,什么都没有修复。我已经看过了分发模块和程序的指南,没有任何帮助。。。除非我错过了什么……问题仍然没有解决,FYI。我已经有了最新的微软Visual C++ ReDIST。包装是最新的。但由于某些原因,我无法更新我的电脑。Windows update当前无法检查更新,因为该服务不可用
跑步您可能需要重新启动计算机。这在重新启动后继续。。。关于如何解决这个问题有什么建议吗?可能有几种可能性,我将寻找以下1。防火墙和所有相关的东西2。检查您的防病毒软件或任何其他相关软件是否阻止更新。另外,我从MS支持团队找到了这个有趣的链接,看看这是否有助于单击“开始”;类型:cmd;右键单击“开始”菜单中的cmd并选择“以管理员身份运行”;类型:净停止Wauserv;按回车键;类型:ren c:\windows\softwaredribution softwaredribution.old;按回车键;类型:净启动Wauserv;按回车键;类型:出口;我尝试了预先的方法,但没有成功。。。我收到一个错误,说服务名称无效。而且Windows Defender没有阻止更新。还有什么想法吗?