gpt4 book ai didi

python - 将py转换为exe时,它不会运行wav和png文件

转载 作者:行者123 更新时间:2023-12-01 08:25:21 24 4
gpt4 key购买 nike

我的游戏在文本编辑器中作为 py 文件运行良好,但是当我使用 cx_Freeze 和 pyinstaller 将其转换为可执行文件时,我无法运行它输出的 exe 文件,它给了我这个错误:

Traceback (most recent call last):
File "egg_rampage.py", line 12, in <module>
File "img_sound.py", line 7, in __init__
pygame.error: Unable to open file 'music/scream.wav'
[9292] Failed to execute script egg_rampage

我已经尝试了在互联网上找到的所有内容:- 使用文件的完整路径- 在文件末尾使用 init.py- 通过安装文件安装exe-将音频和图像文件放在单独的模块上

我不知道为什么会发生这种情况,因为我尝试在另一款游戏中这样做并且它有效,我不知道这个游戏出了什么问题,下面我把我的游戏代码放在下面,以便您可以看到。

import pygame
import sys
import random
import time
from game_settings import Settings
from space_ship import SpaceShip
from img_sound import ImageSound


pygame.init()

imgs = ImageSound()

red = (255,0,0)
green = (0,255,0)
white = (255,255,255)
turquoise = (175,238,238)

crash_sound = imgs.scream
imgs.music

pygame.display.set_caption('Egg Rampage')
pygame.display.set_icon(imgs.caricon)
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.scr_width,
ai_settings.scr_height))
ship = SpaceShip(screen, ai_settings)
screen_rect = screen.get_rect()

pause = False




def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(crash_sound)
pygame.time.delay(600)
exorcista_img = imgs.exorcista
screen.blit(exorcista_img, [0,0])

pygame.display.update()

time.sleep(2)

intro(ai_settings, screen)



def intro(ai_settings, screen):

intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

intro_bg = imgs.intro_bg
screen.blit(intro_bg, (0,0))


button(screen, 170 , 200, 150, 50, ' PLAY', green, turquoise, "play")
button(screen, 170 , 270, 150, 50, ' QUIT', red, turquoise, "quit")

highscore = load_highscores()
button(screen, 170, 340, 150, 50, 'HIGHSCORE:' + str(highscore) , red, red)
pygame.display.update()


def button(screen, x, y, w, h, text, a_color, i_color, action=None):

mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, i_color, (x, y, w, h))
if click[0] == 1 and action != None:
if action == "quit":
sys.exit()
elif action == "play":
run_game()
elif action == "continue":
unpause()
else:
pygame.draw.rect(screen, a_color, (x, y, w, h))

font = pygame.font.Font('freesansbold.ttf' , 20)
small_text2 = font.render(text, True, (255,255,255))
small_text2_rect = small_text2.get_rect()
small_text2_rect.center = (x + (w/2), y + (h/2))
screen.blit(small_text2, small_text2_rect)


def egg(egg_startx, egg_starty):

egg_img = imgs.egg_img
for egg_x in egg_startx:
screen.blit(egg_img, [egg_x, egg_starty])


def display_score(dodged):

font = pygame.font.SysFont(None, 25)
text = font.render("Your score: " + str(len(dodged)), True, white)
screen.blit(text, (0,0))

current_highscore = font.render("Highscore: " + str(load_highscores()), True , white)
screen.blit(current_highscore, (0, 25))

filename = 'highscores.txt'
highscore = load_highscores()

if len(dodged) > highscore :
highscore = len(dodged)
with open(filename, '+r') as f:
f.write(str(len(dodged)))


def load_highscores():
filename = 'highscores.txt'
with open(filename, '+r') as f:
try:
highscore = int(f.read())
except:
highscore = 0
return highscore

def paused(ai_settings, screen):

pygame.mixer.music.pause()

while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

font = pygame.font.Font('freesansbold.ttf', 50)
text = font.render('PAUSED', True, (255,255,255))
text_rect = text.get_rect(center = (ai_settings.scr_width/2, 100))
screen.blit(text , text_rect)

button(screen, 170 , 200, 150, 50, ' CONTINUE', green, turquoise, "continue")
button(screen, 170 , 270, 150, 50, ' QUIT', red, turquoise, "quit")
pygame.display.update()


def unpause():
global pause
pygame.mixer.music.unpause()
pause = False



def run_game():
global pause

pygame.mixer.music.play(-1)


ship = SpaceShip(screen, ai_settings)
egg_startx = [random.randrange(0, ai_settings.scr_width)]
egg_starty = -600
egg_count = 1
ai_settings.egg_speed = 3
dodged = []

y = 0
x = 0
x1 = 0
y1 = -500

screen.blit(ai_settings.bg, ai_settings.bg.get_rect())

while True:

for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
ship.pressed_left = True
elif event.key == pygame.K_RIGHT:
ship.pressed_right = True
elif event.key == pygame.K_p:
pause = True
paused(ai_settings, screen)

elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
ship.pressed_left = False
elif event.key == pygame.K_RIGHT:
ship.pressed_right = False

y1 += 2
y += 2

screen.blit(ai_settings.bg, (x,y))
screen.blit(ai_settings.bg, (x1,y1))
if y > 500:
y = -500
if y1 > 500:
y1 = -500



display_score(dodged)

#crash against egg
egg(egg_startx, egg_starty)
egg_center = float(egg_starty)
egg_center += ai_settings.egg_speed
egg_starty = egg_center

if egg_starty > ai_settings.scr_height:
egg_starty = -600
egg_startx = []

for x in range(egg_count):
new_egg = random.randrange(0, ai_settings.scr_width - 75)
egg_startx.append(new_egg)

ai_settings.egg_speed += 0.1
dodged.append(egg)

if len(dodged) % 10 == 0:
if egg_count < 3:
egg_count += 1

if ship.rect.y < egg_starty + 75 and ship.rect.y + 50 > egg_starty:
for egg_x in egg_startx:
if ship.rect.x < egg_x + 75 and ship.rect.x + 50 > egg_x:
crash()

#crash against screen edges
if ship.rect.left <= screen_rect.left or ship.rect.right >= screen_rect.right:
crash()

ship.blitme()
ship.update()

pygame.display.update()

intro(ai_settings, screen)
run_game()

这是包含文件的模块:

import pygame

pygame.init()

class ImageSound():
def __init__(self):
self.scream = pygame.mixer.Sound("music/scream.wav")
self.music = pygame.mixer.music.load("music/mario_kart_music.wav")
self.caricon = pygame.image.load("images/caricon.png")
self.exorcista = pygame.image.load("images/exorcista.bmp")
self.intro_bg = pygame.image.load("images/intro_background.png")
self.egg_img = pygame.image.load("images/egg.bmp")

这是我使用的安装文件:

import cx_Freeze
import os
os.environ['TCL_LIBRARY'] = r"C:\Users\User\AppData\Local\Programs\Python\Python37\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = r"C:\Users\User\AppData\Local\Programs\Python\Python37\tcl\tk8.6"

executables = [cx_Freeze.Executable("egg_rampage.py")]

cx_Freeze.setup(
name = "Egg Rampage",
options = {"build_exe": {"packages":["pygame"],
"include_files":
[r"C:\Users\User\Desktop\egg_rampage\music\mario_kart_music.wav",
r"C:\Users\User\Desktop\egg_rampage\music\scream.wav",
r"C:\Users\User\Desktop\egg_rampage\images\background.png",
r"C:\Users\User\Desktop\egg_rampage\images\caricon.png",
r"C:\Users\User\Desktop\egg_rampage\images\egg.bmp",
r"C:\Users\User\Desktop\egg_rampage\images\exorcista.bmp",
r"C:\Users\User\Desktop\egg_rampage\images\intro_background.png",
r"C:\Users\User\Desktop\egg_rampage\images\rocket.bmp", "highscores.txt"]} },
executables = executables
)

希望您能找到问题所在。

最佳答案

来自 documentation :

include_files: list containing files to be copied to the target directory;

it is expected that this list will contain strings or 2-tuples for the source and destination;** the source can be a file or a directory (in which case the tree is copied except for .svn and CVS directories); the target must not be an absolute path

我认为您传递的文件被平面复制到目标目录中,而不是子目录中。

要包含 musicimages 文件夹,我只需这样做:

cx_Freeze.setup(
name = "Egg Rampage",
options = {"build_exe": {"packages":["pygame"],
"include_files":
[r"C:\Users\User\Desktop\egg_rampage\music",
r"C:\Users\User\Desktop\egg_rampage\images","highscores.txt"]} },
executables = executables
)

仅对于文件,指定目标目录

cx_Freeze.setup(
name = "Egg Rampage",
options = {"build_exe": {"packages":["pygame"],
"include_files":
[(r"C:\Users\User\Desktop\egg_rampage\music\mario_kart_music.wav","music"),

其他文件依此类推(元组:原始文件,包中的目标相对目录)

顺便说一句,程序仍然必须从主脚本路径运行,否则,因为您的代码使用相对路径,所以这是行不通的。像这样的东西:

MODULE_FILE = sys.executable if getattr(sys, 'frozen', False) else __file__  # to support both frozen and normal script
PROGRAM_DIR = os.path.abspath(os.path.dirname(MODULE_FILE))

然后:

image_file = os.path.join(PROGRAM_DIR,"image","mario.png")

关于python - 将py转换为exe时,它不会运行wav和png文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com