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没有复制图片目录到打包目录下,需要手工复制

你可能感兴趣的:(Python)