python - Pygame cx_freeze 语法错误

标签 python pygame cx-freeze

我使用 2 个 python 文件和 pygame 制作了一个游戏,我正在尝试使用 cx_freeze 将其转换为 .exe .

这是 2 个脚本:

移动.py:

import pygame
from Enemy import *

pygame.init()

gameDisplay = pygame.display.set_mode((800,600))

white = [255,255,255]
black = [0,0,0]
red = [200,0,0]
green = [0,200,0]

gamex = 800

gamey = 600

enemynumber = [5,7,10]

level = 0

blocksize = 10

clock = pygame.time.Clock()

smallfont = pygame.font.SysFont("impactregular", 25)
medfont = pygame.font.SysFont("impactregular", 50)
largefont = pygame.font.SysFont("impactregular", 75)

pygame.display.update()

FPS = 30

EnemyList = []

def makeEnemy(enemyCount):

    for i in range(0,enemyCount):
        EnemyList.append( Enemy())
        EnemyList[i].setup(i*100,200,False,0)

def enemyCalc(blockx,blocky,enemyCount):
    for i in range(0,enemyCount):
        pygame.draw.rect(gameDisplay, red, EnemyList[i].calc(blockx,blocky))

def GameOver():
    gameover = True
    while gameover:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    level = 0
                    gameLoop()
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()
        gameDisplay.fill(white)
        message_to_screen("GAME OVER", red, -100, "large")
        message_to_screen("Press R to try again, or Q to quit", black, 100, "medium")
        pygame.display.update()

def Win():
    win = True
    while win:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    level = 0
                    gameLoop()
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()
        gameDisplay.fill(white)
        message_to_screen("YOU WIN!", green, -100, "large")
        message_to_screen("Press R to play again, or Q to quit", black, 100, "medium")
        pygame.display.update()

def Intro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    level = 0
                    gameLoop()
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()
        gameDisplay.fill(white)
        message_to_screen("GO UP: The Game", red, -100, "large")
        message_to_screen("Get to the top of the screen,",
                          black, 100, "medium")
        message_to_screen("but avoid the red squares!",
                          black, 150, "medium")
        message_to_screen("Press R to play, or Q to quit", black, 250, "medium")
        pygame.display.update()

def text_objects(text,color,size):
    if size == "small":
        textSurface = smallfont.render(text, True, color)
    elif size == "medium":
        textSurface = medfont.render(text, True, color)
    elif size == "large":
        textSurface = largefont.render(text, True, color)

    return textSurface, textSurface.get_rect()

def message_to_screen(msg,color,offset=0, size="small"):
    textSurf, textRect = text_objects(msg,color,size)
    textRect.center = (gamex / 2), ((gamey / 2) + offset)
    gameDisplay.blit(textSurf, textRect)

def collision(playerx, playery, enemyCount):
    for i in range(0,enemyCount):
        enemyx = EnemyList[i].x
        enemyy = EnemyList[i].y
        if playerx == enemyx and playery == enemyy:
            GameOver()

def gameLoop():
    global EnemyList
    global level

    blockx = gamex/2
    blocky = gamey/2

    xchange = 0
    ychange = 0

    gameExit = False

    makeEnemy(enemynumber[level])

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    xchange = 10
                if event.key == pygame.K_LEFT:
                    xchange = -10
                if event.key == pygame.K_DOWN:
                    ychange = 10
                if event.key == pygame.K_UP:
                    ychange = -10
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    xchange = 0
                elif event.key == pygame.K_LEFT:
                    xchange = 0
                elif event.key == pygame.K_DOWN:
                    ychange = 0
                elif event.key == pygame.K_UP:
                    ychange = 0

        collision(blockx,blocky,enemynumber[level])
        if xchange == -10 and blockx == 0 or xchange == 10 and blockx == gamex-10:
            xchange = 0
        if ychange == 10 and blocky == gamey-10:
            ychange = 0
        if ychange == -10 and blocky == 0:
            if level < 2:
                level += 1
                gameLoop()
            else:
                Win()
        blockx += xchange
        blocky += ychange
        gameDisplay.fill(white)
        message_to_screen("Level: "+str(level+1),black,-275)
        enemyCalc(blockx,blocky,enemynumber[level])
        pygame.draw.rect(gameDisplay, black, [blockx,blocky,10,10])
        pygame.display.update()
        clock.tick(FPS)




Intro()

还有 Enemy.py:

import pygame

class Enemy:

    def setup(self,x,y,dead,subclass):
        if not dead:
            self.x = x
            self.y = y
            self.count = 0
            self.reverse = False
            self.subclass = subclass


    def calc(self, playerx, playery):

        if self.count < 10 and self.reverse == False:
            self.count += 1
            self.x += 10
        elif self.count == 10:
            self.reverse = True
            self.x += -10
            self.count += -1
        if self.count > 0 and self.reverse == True:
            self.count += -1
            self.x += -10
        elif self.count == 0:
            self.reverse = False
            self.x += 10
            self.count += 1
        return [self.x,self.y,10,10]

我输入了setup.py来编译脚本:

import cx_Freeze

cx_Freeze.setup(
    name="Go Up: The Game",
    options = {"build_exe": {"packages":["pygame","Enemy"]}},
    executables = [cx_Freeze.Executable("Move.py")]
    )

我输入了正确的 Python 控制台命令,但出现错误:

C:\Users\Sean\Documents\Python\Object Test>c:/python32/python setup.py build
running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 6, in <module>
    executables = [cx_Freeze.Executable("Move.py")]
  File "c:\python32\lib\site-packages\cx_Freeze\dist.py", line 365, in setup
    distutils.core.setup(**attrs)
  File "c:\python32\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "c:\python32\lib\distutils\dist.py", line 917, in run_commands
    self.run_command(cmd)
  File "c:\python32\lib\distutils\dist.py", line 936, in run_command
    cmd_obj.run()
  File "c:\python32\lib\distutils\command\build.py", line 126, in run
    self.run_command(cmd_name)
  File "c:\python32\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "c:\python32\lib\distutils\dist.py", line 936, in run_command
    cmd_obj.run()
  File "c:\python32\lib\site-packages\cx_Freeze\dist.py", line 235, in run
    freezer.Freeze()
  File "c:\python32\lib\site-packages\cx_Freeze\freezer.py", line 575, in Freeze

    self.finder = self._GetModuleFinder()
  File "c:\python32\lib\site-packages\cx_Freeze\freezer.py", line 330, in _GetMo
duleFinder
    finder.IncludePackage(name)
  File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 581, in Include
Package
    self._ImportAllSubModules(module, deferredImports)
  File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 220, in _Import
AllSubModules
    deferredImports)
  File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 338, in _Intern
alImportModule
    parentModule, namespace)
  File "c:\python32\lib\site-packages\cx_Freeze\finder.py", line 366, in _LoadMo
dule
    module.code = compile(codeString, path, "exec")
  File "c:\python32\lib\site-packages\pygame\nmovie.py", line 15
    print "Unable to find a working movie backend. Loading the dummy movie class
..."

   ^
SyntaxError: invalid syntax

C:\Users\Sean\Documents\Python\Object Test>

有人可以告诉我我的文件出了什么问题吗?

最佳答案

您正在使用Python 3.2:

C:\Users\Sean\Documents\Python\Object Test>c:/python32/python setup.py build
                                              ^^^^^^^^

但是 pygame 包中的 nmovie.py 文件具有用于 print 的 Python 2.x 语法:

print "Unable to find a working movie backend. Loading the dummy movie class..."
^^^^^^

print is a function in Python 3.x并且需要用括号调用:

print("Unable to find a working movie backend. Loading the dummy movie class...")
     ^                                                                          ^

这意味着您使用的 pygame 版本适用于 Python 2.x,因此与 Python 3.x 不兼容。您需要执行以下两件事之一:

  1. 用 Python 2.x 重写代码,然后使用 Python 2.x 可执行文件运行 setup.py

  2. 使用 pygame 的 Python 3.x 版本。您可以在他们的website上下载它。 。

关于python - Pygame cx_freeze 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768363/

相关文章:

python - 仅使用一两个特征构建预测模型

python - 类似口袋妖怪的文字显示

python - 使用 cx_Freeze 编译为 exe 时没有此类文件或目录 webdriver_prefs.json

compiler-errors - cx_Freeze 错误 : resource_filename() only supported for . 鸡蛋不是 .zip

python - .only django queryset 到底有什么作用?

python - 我可以抑制 Sphinx 文档中的变量扩展吗?

python - Pygame:与瓷砖碰撞不起作用

python - 在 CX_Freeze 中从 Python 3.6 脚本创建单个 exe

python - 是否可以在python中导入一个模块,然后将其删除,但仍然可以在程序中使用它?

python - 在 Pygame 中绘制箭头