cxu冻结和pygame:致命的python错误(pygame降落伞)分割fau

2024-06-30 12:25:14 发布

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

我用pygame用Python做了一个简短的游戏。我安装了cx_freeze来编译成exe。用这个设置.py脚本I编译:

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\u freeze并遵循了本教程:https://www.youtube.com/watch?v=xz2q2GaTBYE&list=PL6gx4Cwl9DGAjkwJocj7vlc_mFU-4wXJq&index=41

当我编译cmd时会显示以下内容:

^{pr2}$

这是我的游戏脚本第一.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,但是当我在编译后打开我的游戏时,它会给出关于运行时的错误。在

有人能告诉我我做错了什么吗。在

Thnx公司


Tags: keyeventtruesizeifdisplaychangewidth
1条回答
网友
1楼 · 发布于 2024-06-30 12:25:14

我昨天碰到你的问题,因为我一直有同样的问题! 我对Python很陌生,如果我还不熟悉应该使用的专业语言,请原谅。:)所以我是这样做的:

所以不是这样:

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

你可以写:

^{pr2}$

可能还需要定义字体,因此:

^{3}$

您可以在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
    )

我希望我能帮忙。:)

相关问题 更多 >