在PyCharm中使用pyinstaller打包可执行文件实验指导

在PyCharm中使用pyinstaller打包可执行文件实验指导

有关使用pyinstaller介绍和注意事项,及在cmd中使用pyinstaller打包可执行文件,可参见https://blog.csdn.net/cnds123/article/details/115254418

在PyCharm中使用pyinstaller打包可执行文件

1. 首先,打开自己要发布的项目(为方便读者实验提供素材下载链接 捕鱼达人游戏(python)源码-Python文档类资源-CSDN下载

我这个项目的目录结构如下(素材来自网络):

index.py源码文件如下:

# coding:utf-8
# 导入模块
import pygame,sys,time,random
from pygame.locals import *
# 初始化pygame环境
pygame.init()
# 创建一个长宽分别为800/480的窗口
canvas = pygame.display.set_mode((800,480))
canvas.fill((255,255,255))
# 设置窗口标题
pygame.display.set_caption('捕鱼达人')
#播放音乐
filename='./music/musicA.wav'
pygame.mixer.music.load(filename)
pygame.mixer.music.play(-1)# -1代表循环播放

# 加载图片
bg = pygame.image.load("./images/bg.jpg")
fish1 = pygame.image.load("./images/fish1_0.png")
fish2 = pygame.image.load("./images/fish2_0.png")
fish3 = pygame.image.load("./images/fish3_0.png")
fish4 = pygame.image.load("./images/fish4_0.png")
fish5 = pygame.image.load("./images/fish5_0.png")
fish6 = pygame.image.load("./images/fish6_0.png")
fish7 = pygame.image.load("./images/fish7_0.png")
fish8 = pygame.image.load("./images/fish8_0.png")
fish9 = pygame.image.load("./images/fish9_0.png")
fish10 = pygame.image.load("./images/fish10_0.png")
fish11 = pygame.image.load("./images/fish11_0.png")
net = pygame.image.load("./images/net.png")
gameover = pygame.image.load("./images/gameover.jpg")
# 定义事件监听函数
def handleEvent():
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        # 添加鼠标移动事件,让鼠标控制网的移动
        if event.type == MOUSEMOTION:
            Game.net.x = event.pos[0] - Game.net.width/2
            Game.net.y = event.pos[1] - Game.net.height/2
# 定义时间间隔判断函数
def isActionTime(lastTime,interval):
    if lastTime == 0:
        return True
    currentTime = time.time()
    return currentTime - lastTime >= interval
# 定义鱼类
class Fish():
    def __init__(self,width,height,y,img):
        self.width = width
        self.height = height
        self.x = 800 - self.width
        self.y = y
        self.img = img
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
    def step(self):
        self.x -= 10
# 定义网类
class Net():
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.width = 160
        self.height = 160
        self.img = net
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
    # 定义越界函数
    def outOfBounds(self):
        if self.x <= 0:
            self.x = 0
        elif self.x >= 800 - self.width:
            self.x = 800 - self.width
        elif self.y <= 0:
            self.y = 0
        elif self.y >= 480 - self.height:
            self.y = 480 - self.height
    # 定义碰撞函数
    def hit(self,c):
        return c.x > self.x - c.width and c.x < self.x + self.width and c.y > self.y - c.height and c.y < self.y + self.height
# 定义存储游戏数据的类
class Game():
    # 游戏状态
    state = 'RUNNING'
    # 鱼的列表
    fish = []
    # 网的对象
    net = Net(100,100)
    # 分数
    score = 0
    # 时间
    t = 60
    n = 1
    # 上一次时间
    lastTime = 0
    # 时间间隔
    interval = 0.5
    # 所有鱼的宽高
    fish_pos = [[22,13],[50,48],[55,55],[73,73],[104,80],[60,60],[93,93],[94,81],[99,103],[180,140],[320,206],[100,96]]
# 定义产生鱼的函数
def conEnter():
    if not isActionTime(Game.lastTime,Game.interval):
        return
    Game.lastTime = time.time()
    r = random.randint(1,11)
    if Game.t <= 60:
        Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
    elif Game.t <= 30:
        Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
        Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
    elif Game.t <= 10:
        Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
        Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
        Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
# 定义画组件函数
def conPaint():
    canvas.blit(bg,(0,0))
    Game.net.paint()
    showScore()
    showTime()
    for fish in Game.fish:
        fish.paint()
# 定义组件移动函数
def conStep():
    Game.net.outOfBounds()
    for fish in Game.fish:
        fish.step()
# 定义碰撞检测函数
def checkHit():
    for fish in Game.fish:
        if Game.net.hit(fish) and len(Game.fish) != 0:
            Game.fish.remove(fish)
            Game.score += 1
# 定义绘制分数函数
def showScore():
    TextFont = pygame.font.SysFont('SimHei',40)
    TextScore = TextFont.render('得分:'+str(Game.score),True,(255,255,255))
    canvas.blit(TextScore,(20,20))
# 定义绘制时间函数
def showTime():
    TextFont = pygame.font.SysFont('SimHei',40)
    TextScore = TextFont.render('剩余时间:'+str(Game.t),True,(255,255,255))
    canvas.blit(TextScore,(550,20))
    if Game.n % 50 == 1:
        Game.t -= 1
    Game.n += 1
    if Game.t == 0:
        Game.state = 'END'
# 定义主控制函数
def control():
    if Game.state == 'RUNNING':
        conEnter()
        conPaint()
        conStep()
        checkHit()
    elif Game.state == 'END':
        canvas.blit(gameover,(0,0))
        TextFont = pygame.font.SysFont('SimHei',40)
        TextScore = TextFont.render('最终得分:'+str(Game.score),True,(0,0,0))
        canvas.blit(TextScore,(50,50))
while True:
    # 调用主控制函数
    control()
    # 更新屏幕内容
    pygame.display.update()
    # 延迟10毫秒
    pygame.time.delay(10)
    # 监听事件
    handleEvent()

测试运行,运行程序无误,就可以用pyinsatller发布为可执行文件了。

2. 安装pyinstaller扩展模块(库),用下列方法之一

☆点击底部的【Terminal】打开终端,中输入命令pip install pyinstaller后回车,如图所示进行安装

☆打开PyCharm,先单击File菜单中的settings,再点击project下面的project Interpreter,在搜索需要安装的第三方库,这里是“pyglet”,然后点击界面左下角的Install Package按钮进行安装,参见下图:

3. 在terminal终端,输入命令pyinstaller -F -w index.py 点击回车

参数说明:

-F:将所有库文件打包成一个exe

-w:隐藏黑色控制台窗口

4、打包后需要将这些资源文件按原来的目录结构人工放置到生成可执行文件的目录中,作为一个整体,给使用者——手工建立一个文件夹(名称随便定)给使用者,包括的资源和打包后的可执行文件,参见下图:

OK!

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习&实践爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值