cx_freeze and pygame: fatal python error (pygame parachute) segmentation fault

cx_freeze and pygame: fatal python error (pygame parachute) segmentation fault

我在 Python 中与 pygame 进行了一场简短的比赛。我安装了 cx_freeze 将其编译为 exe。我用这个 setup.py 脚本编译它:

import cx_Freeze

executables = [cx_Freeze.Executable("first.py")]

cx_Freeze.setup(
    name="test",
    options={"build_exe":{"packages":["pygame"]}},
    description = "test game",
    executables = executables
    )

当我在 cmd 中写入 python setupt.py build 时,文件编译没有错误。但是当我打开它时,我得到这个错误:致命 python 错误(pygame 降落伞)分段错误。

我是 cx_freeze 的新手并遵循了本教程:https://www.youtube.com/watch?v=xz2q2GaTBYE&list=PL6gx4Cwl9DGAjkwJocj7vlc_mFU-4wXJq&index=41

当我编译 cmd 时显示:

Missing modules:
? AppKit imported from pygame.mac_scrap
? Carbon imported from plistlib
? Carbon.File imported from plistlib
? Carbon.Files imported from plistlib
? Foundation imported from pygame.mac_scrap
? MacOS imported from platform, pygame.macosx
? Numeric imported from pygame, pygame.examples.arraydemo, pygame.examples.blit_blends,     pygame.examples.sound_array_demos, pygame.examples.vgrade, pygame.tests.snd
ray_tags, pygame.tests.sndarray_test, pygame.tests.surfarray_tags, pygame.tests.surfarray_test
? OpenGL.GL imported from pygame, pygame.examples.glcube
? OpenGL.GLU imported from pygame.examples.glcube
? RandomArray imported from pygame.examples.vgrade
? _dummy_threading imported from dummy_threading
? _emx_link imported from os
? _scproxy imported from urllib
? _sysconfigdata imported from distutils.sysconfig, sysconfig
? ce imported from os
? copyreg imported from pygame
? fcntl imported from pygame.tests.test_utils.async_sub, subprocess
? gestalt imported from platform
? grp imported from shutil, tarfile
? importlib._bootstrap imported from pkg_resources
? java.lang imported from platform
? numpy imported from pygame, pygame._camera_opencv_highgui, pygame._numpysndarray,     pygame._numpysurfarray, pygame.examples.arraydemo, pygame.examples.sound_array_
mos, pygame.tests.sndarray_tags, pygame.tests.sndarray_test, pygame.tests.surfarray_tags,     pygame.tests.surfarray_test
? opencv imported from pygame._camera_opencv_highgui
? org.python.core imported from copy, pickle    ? os.path imported from os, pkg_resources,     pkgutil, pygame.examples.aliens, pygame.examples.oldalien,         pygame.examples.sound,     pygame.examples.sound_array_demos, shl
, shutil, sysconfig
? os2 imported from os
? os2emxpath imported from os
? posix imported from os
? psyco imported from pygame.examples.testsprite
? pwd imported from distutils.util, getpass, posixpath, shutil, tarfile, webbrowser
? pygame.sdlmain_osx imported from pygame.macosx
? queue imported from pygame.threads
? riscos imported from os
? riscosenviron imported from os
? riscospath imported from os
? rourl2path imported from urllib
? termios imported from getpass
? test.__main__ imported from pygame.tests
? test.event_test imported from pygame.tests.fastevent_test
? test.test_utils imported from pygame.tests.base_test, pygame.tests.blit_test, pygame.tests.bufferproxy_test, pygame.tests.cdrom_test, pygame.tests.color_test, py
me.tests.cursors_test, pygame.tests.display_test, pygame.tests.draw_test, pygame.tests.event_test, pygame.tests.fastevent_test, pygame.tests.font_test, pygame.test
gfxdraw_test, pygame.tests.image__save_gl_surface_test, pygame.tests.image_test, pygame.tests.joystick_test, pygame.tests.key_test, pygame.tests.mask_test, pygame.
sts.midi_test, pygame.tests.mixer_music_test, pygame.tests.mixer_test, pygame.tests.mouse_test, pygame.tests.movie_test, pygame.tests.overlay_test, pygame.tests.pi
larray_test, pygame.tests.rect_test, pygame.tests.run_tests__tests.all_ok.fake_2_test, pygame.tests.run_tests__tests.all_ok.fake_3_test, pygame.tests.run_tests__te
s.all_ok.fake_4_test, pygame.tests.run_tests__tests.all_ok.fake_5_test, pygame.tests.run_tests__tests.all_ok.fake_6_test, pygame.tests.run_tests__tests.all_ok.no_a
ertions__ret_code_of_1__test, pygame.tests.run_tests__tests.all_ok.zero_tests_test, pygame.tests.run_tests__tests.everything.fake_2_test, pygame.tests.run_tests__t
ts.everything.incomplete_todo_test, pygame.tests.run_tests__tests.everything.magic_tag_test, pygame.tests.run_tests__tests.everything.sleep_test, pygame.tests.run_
sts__tests.exclude.fake_2_test, pygame.tests.run_tests__tests.exclude.invisible_tag_test, pygame.tests.run_tests__tests.exclude.magic_tag_test, pygame.tests.run_te
s__tests.failures1.fake_2_test, pygame.tests.run_tests__tests.failures1.fake_3_test,     pygame.tests.run_tests__tests.failures1.fake_4_test, pygame.tests.run_tests__t
ts.incomplete.fake_2_test, pygame.tests.run_tests__tests.incomplete.fake_3_test,     pygame.tests.run_tests__tests.incomplete_todo.fake_2_test, pygame.tests.run_tests_
ests.incomplete_todo.fake_3_test, pygame.tests.run_tests__tests.infinite_loop.fake_1_test,     pygame.tests.run_tests__tests.infinite_loop.fake_2_test, pygame.tests.ru
tests__tests.print_stderr.fake_2_test, pygame.tests.run_tests__tests.print_stderr.fake_3_test,     pygame.tests.run_tests__tests.print_stderr.fake_4_test, pygame.tests
un_tests__tests.print_stdout.fake_2_test, pygame.tests.run_tests__tests.print_stdout.fake_3_test,     pygame.tests.run_tests__tests.print_stdout.fake_4_test, pygame.te
s.run_tests__tests.timeout.fake_2_test, pygame.tests.run_tests__tests.timeout.sleep_test,     pygame.tests.scrap_test, pygame.tests.sndarray_test, pygame.tests.sprite_
st, pygame.tests.surface_test, pygame.tests.surfarray_test, pygame.tests.surflock_test,     pygame.tests.sysfont_test, pygame.tests.test_utils, pygame.tests.test_utils
un_tests, pygame.tests.test_utils.test_runner, pygame.tests.test_utils.unittest_patch,     pygame.tests.threads_test, pygame.tests.time_test, pygame.tests.transform_te

? test.test_utils.async_sub imported from pygame.tests.test_utils.run_tests
? test.test_utils.run_tests imported from pygame.tests, pygame.tests.__main__
? test.test_utils.test_runner imported from pygame.tests.__main__, pygame.tests.test_utils.run_tests
? test.test_utils.unittest_patch imported from pygame.tests.test_utils.test_runner
? urllib.parse imported from pkg_resources, pygame.docs.__main__
? vidcap imported from pygame._camera_vidcapture
? vms_lib imported from platform
? win32api imported from platform, pygame.tests.test_utils.async_sub
? win32con imported from platform
? win32file imported from pygame.tests.test_utils.async_sub
? win32pipe imported from platform, pygame.tests.test_utils.async_sub
? winreg imported from pygame.sysfont
This is not necessarily a problem - the modules may not be needed on this platform.

这是我的游戏脚本 first.py: 导入 pygame 导入时间 随机导入

pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
block_size = 10
display_height = 600
display_width = 800
speed = 10
FPS = 30

font = pygame.font.SysFont(None, 25)

def snake(block_size, snakelist):
    for XnY in snakelist:
        pygame.draw.rect(gameDisplay,green,[XnY[0],XnY[1],block_size,block_size])

def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [display_width*0.1, display_height/2])

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('coolle game')

clock = pygame.time.Clock()

def gameLoop():
    randAppleX = random.randrange(0, display_width-block_size)
    randAppleY = random.randrange(0, display_height-block_size)

    gameExit = False
    gameOver = False

    x_change = 0
    y_change = 0

    x = display_width/2
    y = display_height/2

    snakeList = []
    snakeLength = 1

    AppleThickness = 30

    while not gameExit:

        while gameOver == True:
            gameDisplay.fill(white)
            message_to_screen("Game over, druk op C om op nieuw te beginnen of druk op Q om te stoppen", red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -speed
                    y_change = 0
                elif event.key == pygame.K_RIGHT:
                    x_change = speed
                    y_change = 0
                elif event.key == pygame.K_DOWN:
                    y_change = speed
                    x_change = 0
                elif event.key == pygame.K_UP:
                    y_change = -speed
                    x_change = 0
                if event.key == pygame.K_q:
                    gameExit = True

        if x+block_size > display_width or x < 0 or y+block_size > display_height or y < 0:
            gameOver = True

        x += x_change
        y += y_change

        gameDisplay.fill(white)
        pygame.draw.rect(gameDisplay, red, [randAppleX,randAppleY,AppleThickness,AppleThickness])

        snakeHead = []
        snakeHead.append(x)
        snakeHead.append(y)
        snakeList.append(snakeHead)

        if len(snakeList) > snakeLength:
            del snakeList[0]

        for eachSegment in snakeList[:-1]:
            if eachSegment == snakeHead:
                gameOver = True

        snake(block_size,snakeList)

        pygame.display.update()

##        if x == randAppleX and y == randAppleY:
##            randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
##            randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
##            snakeLength += 1

        if x >= randAppleX and x <= randAppleX+AppleThickness and y >= randAppleY and y <= randAppleY+AppleThickness:
            randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
            randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
            snakeLength += 100
            if randAppleX+AppleThickness > display_width:
                randAppleX = randAppleX-AppleThickness
            if randAppleY+AppleThickness > display_height:
                randAppleY = randAppleY-AppleThickness

        clock.tick(FPS)

    pygame.quit()
    quit()

gameLoop()

我已经尝试过 pygame2exe 但是当我在编译后打开我的游戏时它给出了关于运行时的错误。

谁能告诉我我做错了什么。

谢谢

我昨天看到你的问题,因为我也遇到了同样的问题! 我是 Python 的新手,所以如果我还不熟悉应该使用的专业语言,请原谅。 :) 所以这就是我所做的:

所以不是这个:

font = pygame.font.SysFont(None, 25)

你可以写:

font = pygame.font.Font(None, 25)

可能还需要定义字体,所以:

font = pygame.font.Font(C:\Windows\Fonts\COOPBL.TTF, 25)

C:\Windows\Fonts

下可以找到很多字体

然后您必须在设置文件中包含字体:

import cx_Freeze

executables = [cx_Freeze.Executable("first.py")]

cx_Freeze.setup(
    name="test",
    options={"build_exe":{"packages":["pygame"],
    "include_files": ["C:\Windows\Fonts\COOPBL.TTF"]}},
    description = "test game",
    executables = executables
    )

希望能帮到你。 :)