我正在用 pygame 制作游戏 我注意到任何 pyinstaller 文件的大小至少为 17MB
有什么办法可以缩小那个尺寸
我的游戏代码:
import sys
import os
import pygame
from pygame.locals import *
pygame.init()
screen_width = 500
screen_height = 500
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Platformer')
#var
tile_size = 25
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#img
bg_img_s = resource_path('assets/img/sky.png')
bg_img = pygame.image.load(bg_img_s)
class Player():
def __init__(self, x, y):
self.images_right = []
self.images_left = []
self.index = 0
self.counter = 0
for num in range(1, 5):
img_right_s = resource_path(f'assets/img/cooly{num}.png')
img_right = pygame.image.load(img_right_s)
img_right = pygame.transform.scale(img_right, (16, 40))
img_left = pygame.transform.flip(img_right, True, False)
self.images_left.append(img_left)
self.images_right.append(img_right)
self.image = self.images_right[self.index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.speed = 1
self.val_y = 1
self.jumped = False
self.direction = 1
self.debug_mode = False
self.ground_check = None
def update(self):
dx = 0
dy = 0
walk_cooldown = 10
key = pygame.key.get_pressed()
if key[pygame.K_UP] and self.jumped == False and self.ground_check == 1:
self.val_y = -3
self.jumped = True
if key[pygame.K_UP] == False:
self.jumped = False
if key[pygame.K_LEFT]:
self.counter += 1
dx -= self.speed
self.direction = -1
if key[pygame.K_RIGHT]:
self.counter += 1
dx += self.speed
self.direction = 1
if key[pygame.K_RIGHT] == False and key[pygame.K_LEFT] == False:
self.counter = 0
self.index = 0
if self.direction == 1:
self.image = self.images_right[self.index]
if self.direction == -1:
self.image = self.images_left[self.index]
if key[pygame.K_LEFT] and key[pygame.K_RIGHT]:
self.counter = 0
self.index = 0
self.image = self.images_right[self.index]
if self.counter > walk_cooldown:
self.counter = 0
self.index += 1
if self.index >= len(self.images_right):
self.index = 0
if self.direction == 1:
self.image = self.images_right[self.index]
if self.direction == -1:
self.image = self.images_left[self.index]
self.val_y += 0.1
if self.val_y == 5:
self.val_y = 5
dy += self.val_y
for tile in world.tile_list:
if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
dx = 0
if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
if self.val_y < 0:
dy = tile[1].bottom - self.rect.top
self.val_y = 0
elif self.val_y >= 0:
dy = tile[1].top - self.rect.bottom
self.val_y = 0
self.ground_check = 0
for b in world.tile_list:
if b[1].collidepoint(self.rect.centerx, self.rect.bottom + 5):
self.ground_check = 1
break
self.rect.x += dx
self.rect.y += dy
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
screen.blit(self.image, self.rect)
if self.debug_mode == True:
pygame.draw.rect(screen, (255, 255, 255), self.rect, 2)
class World():
def __init__(self, data):
self.tile_list = []
self.debug_mode = False
#load img
dirt_s = resource_path('assets/img/dirt.png')
dirt = pygame.image.load(dirt_s)
grass_dirt_s = resource_path('assets/img/grass_dirt.png')
grass_dirt = pygame.image.load(grass_dirt_s)
stone_s = resource_path('assets/img/stone.png')
stone = pygame.image.load(stone_s)
cobblestone_s = resource_path('assets/img/cobblestone.png')
cobblestone = pygame.image.load(cobblestone_s)
coin_blit_s = resource_path('assets/img/coin_blit.png')
coin_blit = pygame.image.load(coin_blit_s)
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 2:
img = pygame.transform.scale(grass_dirt, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 3:
img = pygame.transform.scale(stone, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 4:
img = pygame.transform.scale(cobblestone, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 5:
img = pygame.transform.scale(coin_blit, (20, 20))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
col_count += 1
row_count += 1
def draw(self):
for tile in self.tile_list:
screen.blit(tile[0], tile[1])
if self.debug_mode == True:
pygame.draw.rect(screen, (255, 255, 255), self.rect, 2)
world_data=[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,1,1],
[0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,1,1,1,1],
[0,0,0,0,0,0,0,0,0,2,2,0,1,1,1,0,1,1,1,1],
[0,0,0,0,0,5,0,0,2,1,1,0,1,1,3,0,1,1,1,1],
[0,0,0,0,2,2,2,2,1,1,1,0,1,3,4,3,3,1,1,1],
[2,2,2,2,1,1,1,1,1,1,1,3,3,3,4,4,3,3,1,1],
]
player = Player(100, screen_height - 65)
world = World(world_data)
run = True
while run:
screen.blit(bg_img, (0, 0))
world.draw()
player.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
这只是一个有世界Gen的玩家的游戏 如果有什么方法可以在不破坏游戏的情况下削减代码 会有帮助的
我使用的命令:
pyinstaller --onefile --name Game main.py --hidden-import pygame
pyinstaller Game.spec
我不知道为什么但是当我在命令前加上“python3 -m” 它输出:
C:\Users\user\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe: No module named pythinstaller
我的 Game.spec 文件:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['D:\\Python\\JumpToRope - Copy\\Test'],
binaries=[],
datas=[('assets/img/*', 'assets/img')],
hiddenimports=['pygame'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='Game',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
我只更改了 Game.spec 中的“datas=[('assets/img/*', 'assets/img')]”
任何人,请帮忙
谢谢