怎么把python文件做成exe文件啊
怎么把python文件做成exe文件啊,求解用 pyinstaller 可以将 py 文件打包成 exe
安装 pyinstaller 模块:
python -m pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
打包流程:
将 需要打包的文件放一起 比如 我在 C盘创个py文件夹脚本为 x.py 图标为 z.ico(图标如果没有可以不用,就去掉下面代码中的 -i 和 z.ico )
1. 打开 cmd 窗口 输入将工作目录切换到py文件夹下cd C:\py
2.输入 pyinstaller -F -i z.ico x.py 回车进行打包操作
3.打包完成后在 dist 文件夹下可找到 exe 程序
Ps: 若是 gui 界面程序 建议 -F前面加上 -w 可隐藏 cmd 窗口运行
Twilight6 发表于 2020-8-4 11:14
用 pyinstaller 可以将 py 文件打包成 exe
多个文件怎么打包成一个文件啊 fytfytf 发表于 2020-8-4 11:17
多个文件怎么打包成一个文件啊
打包主程序即可 Twilight6 发表于 2020-8-4 11:19
打包主程序即可
图片等素材也会打包进去吗 fytfytf 发表于 2020-8-4 11:28
图片等素材也会打包进去吗
图片不会的,你需要和 exe 打包,而且目录建议设置再 exe 同目录下,否则其他电脑会报错的 本帖最后由 fytfytf 于 2020-8-4 11:55 编辑
Twilight6 发表于 2020-8-4 11:29
图片不会的,你需要和 exe 打包,而且目录建议设置再 exe 同目录下,否则其他电脑会报错的
被引用的的文件都会打包进来吗 fytfytf 发表于 2020-8-4 11:33
被引用的的文件都会打包进来吗
不会,需要另外复制到程序文件夹下 zltzlt 发表于 2020-8-4 12:02
不会,需要另外复制到程序文件夹下
把.py文件放到exe目录吗 fytfytf 发表于 2020-8-4 12:30
把.py文件放到exe目录吗
不是,那些程序需要用到的资源需要放在 .exe 同文件夹下 zltzlt 发表于 2020-8-4 12:34
不是,那些程序需要用到的资源需要放在 .exe 同文件夹下
我把.py文件和图片都放在exe的同文件夹下,但是exe文件点了没反应,求解 fytfytf 发表于 2020-8-4 13:31
我把.py文件和图片都放在exe的同文件夹下,但是exe文件点了没反应,求解
把代码发上来看看 zltzlt 发表于 2020-8-4 13:32
把代码发上来看看
import pygame
import sys
from pygame.locals import *
import traceback
import myplane
import enemy as e
import bullet
import supply
pygame.init()
pygame.mixer.init()
clock=pygame.time.Clock()
bg_size=width,height=480,700
screen=pygame.display.set_mode(bg_size)
pygame.display.set_caption('The battle of the plane')
bg_image=pygame.image.load('./images/background.png').convert_alpha()
#载入结束画面图片
again_image=pygame.image.load('./images/again.png').convert_alpha()
again_rect=again_image.get_rect()
again_rect.left,again_rect.top=(bg_size-again_rect.width)//2,300
gameover_image=pygame.image.load('./images/gameover.png').convert_alpha()
gameover_rect=gameover_image.get_rect()
gameover_rect.left,gameover_rect.top=(bg_size-gameover_rect.width)//2,350
#载入炸弹
bomb_image=pygame.image.load('./images/bomb.png').convert_alpha()
bomb_rect=bomb_image.get_rect()
bomb_rect.left,bomb_rect.top=0,bg_size-bomb_rect.height
bomb_font=pygame.font.Font('./font/font.ttf',36)
#载入暂停按钮
pause_nor=pygame.image.load('./images/pause_nor.png').convert_alpha()
pause_pressed=pygame.image.load('./images/pause_pressed.png').convert_alpha()
resume_nor=pygame.image.load('./images/resume_nor.png').convert_alpha()
resume_pressed=pygame.image.load('./images/resume_pressed.png').convert_alpha()
pause_rect=pause_nor.get_rect()
pause_rect.left,pause_rect.top=bg_size-60,0
pause_image=pause_nor
#载入音乐
pygame.mixer.music.load('./sound/game_music.ogg')
bullet_sound=pygame.mixer.Sound('./sound/bullet.wav')
bullet_sound.set_volume(0.2)
button_sound=pygame.mixer.Sound('./sound/button.wav')
button_sound.set_volume(0.2)
enemy1_down=pygame.mixer.Sound('./sound/enemy1_down.wav')
enemy1_down.set_volume(0.5)
enemy2_down=pygame.mixer.Sound('./sound/enemy2_down.wav')
enemy2_down.set_volume(0.5)
enemy3_down=pygame.mixer.Sound('./sound/enemy3_down.wav')
enemy3_down.set_volume(0.5)
enemy3_flying=pygame.mixer.Sound('./sound/enemy3_flying.wav')
enemy3_flying.set_volume(0.3)
get_bomb=pygame.mixer.Sound('./sound/get_bomb.wav')
get_bomb.set_volume(0.2)
get_bullet=pygame.mixer.Sound('./sound/get_bullet.wav')
get_bullet.set_volume(0.2)
me_down=pygame.mixer.Sound('./sound/me_down.wav')
me_down.set_volume(0.2)
supply_sound=pygame.mixer.Sound('./sound/supply.wav')
supply_sound.set_volume(0.2)
upgrade=pygame.mixer.Sound('./sound/upgrade.wav')
upgrade.set_volume(0.2)
use_bomb=pygame.mixer.Sound('./sound/use_bomb.wav')
use_bomb.set_volume(0.2)
pygame.mixer.music.set_volume(0.2)
pygame.key.set_repeat(100,100)
def add_small(num,group1,group2):
for i in range(num):
small_plane=e.Xiao(bg_size)
group1.add(small_plane)
group2.add(small_plane)
def add_middle(num,group1,group2):
for i in range(num):
middle_plane=e.Zhong(bg_size)
group1.add(middle_plane)
group2.add(middle_plane)
def add_big(num,group1,group2):
for i in range(num):
big_plane=e.Da(bg_size)
group1.add(big_plane)
group2.add(big_plane)
def speed_inc(group,num):
for i in group:
i.speed+=num
def main():
pygame.mixer.music.play(-1)#循环播放bgm
level=1#表示难度等级
bomb_num=3#表示炸弹数量
pause=False#表示是否暂停
running=True
switch=True#用于切换图片
recorded=False#用于存档
delay=60#用于延迟
#创建自己的飞机
me=myplane.Myplane(bg_size)
#创建敌人的飞机
enemies=pygame.sprite.Group()
smalls=pygame.sprite.Group()
middles=pygame.sprite.Group()
bigs=pygame.sprite.Group()
add_small(20,enemies,smalls)
add_middle(10,enemies,middles)
add_big(2,enemies,bigs)
#创建子弹
bullet1=[]
bullet1_index=0
for i in range(4):
bullet1.append(bullet.Bullet1(bg_size,me.rect.midtop))
#创建超级子弹
bullet2=[]
bullet2_index=0
for i in range(8//2):
bullet2.append(bullet.Bullet2(bg_size,(me.rect.centerx-33,me.rect.centery)))
bullet2.append(bullet.Bullet2(bg_size,(me.rect.centerx+30,me.rect.centery)))
#我方生命数
life=pygame.image.load('./images/life.png').convert_alpha()
life_num=3
life_font=pygame.font.Font('./font/font.ttf',36)
life_rect=life.get_rect()
life_rect.left,life_rect.top=bg_size-life_rect.width,bg_size-life_rect.height
life_text=life_font.render('× %s'%life_num,True,(255,255,0))
#创建中弹图片索引
small_index=0
middle_index=0
big_index=0
me_index=0
#创建分数
score=0
score_font=pygame.font.Font('./font/font.ttf',36)
#创建补给
bomb_supply=supply.Bomb(bg_size)
bullet_supply=supply.Bullet(bg_size)
supply_list=pygame.sprite.Group()
supply_list.add(bomb_supply)
supply_list.add(bullet_supply)
SUPPLY_TIME=USEREVENT
#创建无敌计数器
INVINCIBLE_TIME=USEREVENT+2
#超级子弹定时器
SUPER_BULLET=USEREVENT+1
is_super_bullet=False#是否使用超级子弹
pygame.time.set_timer(SUPPLY_TIME,30*1000)
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type==pygame.KEYDOWN:
if event.key==K_SPACE:#使用炸弹
if bomb_num:
for i in enemies:
if i.rect.top>0:
i.active=False
use_bomb.play()
bomb_num-=1
elif event.type==pygame.MOUSEBUTTONDOWN:
if event.button==1 :
if pause_rect.collidepoint(event.pos):#暂停游戏
pause=not pause
if pause:
pygame.time.set_timer(SUPPLY_TIME,0)
pygame.mixer.music.pause()
pygame.mixer.pause()
else:
pygame.time.set_timer(SUPPLY_TIME,30*1000)
pygame.mixer.unpause()
pygame.mixer.music.unpause()
elif event.type==SUPPLY_TIME:
bomb_supply.active=True#补给开始
bullet_supply.active=True
supply_sound.play()
elif event.type==SUPER_BULLET:
is_super_bullet=False
pygame.time.set_timer(SUPER_BULLET,0)
elif event.type==INVINCIBLE_TIME:
me.invincible=False
pygame.time.set_timer(INVINCIBLE_TIME,0)
key_pressed=pygame.key.get_pressed()#获取键盘输入情况
mouse_pos=pygame.mouse.get_pos()#获取鼠标位置情况
if not pause:
if pause_rect.collidepoint(mouse_pos):
pause_image=pause_pressed
else:
pause_image=pause_nor
else:
if pause_rect.collidepoint(mouse_pos):
pause_image=resume_pressed
else:
pause_image=resume_nor
screen.blit(bg_image,(0,0))#绘制背景
if not pause:
bomb_text=bomb_font.render('× %s'%str(bomb_num),True,(255,0,0))
#绘制炸弹图标
screen.blit(bomb_image,bomb_rect)
screen.blit(bomb_text,(bomb_rect+bomb_rect.width+5,bomb_rect.top+8))
#我方飞机移动
if key_pressed:
if key_pressed:
me.move_top()
if key_pressed:
me.move_bottom()
me.move_left()
if key_pressed:
if key_pressed:
me.move_top()
if key_pressed:
me.move_bottom()
me.move_right()
if key_pressed:
if key_pressed:
me.move_left()
if key_pressed:
me.move_right()
me.move_top()
if key_pressed:
if key_pressed:
me.move_left()
if key_pressed:
me.move_right()
me.move_bottom()
#检测是否吃到炸弹补给并绘制
if bomb_supply.active:
bomb_supply.move()
screen.blit(bomb_supply.image,bomb_supply.rect)
if pygame.sprite.collide_mask(me,bomb_supply):
bomb_supply.active=False
get_bomb.play()
bomb_supply.reset()
if bomb_num<3:
bomb_num+=1
#检测是否吃到超级子弹补给并绘制
if bullet_supply.active:
bullet_supply.move()
screen.blit(bullet_supply.image,bullet_supply.rect)
if pygame.sprite.collide_mask(me,bullet_supply):
is_super_bullet=True
bullet_supply.active=False
pygame.time.set_timer(SUPER_BULLET,18*1000)
get_bullet.play()
bullet_supply.reset()
if delay%10==0:#发射子弹
bullet_sound.play()
if is_super_bullet:
bullets=bullet2
bullet2.reset((me.rect.centerx-33,me.rect.centery))
bullet2_index=(bullet2_index+1)%8
bullet2.reset((me.rect.centerx+30,me.rect.centery))
bullet2_index=(bullet2_index+1)%8
else:
bullets=bullet1
bullet1.reset(me.rect.midtop)
bullet1_index=(bullet1_index+1)%4
#检测子弹是否击中敌机
for b in bullets:
b.move()
if b.active:
screen.blit(b.image,b.rect)
bullet_down=pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
if bullet_down:
b.active=False
for i in bullet_down:
if i in smalls:
i.active=False
elif i in middles:
i.hit=True
if i.hp==0:
i.active=False
else:
i.hp-=1
elif i in bigs:
i.hit=True
if i.hp==0:
i.active=False
else:
i.hp-=1
#等级提升难度
if level==1 and score>500:
upgrade.play()
level=2
add_small(2,enemies,smalls)
add_middle(1,enemies,middles)
elif level==2 and score>1500:
upgrade.play()
level=3
add_small(3,enemies,smalls)
add_middle(1,enemies,middles)
add_big(1,enemies,bigs)
speed_inc(smalls,1)
elif level==3 and score>3000:
upgrade.play()
level=4
add_small(3,enemies,smalls)
add_middle(1,enemies,middles)
add_big(1,enemies,bigs)
speed_inc(smalls,1)
speed_inc(middles,1)
speed_inc(bigs,1)
elif level==4 and score>5000:
upgrade.play()
level=5
add_small(3,enemies,smalls)
add_middle(2,enemies,middles)
add_big(1,enemies,bigs)
speed_inc(smalls,1)
speed_inc(middles,1)
speed_inc(bigs,1)
#检测碰撞
enemies_down=pygame.sprite.spritecollide(me,enemies,False,pygame.sprite.collide_mask)
if enemies_down:
if not me.invincible:
me.active=False
for i in enemies_down:
i.active=False
screen.blit(pause_nor,(bg_size-60,0))
#绘制我方飞机
if me.active:
if switch:
screen.blit(me.image1,me.rect)
else:
screen.blit(me.image2,me.rect)
else:
if (delay%10)==0:
if me_index==0:
me_down.play()
screen.blit(me.down_images,me.rect)
me_index=(me_index+1)%4
if me_index==0:
life_num-=1
pygame.time.set_timer(INVINCIBLE_TIME,3*1000)
life_text=life_font.render('× %s'%life_num,True,(255,255,0))
me.reset()
#绘制剩余生命数量
screen.blit(life,life_rect)
screen.blit(life_text,(life_rect-50,life_rect))
delay-=1
if not delay:
delay=60
if not (delay%5):
switch=not switch#切换图片
if bomb_supply.active:#炸弹移动
bomb_supply.move()
screen.blit(bomb_supply.image,bomb_supply.rect)
#绘制敌方飞机
for i in bigs:#绘制大飞机
if i.active:
i.move()
if i.hit:
screen.blit(i.hit_image,i.rect)
i.hit=False
else:
if switch:
screen.blit(i.image,i.rect)
else:
screen.blit(i.image_n2,i.rect)
if e.Da.hp>i.hp:
pygame.draw.rect(screen,(0,0,0),(i.rect.left,i.rect.top,i.rect.width,7))#绘制血条
if i.hp<e.Da.hp*0.3:
pygame.draw.rect(screen,(255,0,0),(i.rect.left,i.rect.top,i.rect.width//e.Da.hp*i.hp,7))#绘制血条
else:
pygame.draw.rect(screen,(0,255,0),(i.rect.left,i.rect.top,i.rect.width//e.Da.hp*i.hp,7))#绘制血条
if i.rect.bottom==-50:#大飞机进场音乐
enemy3_flying.play(-1)
else:
if not (delay%6):
if big_index==0:
enemy3_down.play()
enemy3_flying.stop()
screen.blit(i.down_images,i.rect)
big_index=(big_index+1)%6
if big_index==0:
score+=200
i.reset()
for i in middles:#绘制中飞机
if i.active:
i.move()
if i.hit:
screen.blit(i.hit_image,i.rect)
i.hit=False
else:
screen.blit(i.image,i.rect)
if e.Zhong.hp>i.hp:
pygame.draw.rect(screen,(0,0,0),(i.rect.left,i.rect.top,i.rect.width,7))#绘制血条
if i.hp<e.Zhong.hp*0.3:
pygame.draw.rect(screen,(255,0,0),(i.rect.left,i.rect.top,i.rect.width//e.Zhong.hp*i.hp,7))#绘制血条
else:
pygame.draw.rect(screen,(0,255,0),(i.rect.left,i.rect.top,i.rect.width//e.Zhong.hp*i.hp,7))#绘制血条
else:
if (delay%6)==0:
if middle_index==0:
enemy2_down.play()
screen.blit(i.down_images,i.rect)
middle_index=(middle_index+1)%4
if middle_index==0:
score+=50
i.reset()
for i in smalls:#绘制小飞机
if i.active:
i.move()
screen.blit(i.image,i.rect)
else:
if (delay%6)==0:
if small_index==0:
enemy1_down.play()
screen.blit(i.down_images,i.rect)
small_index=(small_index+1)%4
if small_index==0:
score+=10
i.reset()
score_text=score_font.render('Score : %s'% str(score),True,(255,255,255))
screen.blit(score_text,(0,0))#绘制得分
#绘制暂停按钮
screen.blit(pause_image,pause_rect)
if life_num==0:#游戏结束
#背景音乐停止
pygame.mixer.music.stop()
#其他音效停止
pygame.mixer.stop()
#停止发放补给
pygame.time.set_timer(SUPPLY_TIME,0)
if not recorded:
recorded=True
#读取最高分
with open('./record.txt','r') as f:
record_score=int(f.readline())
#如果高于最高分则存档
if score>record_score:
record_score=score
with open('./record.txt','w') as f:
f.write(str(score))
#绘制结束界面
pause=True
screen.blit(bg_image,(0,0))
screen.blit(score_text,(0,0))
max_score=score_font.render('Max Score = %s'% str(record_score),True,(255,255,255))#绘制最高分数
screen.blit(max_score,(120,200))
screen.blit(again_image,again_rect)#绘制重新开始
screen.blit(gameover_image,gameover_rect)#绘制结束游戏
mouse_pressed=pygame.mouse.get_pressed()
if again_rect.collidepoint(mouse_pos):
if mouse_pressed:
main()
elif gameover_rect.collidepoint(mouse_pos):
if mouse_pressed:
pygame.quit()
sys.exit()
pygame.display.flip()
clock.tick(60)
if __name__=='__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
去这个网页参考一下啊~
https://www.bilibili.com/video/BV1y7411H7dB/?spm_id_from=333.788.videocard.1
页:
[1]