python打包问题解决方案

用python写了一个 打外星人 游戏,在python运行正常。

用pyinstaller打包,报  找不到指定的模块

解决方案:

一、Geany编码首选项中缺省编码 由 UTF-8 改为  简体中文(GBK)


二、alien.py
原代码:
    def __init__(self,ai_settings,screen):
        # initial a alien and set it a location
        super(Alien,self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings
        
        # load a alien picture and set it's rect 属性
        self.image = pygame.image.load('images/alien.bmp')
        self.rect = self.image.get_rect()
        


改之后
    def __init__(self,ai_settings,screen):
        # initial a alien and set it a location
        super(Alien,self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings

        if getattr(sys, 'frozen', False):
            Path = sys._MEIPASS  # This is for when the program is frozen
        else:
            Path = os.path.dirname(__file__)  # This is when the program normally runs
        # load a alien picture and set it's rect
        self.image = pygame.image.load(os.path.join(Path,'images\\alien.bmp'))
        self.rect = self.image.get_rect()he program normally runs

三、ship.py

原代码:
class Ship(Sprite):
    # store Alien Invasion 's setting class
    def __init__(self,ai_settings,screen,pic_type):
        # initcial ship and set it init location
        super(Ship,self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings
        
        
        # load ship image and get it outjoin retriangle
        if pic_type == 'big':
            self.image = pygame.image.load('images/ship.bmp')
        elif pic_type == 'small' :
            self.image = pygame.image.load('images/ship_small.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
改之后:        
import sys
class Ship(Sprite):
    # store Alien Invasion 's setting class
    def __init__(self,ai_settings,screen,pic_type):
        # initcial ship and set it init location
        super(Ship,self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings


        if getattr(sys, 'frozen', False):
            Path = sys._MEIPASS  # This is for when the program is frozen
        else:
            Path = os.path.dirname(__file__)  # This is when the program normally runs
        # load ship image and get it outjoin retriangle
        if pic_type == 'big':
            self.image = pygame.image.load(os.path.join(Path,'images\\ship.bmp'))
        elif pic_type == 'small' :
            self.image = pygame.image.load(os.path.join(Path,'images\\ship_small.bmp'))
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

四、button.py 字符集

原代码:
import pygame.font

class Button():
    
    def __init__(self,ai_settings,screen,msg):
        # initial the buttom's atribute
        self.screen = screen
        self.screen_rect = screen.get_rect()
        
        self.width,self.height = 200,50
        self.button_color = (0,139,69)
        self.text_color = (255,255,255)
        self.font = pygame.font.SysFont(None,48)
        

改之后:    
import pygame.py
class Button():
    
    def __init__(self,ai_settings,screen,msg):
        # initial the buttom's atribute
        self.screen = screen
        self.screen_rect = screen.get_rect()
        
        self.width,self.height = 200,50
        self.button_color = (0,25,0)
        self.text_color = (255,255,255)
        self.font = pygame.font.SysFont('Sans',48)

五、复制images目录
   pyinstaller没有复制图片目录到打包目录下,需要手工复制

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值