Pygame cx_freeze 语法错误
Pygame cx_freeze Syntax Error
我已经使用 2 个 python 文件和 pygame
制作了一个游戏,我正在尝试使用 cx_freeze
将它变成 .exe
。
这是 2 个脚本:
Move.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
文件具有 Python 2.x 语法 print
:
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 不兼容。您需要执行以下两项操作之一:
重写 Python 2.x 中的代码,然后使用 Python 2.x 可执行文件重写 运行 setup.py
中的代码。
使用 Python 3.x 版本的 pygame
。您可以在他们的 website 上下载它。
我已经使用 2 个 python 文件和 pygame
制作了一个游戏,我正在尝试使用 cx_freeze
将它变成 .exe
。
这是 2 个脚本:
Move.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
文件具有 Python 2.x 语法 print
:
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 不兼容。您需要执行以下两项操作之一:
重写 Python 2.x 中的代码,然后使用 Python 2.x 可执行文件重写 运行
setup.py
中的代码。使用 Python 3.x 版本的
pygame
。您可以在他们的 website 上下载它。