简体   繁体   English

Pyinstaller问题,当我尝试制作一个exe文件时

[英]Pyinstaller problem, when i try to make an exe file

I recently made a small game about some of my friends and when i try to convert it to an exe file, using the auto-py-to-exe project i get an error.When I open the exe file it says "Failed to execute script" I have some pictures and audios, all the elements are in the same folder.我最近做了一个关于我的一些朋友的小游戏,当我尝试将其转换为 exe 文件时,使用 auto-py-to-exe 项目出现错误。当我打开 exe 文件时,它显示“执行失败脚本”我有一些图片和音频,所有元素都在同一个文件夹中。 Here's the code:这是代码:

import pygame
import random
import math
from pygame import mixer

# Initialize the pygame
pygame.init()

# rezolutia ecranului
screen = pygame.display.set_mode((800, 600))

#Background
background = pygame.image.load("background.png")

#Background sound
#mixer.music.load("Talent de tigan.mp3")
#mixer.music.play(-1)

# titlu si iconita
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("icon.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load("vld1.png")
playerX = 370
playerY = 515
playerX_change = 0

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range (num_of_enemies):
    enemyImg.append(pygame.image.load("urs.png"))
    enemyX.append(random.randint(0, 735))
    enemyY.append(random.randint(50, 150))
    enemyX_change.append(2)
    enemyY_change.append(40)

# Bullet
bulletImg = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
# ready - nu pot vedea glontu pe ecran
# fire - glontu se misca
bullet_state = "ready"

# Score
score_value = 0
font = pygame.font.Font("Fruity Stories.ttf", 32)

textX = 10
textY = 10

# Game over text

over_font = pygame.font.Font("Fruity Stories.ttf", 124)

def show_score(x,y):
    score = font.render("Ursi ucisi :" + str(score_value),True, (255,255,255))
    screen.blit(score, (x, y))

def  game_over_text():
    over_text = over_font.render("Te-a mancat ",True, (255,255,255))
    screen.blit(over_text, (200, 250))

def player(x,y):
    screen.blit(playerImg, (x, y))

def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))

def fire_bullet(x,y):
    global bullet_state
    bullet_state = "fire"
    screen.blit(bulletImg, (x + 16, y + 10))

def isCollision(enemyX,enemyY,bulletX,bulletY):
    distance = math.sqrt((math.pow(enemyX-bulletX,2)) + (math.pow(enemyY-bulletY,2)))
    if distance < 27:
        return True
    else:
        return False

# sa nu se inchida fereastra
running = True
while running:
   #background  R  G  B
    screen.fill((0, 0, 0))
    #background image
    screen.blit(background,(0,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
#player movement
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -3
            if event.key == pygame.K_RIGHT:
                playerX_change = 3
            if event.key == pygame.K_SPACE:
                if bullet_state is "ready":
                    bullet_sound = mixer.Sound("inreg.wav")
                    bullet_sound.play()
                    #get the current x coordinate of the spaceship
                    bulletX = playerX
                    fire_bullet(bulletX, bulletY)


        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0

    playerX += playerX_change

    if playerX <=0:
        playerX = 0
    elif playerX >=736:
        playerX = 736
#enemy movement
    for i in range(num_of_enemies):

        #Game Over
        if enemyY[i] > 470:
            for j in range(num_of_enemies):
                enemyY[j] = 2000
            game_over_text()
            break


        enemyX[i] += enemyX_change[i]
        if enemyX[i] <=0:
            enemyX_change[i] = 2
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >=736:
            enemyX_change[i] = -2
            enemyY[i] += enemyY_change[i]
         # Collision
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            explx_sound = mixer.Sound("mustea4.wav")
            explx_sound.play()
            bulletY = 480
            bullet_state = "ready"
            score_value += 1
            enemyX[i] = random.randint(0, 736)
            enemyY[i] = random.randint(50, 150)

        enemy(enemyX[i], enemyY[i], i)
#bullet movement
    if bulletY <=0:
        bulletY = 480
        bullet_state = "ready"

    if bullet_state is "fire":
        fire_bullet(bulletX,bulletY)
        bulletY -= bulletY_change



    player(playerX,playerY)
    show_score(textX,textY)
    pygame.display.update()

Thank you for your time.感谢您的时间。 I am using Python 3 btw.我正在使用 Python 3 顺便说一句。

you need to add your data files:您需要添加数据文件:
If you use windows:如果您使用 windows:

pyinstaller --onefile -w --add-data="background.png;." --add-data="icon.png;."\
#continue adding add-data for all your files
script.py

else replace ;否则替换; with : .:
This way pyinstaller knows which files are needed for your game to work and can add them通过这种方式,pyinstaller 知道您的游戏需要哪些文件才能运行并可以添加它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试创建 exe 文件时出现 Pyinstaller 错误 - Pyinstaller Error when try to create exe file pyinstaller:当我用pyinstaller制作.exe文件时,在python中使用内置函数打开的可执行文件关闭1秒 - Pyinstaller: Executable file opened with built in function in python closes after 1 sec when i make .exe file with pyinstaller 我想使用 pyinstaller 将 .py 文件转换为 exe 文件 - I want to make .py file into exe file by using pyinstaller 如何使用 pyinstaller 使用 torch 模块制作 exe 文件? - How do I make exe file with torch module by using pyinstaller? pyinstaller 生成的 exe 文件执行有问题 - exe file made by pyinstaller has problem executing 创建带有 Pyinstaller 问题的 Python Tkinter exe 文件问题 - Creating Python Tkinter exe file with Pyinstaller problem 使用 pyinstaller 和 pysqlcipher 创建一个文件 exe 的问题 - Problem Creating One File exe with pyinstaller and pysqlcipher 来自 PyInstaller 的 python 文件的 making.exe 问题 - Problem with making .exe from python file by PyInstaller 错误方言 = eval(dialects.strip()) 文件“<string> ",尝试在 PyInstaller 中创建 exe 文件时的第 0 行</string> - Error dialects = eval(dialects.strip()) File "<string>", line 0 when try to create exe file in PyInstaller 使用 pyinstaller 创建的 .exe 文件在我运行时立即关闭 - .exe file created with pyinstaller shuts down immediately when i run it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM