鱼C论坛

 找回密码
 立即注册
查看: 2833|回复: 19

[已解决]怎样将py文件和资源文件打包成一个exe

[复制链接]
发表于 2021-1-28 18:36:04 | 显示全部楼层 |阅读模式
60鱼币
怎样将py文件和资源文件打包成一个exe?
最佳答案
2021-1-28 18:36:05
本帖最后由 hrp 于 2021-1-28 23:34 编辑

打包成单exe文件并把资源文件添加到exe文件中,你的源代码需要进行修改。

首先你的源代码中要确定当前目录的起始路径,因为打包成单exe文件后,当前目录并不是exe文件所在的文件夹。

  1. import os, sys
  2. if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
  3.     cur_dir = sys._MEIPASS
  4. else:
  5.     cur_dir = os.path.dirname(os.path.abspath(__file__))
复制代码


以后代码中读取任何资源文件时,都应以cur_dir为起始。
例如你的程序要读取与py文件在同一文件夹下的mm.jpeg:
  1. # mm.jpeg文件的路径应该是
  2. jpeg_path = os.path.join(cur_dir, 'mm.jpeg')
  3. # 读取mm.jpeg时应按jpeg_path读取
复制代码


然后使用3#推荐的工具,把你的资源文件添加到"非源代码资源文件"中,打包后就可以运行了。


如果打包成单exe但资源文件是放在与打包后的exe文件相同目录下,
那上面说的起始路径用
  1. cur_dir = os.path.dirname(os.path.abspath(__file__))
复制代码

即可。


如果打包成单目录,那你的源代码基本上不需要修改,直接添加该添加的资源文件到"非源代码资源文件"中,打包后即可运行。

最佳答案

查看完整内容

打包成单exe文件并把资源文件添加到exe文件中,你的源代码需要进行修改。 首先你的源代码中要确定当前目录的起始路径,因为打包成单exe文件后,当前目录并不是exe文件所在的文件夹。 以后代码中读取任何资源文件时,都应以cur_dir为起始。 例如你的程序要读取与py文件在同一文件夹下的mm.jpeg: 然后使用3#推荐的工具,把你的资源文件添加到"非源代码资源文件"中,打包后就可以运行了。 如果打包成单exe但资 ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-28 18:36:05 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hrp 于 2021-1-28 23:34 编辑

打包成单exe文件并把资源文件添加到exe文件中,你的源代码需要进行修改。

首先你的源代码中要确定当前目录的起始路径,因为打包成单exe文件后,当前目录并不是exe文件所在的文件夹。

  1. import os, sys
  2. if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
  3.     cur_dir = sys._MEIPASS
  4. else:
  5.     cur_dir = os.path.dirname(os.path.abspath(__file__))
复制代码


以后代码中读取任何资源文件时,都应以cur_dir为起始。
例如你的程序要读取与py文件在同一文件夹下的mm.jpeg:
  1. # mm.jpeg文件的路径应该是
  2. jpeg_path = os.path.join(cur_dir, 'mm.jpeg')
  3. # 读取mm.jpeg时应按jpeg_path读取
复制代码


然后使用3#推荐的工具,把你的资源文件添加到"非源代码资源文件"中,打包后就可以运行了。


如果打包成单exe但资源文件是放在与打包后的exe文件相同目录下,
那上面说的起始路径用
  1. cur_dir = os.path.dirname(os.path.abspath(__file__))
复制代码

即可。


如果打包成单目录,那你的源代码基本上不需要修改,直接添加该添加的资源文件到"非源代码资源文件"中,打包后即可运行。

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-28 19:11:37 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-28 19:15:50 | 显示全部楼层
屏幕截图 2021-01-28 191410.png
屏幕截图 2021-01-28 191452.png

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
hrp + 5 + 5 感谢推荐 ^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-28 20:20:14 | 显示全部楼层
https://blog.csdn.net/douzhenwen/article/details/78886244
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-28 21:11:19 | 显示全部楼层
liaozuhen 发表于 2021-1-28 19:11
https://fishc.com.cn/forum.php?mod=viewthread&tid=188498&highlight=exe

为啥我的python环境选择不了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-29 12:15:57 | 显示全部楼层
hrp 发表于 2021-1-28 21:42
打包成单exe文件并把资源文件添加到exe文件中,你的源代码需要进行修改。

首先你的源代码中要确定当前目 ...

恕我新手,大神指导一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-29 12:17:23 | 显示全部楼层
这是我的源码
  1. import random
  2. import pygame
  3. import sys

  4. from pygame.locals import *
  5. pygame.mixer.init()
  6. pygame.mixer.music.load("music.mp3")

  7. snake_speed = 5  # 速度
  8. windows_width = 800
  9. windows_height = 600 #大小
  10. cell_size = 20       #贪方块大小

  11. map_width = int(windows_width / cell_size)
  12. map_height = int(windows_height / cell_size)

  13. # 颜色定义
  14. white = (255, 255, 255)
  15. black = (0, 0, 0)
  16. gray = (230, 230, 230)
  17. dark_gray = (40, 40, 40)
  18. DARKGreen = (0, 155, 0)
  19. Green = (0, 255, 0)
  20. Red = (255, 0, 0)
  21. blue = (0, 0, 255)
  22. dark_blue =(0,0, 139)

  23. BG_COLOR = black #背景色

  24. # 方向
  25. UP = 4
  26. DOWN = 3
  27. LEFT = 2
  28. RIGHT = 1

  29. HEAD = 0 #贪吃蛇

  30. #主函数
  31. def main():
  32.     pygame.init() # 模块初始化
  33.     snake_speed_clock = pygame.time.Clock() # 创建Pygame时钟对象
  34.     screen = pygame.display.set_mode((windows_width, windows_height)) #
  35.     screen.fill(white)

  36.     pygame.display.set_caption("蟒蛇修仙") #设置标题
  37.     show_start_info(screen)               #欢迎信息
  38.     while True:
  39.         running_game(screen, snake_speed_clock)
  40.         show_gameover_info(screen)

  41. #游戏运行主体
  42. def running_game(screen,snake_speed_clock):
  43.     startx = random.randint(3, map_width - 8) #开始位置
  44.     starty = random.randint(3, map_height - 8)
  45.     snake_coords = [{'x': startx, 'y': starty},  #初始贪吃蛇
  46.                   {'x': startx - 1, 'y': starty},
  47.                   {'x': startx - 2, 'y': starty}]

  48.     direction = RIGHT       #  开始时向右移动

  49.     food = get_random_location()     #实物随机位置

  50.     while True:
  51.         for event in pygame.event.get():
  52.             if event.type == QUIT:
  53.                 terminate()
  54.             elif event.type == KEYDOWN:
  55.                 if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
  56.                     direction = LEFT
  57.                 elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
  58.                     direction = RIGHT
  59.                 elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
  60.                     direction = UP
  61.                 elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
  62.                     direction = DOWN
  63.                 elif event.key == K_ESCAPE:
  64.                     terminate()

  65.         move_snake(direction, snake_coords) #移动蛇

  66.         ret = snake_is_alive(snake_coords)
  67.         if not ret:
  68.             break # 游戏结束
  69.         snake_is_eat_food(snake_coords, food) #判断蛇是否吃到食物

  70.         screen.fill(BG_COLOR)
  71.         #draw_grid(screen)
  72.         draw_snake(screen, snake_coords)
  73.         draw_food(screen, food)
  74.         draw_score(screen, len(snake_coords) - 3)
  75.         pygame.display.update()
  76.         snake_speed_clock.tick(snake_speed) #控制fps
  77. #将食物画出来
  78. def draw_food(screen, food):
  79.     x = food['x'] * cell_size
  80.     y = food['y'] * cell_size
  81.     appleRect = pygame.Rect(x, y, cell_size, cell_size)
  82.     pygame.draw.rect(screen, Red, appleRect)
  83. #将贪吃蛇画出来
  84. def draw_snake(screen, snake_coords):
  85.     for coord in snake_coords:
  86.         x = coord['x'] * cell_size
  87.         y = coord['y'] * cell_size
  88.         wormSegmentRect = pygame.Rect(x, y, cell_size, cell_size)
  89.         pygame.draw.rect(screen, dark_blue, wormSegmentRect)
  90.         wormInnerSegmentRect = pygame.Rect(                #蛇身子里面的第二层亮绿色
  91.             x + 4, y + 4, cell_size - 8, cell_size - 8)
  92.         pygame.draw.rect(screen, blue, wormInnerSegmentRect)
  93. #画网格(可选)
  94. def draw_grid(screen):
  95.     for x in range(0, windows_width, cell_size):  # draw 水平 lines
  96.         pygame.draw.line(screen, dark_gray, (x, 0), (x, windows_height))
  97.     for y in range(0, windows_height, cell_size):  # draw 垂直 lines
  98.         pygame.draw.line(screen, dark_gray, (0, y), (windows_width, y))
  99. #移动贪吃蛇
  100. def move_snake(direction, snake_coords):
  101.     if direction == UP:
  102.         newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] - 1}
  103.     elif direction == DOWN:
  104.         newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] + 1}
  105.     elif direction == LEFT:
  106.         newHead = {'x': snake_coords[HEAD]['x'] - 1, 'y': snake_coords[HEAD]['y']}
  107.     elif direction == RIGHT:
  108.         newHead = {'x': snake_coords[HEAD]['x'] + 1, 'y': snake_coords[HEAD]['y']}

  109.     snake_coords.insert(0, newHead)
  110. #判断蛇死了没
  111. def snake_is_alive(snake_coords):
  112.     tag = True
  113.     if snake_coords[HEAD]['x'] == -1 or snake_coords[HEAD]['x'] == map_width or snake_coords[HEAD]['y'] == -1 or \
  114.             snake_coords[HEAD]['y'] == map_height:
  115.         tag = False # 蛇碰壁啦
  116.     for snake_body in snake_coords[1:]:
  117.         if snake_body['x'] == snake_coords[HEAD]['x'] and snake_body['y'] == snake_coords[HEAD]['y']:
  118.             tag = False # 蛇碰到自己身体啦
  119.     return tag
  120. #判断贪吃蛇是否吃到食物
  121. def snake_is_eat_food(snake_coords, food):  #如果是列表或字典,那么函数内修改参数内容,就会影响到函数体外的对象。
  122.     if snake_coords[HEAD]['x'] == food['x'] and snake_coords[HEAD]['y'] == food['y']:
  123.         food['x'] = random.randint(0, map_width - 1)
  124.         food['y'] = random.randint(0, map_height - 1) # 实物位置重新设置
  125.     else:
  126.         del snake_coords[-1]  # 如果没有吃到实物, 就向前移动, 那么尾部一格删掉
  127. #食物随机生成
  128. def get_random_location():
  129.     return {'x': random.randint(0, map_width - 1), 'y': random.randint(0, map_height - 1)}
  130. #开始信息显示
  131. def show_start_info(screen):
  132.     if pygame.mixer.music.get_busy() == False:
  133.         pygame.mixer.music.play()
  134.     font = pygame.font.Font('myfont.ttf', 40)
  135.     tip = font.render('按任意键开始游戏', True, (65, 105, 225))
  136.     gamestart = pygame.image.load('ks.png')
  137.     screen.blit(gamestart, (140, 30))
  138.     screen.blit(tip, (240, 550))
  139.     pygame.display.update()

  140.     while True:  #键盘监听事件
  141.         for event in pygame.event.get():  # event handling loop
  142.             if event.type == QUIT:
  143.                 terminate()     #终止程序
  144.             elif event.type == KEYDOWN:
  145.                 if (event.key == K_ESCAPE):  #终止程序
  146.                     terminate() #终止程序
  147.                 else:
  148.                     return #结束此函数, 开始游戏
  149. #游戏结束信息显示
  150. def show_gameover_info(screen):
  151.     if pygame.mixer.music.get_busy() == False:
  152.         pygame.mixer.music.play()
  153.     font = pygame.font.Font('myfont.ttf', 40)
  154.     tip = font.render('按Q或者ESC退出, 按任意键重新开始', True, (65, 105, 225))
  155.     gamestart = pygame.image.load('sw.png')
  156.     screen.blit(gamestart, (60, 0))
  157.     screen.blit(tip, (80, 300))
  158.     pygame.display.update()

  159.     while True:  #键盘监听事件
  160.         for event in pygame.event.get():  # event handling loop
  161.             if event.type == QUIT:
  162.                 terminate()     #终止程序
  163.             elif event.type == KEYDOWN:
  164.                 if event.key == K_ESCAPE or event.key == K_q:  #终止程序
  165.                     terminate() #终止程序
  166.                 else:
  167.                     return #结束此函数, 重新开始游戏
  168. #画成绩
  169. def draw_score(screen,score):
  170.     font = pygame.font.Font('myfont.ttf', 30)
  171.     scoreSurf = font.render('得分: %s' % score, True, Green)
  172.     scoreRect = scoreSurf.get_rect()
  173.     scoreRect.topleft = (windows_width - 120, 10)
  174.     screen.blit(scoreSurf, scoreRect)
  175. #程序终止
  176. def terminate():
  177.     pygame.quit()
  178.     sys.exit()


  179. main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 12:57:53 | 显示全部楼层
鱼的七秒记忆 发表于 2021-1-28 21:11
为啥我的python环境选择不了

屏幕截图 2021-01-29 125614.png
屏幕截图 2021-01-29 125653.png
然后就可以选择python环境了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-29 13:05:24 | 显示全部楼层
liaozuhen 发表于 2021-1-29 12:57
然后就可以选择python环境了

我生成出来是spec和pyc文件
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-29 13:14:23 | 显示全部楼层
没有exe
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-29 13:25:33 | 显示全部楼层
hrp大佬说要加代码我不知道加在哪
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-29 13:26:05 | 显示全部楼层

源码如上
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 13:45:58 From FishC Mobile | 显示全部楼层
鱼的七秒记忆 发表于 2021-1-29 13:14
没有exe

如果没有修改生成文件的储存位置,并且提示打包完成,那exe文件就在与py文件同位置的dist文件夹里。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 13:48:08 From FishC Mobile | 显示全部楼层
本帖最后由 hrp 于 2021-1-29 13:51 编辑
鱼的七秒记忆 发表于 2021-1-29 12:17
这是我的源码

  1. import random
  2. import pygame
  3. import sys, os

  4. def abs_path(rel):
  5.     if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
  6.         # 如果已打包则当前目录是以下路径
  7.         cur_dir = sys._MEIPASS
  8.     else:
  9.         # 如果没有打包,则当前目录是py文件所在目录
  10.         cur_dir = os.path.dirname(os.path.abspath(__file__))
  11.     # 返回文件rel的绝对路径
  12.     return os.path.join(cur_dir, rel)

  13. from pygame.locals import *
  14. pygame.mixer.init()
  15. # 调用 abs_path 函数,读取其他资源文件同理
  16. pygame.mixer.music.load(abs_path("music.mp3"))

  17. snake_speed = 5  # 速度
  18. windows_width = 800
  19. windows_height = 600 #大小
  20. cell_size = 20       #贪方块大小

  21. map_width = int(windows_width / cell_size)
  22. map_height = int(windows_height / cell_size)

  23. # 颜色定义
  24. white = (255, 255, 255)
  25. black = (0, 0, 0)
  26. gray = (230, 230, 230)
  27. dark_gray = (40, 40, 40)
  28. DARKGreen = (0, 155, 0)
  29. Green = (0, 255, 0)
  30. Red = (255, 0, 0)
  31. blue = (0, 0, 255)
  32. dark_blue =(0,0, 139)

  33. BG_COLOR = black #背景色

  34. # 方向
  35. UP = 4
  36. DOWN = 3
  37. LEFT = 2
  38. RIGHT = 1

  39. HEAD = 0 #贪吃蛇

  40. #主函数
  41. def main():
  42.     pygame.init() # 模块初始化
  43.     snake_speed_clock = pygame.time.Clock() # 创建Pygame时钟对象
  44.     screen = pygame.display.set_mode((windows_width, windows_height)) #
  45.     screen.fill(white)

  46.     pygame.display.set_caption("蟒蛇修仙") #设置标题
  47.     show_start_info(screen)               #欢迎信息
  48.     while True:
  49.         running_game(screen, snake_speed_clock)
  50.         show_gameover_info(screen)

  51. #游戏运行主体
  52. def running_game(screen,snake_speed_clock):
  53.     startx = random.randint(3, map_width - 8) #开始位置
  54.     starty = random.randint(3, map_height - 8)
  55.     snake_coords = [{'x': startx, 'y': starty},  #初始贪吃蛇
  56.                   {'x': startx - 1, 'y': starty},
  57.                   {'x': startx - 2, 'y': starty}]

  58.     direction = RIGHT       #  开始时向右移动

  59.     food = get_random_location()     #实物随机位置

  60.     while True:
  61.         for event in pygame.event.get():
  62.             if event.type == QUIT:
  63.                 terminate()
  64.             elif event.type == KEYDOWN:
  65.                 if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
  66.                     direction = LEFT
  67.                 elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
  68.                     direction = RIGHT
  69.                 elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
  70.                     direction = UP
  71.                 elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
  72.                     direction = DOWN
  73.                 elif event.key == K_ESCAPE:
  74.                     terminate()

  75.         move_snake(direction, snake_coords) #移动蛇

  76.         ret = snake_is_alive(snake_coords)
  77.         if not ret:
  78.             break # 游戏结束
  79.         snake_is_eat_food(snake_coords, food) #判断蛇是否吃到食物

  80.         screen.fill(BG_COLOR)
  81.         #draw_grid(screen)
  82.         draw_snake(screen, snake_coords)
  83.         draw_food(screen, food)
  84.         draw_score(screen, len(snake_coords) - 3)
  85.         pygame.display.update()
  86.         snake_speed_clock.tick(snake_speed) #控制fps
  87. #将食物画出来
  88. def draw_food(screen, food):
  89.     x = food['x'] * cell_size
  90.     y = food['y'] * cell_size
  91.     appleRect = pygame.Rect(x, y, cell_size, cell_size)
  92.     pygame.draw.rect(screen, Red, appleRect)
  93. #将贪吃蛇画出来
  94. def draw_snake(screen, snake_coords):
  95.     for coord in snake_coords:
  96.         x = coord['x'] * cell_size
  97.         y = coord['y'] * cell_size
  98.         wormSegmentRect = pygame.Rect(x, y, cell_size, cell_size)
  99.         pygame.draw.rect(screen, dark_blue, wormSegmentRect)
  100.         wormInnerSegmentRect = pygame.Rect(                #蛇身子里面的第二层亮绿色
  101.             x + 4, y + 4, cell_size - 8, cell_size - 8)
  102.         pygame.draw.rect(screen, blue, wormInnerSegmentRect)
  103. #画网格(可选)
  104. def draw_grid(screen):
  105.     for x in range(0, windows_width, cell_size):  # draw 水平 lines
  106.         pygame.draw.line(screen, dark_gray, (x, 0), (x, windows_height))
  107.     for y in range(0, windows_height, cell_size):  # draw 垂直 lines
  108.         pygame.draw.line(screen, dark_gray, (0, y), (windows_width, y))
  109. #移动贪吃蛇
  110. def move_snake(direction, snake_coords):
  111.     if direction == UP:
  112.         newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] - 1}
  113.     elif direction == DOWN:
  114.         newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] + 1}
  115.     elif direction == LEFT:
  116.         newHead = {'x': snake_coords[HEAD]['x'] - 1, 'y': snake_coords[HEAD]['y']}
  117.     elif direction == RIGHT:
  118.         newHead = {'x': snake_coords[HEAD]['x'] + 1, 'y': snake_coords[HEAD]['y']}

  119.     snake_coords.insert(0, newHead)
  120. #判断蛇死了没
  121. def snake_is_alive(snake_coords):
  122.     tag = True
  123.     if snake_coords[HEAD]['x'] == -1 or snake_coords[HEAD]['x'] == map_width or snake_coords[HEAD]['y'] == -1 or \
  124.             snake_coords[HEAD]['y'] == map_height:
  125.         tag = False # 蛇碰壁啦
  126.     for snake_body in snake_coords[1:]:
  127.         if snake_body['x'] == snake_coords[HEAD]['x'] and snake_body['y'] == snake_coords[HEAD]['y']:
  128.             tag = False # 蛇碰到自己身体啦
  129.     return tag
  130. #判断贪吃蛇是否吃到食物
  131. def snake_is_eat_food(snake_coords, food):  #如果是列表或字典,那么函数内修改参数内容,就会影响到函数体外的对象。
  132.     if snake_coords[HEAD]['x'] == food['x'] and snake_coords[HEAD]['y'] == food['y']:
  133.         food['x'] = random.randint(0, map_width - 1)
  134.         food['y'] = random.randint(0, map_height - 1) # 实物位置重新设置
  135.     else:
  136.         del snake_coords[-1]  # 如果没有吃到实物, 就向前移动, 那么尾部一格删掉
  137. #食物随机生成
  138. def get_random_location():
  139.     return {'x': random.randint(0, map_width - 1), 'y': random.randint(0, map_height - 1)}
  140. #开始信息显示
  141. def show_start_info(screen):
  142.     if pygame.mixer.music.get_busy() == False:
  143.         pygame.mixer.music.play()
  144.     font = pygame.font.Font('myfont.ttf', 40)
  145.     tip = font.render('按任意键开始游戏', True, (65, 105, 225))
  146.     gamestart = pygame.image.load('ks.png')
  147.     screen.blit(gamestart, (140, 30))
  148.     screen.blit(tip, (240, 550))
  149.     pygame.display.update()

  150.     while True:  #键盘监听事件
  151.         for event in pygame.event.get():  # event handling loop
  152.             if event.type == QUIT:
  153.                 terminate()     #终止程序
  154.             elif event.type == KEYDOWN:
  155.                 if (event.key == K_ESCAPE):  #终止程序
  156.                     terminate() #终止程序
  157.                 else:
  158.                     return #结束此函数, 开始游戏
  159. #游戏结束信息显示
  160. def show_gameover_info(screen):
  161.     if pygame.mixer.music.get_busy() == False:
  162.         pygame.mixer.music.play()
  163.     font = pygame.font.Font('myfont.ttf', 40)
  164.     tip = font.render('按Q或者ESC退出, 按任意键重新开始', True, (65, 105, 225))
  165.     gamestart = pygame.image.load('sw.png')
  166.     screen.blit(gamestart, (60, 0))
  167.     screen.blit(tip, (80, 300))
  168.     pygame.display.update()

  169.     while True:  #键盘监听事件
  170.         for event in pygame.event.get():  # event handling loop
  171.             if event.type == QUIT:
  172.                 terminate()     #终止程序
  173.             elif event.type == KEYDOWN:
  174.                 if event.key == K_ESCAPE or event.key == K_q:  #终止程序
  175.                     terminate() #终止程序
  176.                 else:
  177.                     return #结束此函数, 重新开始游戏
  178. #画成绩
  179. def draw_score(screen,score):
  180.     font = pygame.font.Font('myfont.ttf', 30)
  181.     scoreSurf = font.render('得分: %s' % score, True, Green)
  182.     scoreRect = scoreSurf.get_rect()
  183.     scoreRect.topleft = (windows_width - 120, 10)
  184.     screen.blit(scoreSurf, scoreRect)
  185. #程序终止
  186. def terminate():
  187.     pygame.quit()
  188.     sys.exit()


  189. main()
复制代码


顺便提一句,mp3格式的音乐,打包后可能完成程序无法运行,有鱼油试过ogg或wav格式的正常(到底是哪个格式我忘了)

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-29 14:50:22 | 显示全部楼层
hrp 发表于 2021-1-29 13:48
顺便提一句,mp3格式的音乐,打包后可能完成程序无法运行,有鱼油试过ogg或wav格式的正常(到底是哪 ...

我打包出来运行显示failed to execute script(无法执行脚本)是咋回事
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 15:04:53 | 显示全部楼层
鱼的七秒记忆 发表于 2021-1-29 13:05
我生成出来是spec和pyc文件

屏幕截图 2021-01-29 150408.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 15:05:55 From FishC Mobile | 显示全部楼层
鱼的七秒记忆 发表于 2021-1-29 14:50
我打包出来运行显示failed to execute script(无法执行脚本)是咋回事

打包之前能运行吗?
音乐格式转换过没?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 15:08:09 | 显示全部楼层
建议打包成单目录
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 15:09:50 | 显示全部楼层
鱼的七秒记忆 发表于 2021-1-29 14:50
我打包出来运行显示failed to execute script(无法执行脚本)是咋回事

打包成单目录就可以了吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 11:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表