python version : 3.7.6
cx-freeze version : 6.1
我制作了简单的游戏,并尝试使用cx_freeze创建 exe文件
我输入了python setup.py build
,但出现了一些错误消息,如下所示
Traceback (most recent call last):
File "setup.py", line 12, in <module>
executables=[Executable("cave.py")])
File "C:\ProgramData\Anaconda3\envs\testbox\lib\site-packages\cx_Freeze\dist.py", line 340, in setup
distutils.core.setup(**attrs)
File "C:\ProgramData\Anaconda3\envs\testbox\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\ProgramData\Anaconda3\envs\testbox\lib\distutils\dist.py", line 966, in run_commands
self.run_command(cmd)
File "C:\ProgramData\Anaconda3\envs\testbox\lib\distutils\dist.py", line 985, in run_command
cmd_obj.run()
File "C:\ProgramData\Anaconda3\envs\testbox\lib\distutils\command\build.py", line 135, in run
self.run_command(cmd_name)
File "C:\ProgramData\Anaconda3\envs\testbox\lib\distutils\cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "C:\ProgramData\Anaconda3\envs\testbox\lib\distutils\dist.py", line 985, in run_command
cmd_obj.run()
File "C:\ProgramData\Anaconda3\envs\testbox\lib\site-packages\cx_Freeze\dist.py", line 211, in run
freezer.Freeze()
File "C:\ProgramData\Anaconda3\envs\testbox\lib\site-packages\cx_Freeze\freezer.py", line 612, in Freeze
self._FreezeExecutable(executable)
File "C:\ProgramData\Anaconda3\envs\testbox\lib\site-packages\cx_Freeze\freezer.py", line 211, in _FreezeExecutable
self._AddVersionResource(exe)
File "C:\ProgramData\Anaconda3\envs\testbox\lib\site-packages\cx_Freeze\freezer.py", line 127, in _AddVersionResource
stamp(fileName, versionInfo)
File "C:\ProgramData\Anaconda3\envs\testbox\lib\site-packages\win32\lib\win32verstamp.py", line 163, in stamp
h = BeginUpdateResource(pathname, 0)
pywintypes.error: (2, 'BeginUpdateResource', 'The system cannot find the file specified.')
所以我试图用一个示例python文件(来自https://www.youtube.com/watch?v=GSoOwSqTSrs)构建另一个exe文件来检查我的cx_freeze状态,一切顺利,没有问题。
所以我认为我的python和cx-freeze包之间没有兼容性问题
但我仍然不知道该怎么做才能成功使pygame冻结文件
我的代码在下面
# setup.py
from cx_Freeze import setup, Executable
setup(name='cave',
version="1.0",
description="",
options={"build_exe":{"packages":["pygame"], "include_files":["bang.png", "ship.png"]}},
executables=[Executable("cave.py")])
# cave.py
import sys
import random
import pygame as pg
from pygame.locals import QUIT, Rect, KEYDOWN, K_SPACE, K_r
pg.init()
pg.key.set_repeat(5, 5)
SURFACE = pg.display.set_mode((800, 600))
FPS = pg.time.Clock()
SYSFONT = pg.font.SysFont(None, 36)
def main():
game_over = False
ship_y = 250
velocity = 0
score = 0
slope = random.randint(1, 6)
walls = 160
holes = []
for xpos in range(walls):
holes.append(Rect(xpos * 5, 100, 5, 400)) # width : 10
ship_img = pg.image.load("ship.png")
bang_img = pg.image.load("bang.png")
while True:
space_down = False
for event in pg.event.get():
if event.type == QUIT:
pg.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_SPACE:
space_down = True
if not game_over:
score += 10
velocity += -3 if space_down else 3
ship_y += velocity
# Scroll map automatically
edge = holes[-1].copy()
test = edge.move(0, slope)
if test.top <= 0 or test.bottom >= 600:
slope = random.randint(1, 6) * (-1 if slope > 0 else 1)
if edge.height >= 160:
edge.inflate_ip(0, -20)
edge.move_ip(5, slope)
holes.append(edge)
del holes[0]
holes = [x.move(-5, 0) for x in holes]
# Check crash
if holes[0].top > ship_y or holes[0].bottom < ship_y + 50:
game_over = True
SURFACE.fill((0, 255, 0))
for hole in holes:
pg.draw.rect(SURFACE, (0, 0, 0), hole)
SURFACE.blit(ship_img, (0, ship_y))
score_board = SYSFONT.render(f"score : {score}", True, (0, 0, 225))
SURFACE.blit(score_board, (600, 20))
if game_over:
end_msg = SYSFONT.render(f"press R to restart", True, (255, 255, 255))
cal_level = int((400 - holes[0].height) / 20)
level = SYSFONT.render(f"level : {cal_level}", True, (255, 0, 0))
SURFACE.blit(end_msg, (320, 200))
SURFACE.blit(level, (370, 240))
SURFACE.blit(bang_img, (0, ship_y - 40))
pg.display.update()
while True:
for event in pg.event.get():
if event.type == KEYDOWN:
if event.key == K_r:
game_over = False
if not game_over:
ship_y = 250
velocity = 0
score = 0
slope = random.randint(1, 6)
holes = []
for xpos in range(walls):
holes.append(Rect(xpos * 5, 100, 5, 400))
break
pg.display.update()
FPS.tick(30)
if __name__ == "__main__":
main()