python - 我无法修复的 Pygame2Exe 错误

标签 python python-2.7 pygame runtime-error py2exe

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

我对 Py2Exe 和 Pyinstaller 做了很多研究。我浏览了许多教程、修复程序、错误,但似乎没有一个对我有帮助。

Pyinstaller 是无用的,因为它不喜欢 Pygame 中的字体,并且 Py2exe 不会编译内置模块,所以我找到了 Pygame2exe,它只是一个预制的安装脚本,用于包含 pygame 和字体的 py2exe。它应该构建良好,但 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()

最佳答案

我也遇到了这个问题。经过排查,发现运行时错误是字体引起的。我注意到您也使用 None 作为字体名称。请记住,在 pygame2exe page 的“Changes by arit:”下方有关于字体使用 pygame2exe 的通知。 ,我们应该使用“fontname.ttf”来替换 None 并将 fontname.ttf 放在 exe 可以找到的正确文件夹下。例如创建字体时可以用“freesansbold.ttf”代替None,将freesansbold.ttf放在exe文件所在的文件夹下。希望对您有所帮助。

关于python - 我无法修复的 Pygame2Exe 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12826093/

相关文章:

python - 什么会导致 python 中的 block 堆栈下溢?

python - 有没有办法检测用户移动的 pygame 显示窗口?

python - 为什么字符串方法在 for 循环中使用时停止对对象列起作用

python - FigureCanvasTkAgg 的声明导致内存泄漏

python - 漂亮的汤获得<s>标签或获得价格

python - 安装 Paramiko 错误

python - 列表索引超出范围,当我将范围 1 缩小时,它会丢失一项

python - "Pump GTK messages?"是什么意思

python - 创建默认值为零 (0) 的 defaultdict

python - 有条件地连接具有条件的字符串列表