Pygame2Exe无法修复的错误

2024-05-29 02:39:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个“游戏”。我喜欢玩它,我想把它分发给我的朋友,而不必在他们的电脑上安装Python和Pygame。在

我对Py2Exe和Pyinstaller做了很多研究。我看了很多教程,修正,错误,但似乎没有一个对我有帮助。在

Pyinstaller没有用,因为它不喜欢Pygame中的字体,而且Py2exe无法编译内置模块,所以我找到了Pygame2exe,它只是一个用于Py2exe的预先准备好的安装脚本,其中包括Pygame和字体。它应该构建得很好,但exe无法使用。。。我得到了一个错误:

"Microsoft Visual C++ Runtime Library

Runtime Error!

Program C:...\dist\Worm Game.exe

This application has requested the Runtime to terminate in an unusual way. Please contact the application's support team for more information."

我只是不明白。。。为什么我不能编译这个游戏!!!在

以下是使用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()

Tags: selfeventfalsetrueifdefscreensurface
3条回答

我不认为你的代码有什么问题。事实上,它编译得很好,我也玩得很好。不错的比赛。在

我建议在你的电脑里查一下以下内容

  1. 你安装了所有的Microsoft更新吗
  2. 查看程序(控制面板-程序和特性),看看是否有最新的微软Visual C++库。在

我认为,如果以上两个适当的地方,它应该工作得很好。在

我在具有以下配置的机器上进行了测试: 1更新了所有安全补丁的Windows7。在

我也遇到了这个问题。经过调查,我发现运行时的错误是由字体引起的。我注意到你也用None作为字体名。请记住,在“Changes by arit:”at pygame2exe page下面有一个关于使用pygame2exe的通知,我们应该使用字体名.ttf“一个也不取代字体名.ttf在exe可以找到的正确文件夹下。例如,您可以使用“自由行粗体.ttf“在创建字体时替换None,并放置自由行粗体.ttf在文件夹下存在exe文件。希望有帮助。在

我的回答是:

几周后(甚至以前有这个问题)我很高兴地说,我解决了这个问题!:)

我问题的第一部分http://i.stack.imgur.com/WpkjR.png): 我通过编辑解决了这个问题设置.py添加“排除”部分的脚本。这导致了可执行文件的成功制作!在

修改设置.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"],
              }
          }
      )

所以,如果你有类似的问题,把那些“缺失”的模块放到“排除”行。在

第二部分:

在我成功生成可执行文件之后,我遇到了下一个问题:应用程序请求运行时以不寻常的方式终止它。请联系…。经过一天又一天的寻找和思考如何解决这个另一个问题,我找到了一个方法。我不敢相信这个问题如此荒谬。问题出在我的代码中,字体定义:

^{pr2}$

在将“None”更改为某个系统字体名(例如“Arial”(必须是字符串))并进行编译之后,我不敢相信我的.exe文件有效!在

font1 = pygame.font.SysFont("Arial", 13)

当然,您可以使用自己的字体,但必须指定其路径并在程序中定义它。在

所以,对于所有经历过这些问题的人来说,试试这些步骤,我希望你们会成功。 我真的希望这对你有帮助,因为我花了好几天和几周的时间来解决这些问题。我甚至尝试用python和pygame的所有版本,以及许多其他的.exe构建器和安装脚本来制作我的.exe文件,但是没有成功。除了这些问题,我以前还有很多其他的问题,但是我在上面找到了答案stackoverflow.com网站. 在

我很高兴我找到了解决这些问题的方法,如果你遇到同样的问题,我会帮助你。在

小提示(我也做过):

1st: update your Microsoft Visual C++ library to the latest one.

2nd: if you have images or fonts similar that your executable program needs, include them to dist folder (where your .exe file has been created).

3rd: when you are making your .exe file, include all needed files to the folder where your setup.py script is (all files and directories that your main script uses).

使用了python2.7x64、pygamepy2exe。在

相关问题 更多 >

    热门问题