这是我在这里的第一篇文章。我看到了很多类似的问题,但我的问题有点具体。为了进入主题,我需要用python使单个文件可执行,就像我用py2exe和Iexpress所做的那样。一切都很顺利,直到我昨天把图片(.png)包括在内。在用py2exe编译后,一切都正常了,所以我很确定pygame能够打开.png,但当我用Iexpress压缩时,我得到了:
"Traceback(最近一次通话):文件"Game.py",第23行,在pygame.error:文件不是Windows BMP文件"
所以我得到了两个完全相同的文件夹,第一个是工作的pre-express,第二个是不工作的临时文件夹。我使用了Python 3.4和pygame‑1.9.2a0‑cp34这是我的代码
__author__ = 'Xpd'
back = "bg.png"
sh = "ship.png"
ali = "alien.png"
import pygame
import random
import sys
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green =(0,255,0)
blue = (0,0,255)
display_width = 800
display_height = 600
clock = pygame.time.Clock()
gameDisplay = pygame.display.set_mode((display_width,display_height), 0, 0)
pygame.display.set_caption('Invaders')
background = pygame.image.load(back)
ship = pygame.image.load(sh)
alien = pygame.image.load(ali)
def Intro():
Intro = True
while Intro == True:
gameDisplay.fill(green)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
Intro = False
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = ((buttonx+(buttonwidth/2)), buttony+(buttonheight/2))
gameDisplay.blit(textSurf, textRect)
return textSurface, textSurface.get_rect()
def text(msg,color, y_displace=0, size = "small"):
textSurf, textRect = text_objects(msg,color, size)
textRect.center = (display_width / 2), (display_height / 2)+y_displace
gameDisplay.blit(textSurf, textRect)
def GameLoop():
gameExit = False
gameOver = False
prime_x = display_width/2
block = 3
fps=300
alien_x = random.randrange(50,300)
alien_y = random.randrange(50,300)
alien_health = 1
ship_health = 3
ship_damage = 1
bulletspeed = 0
bullet_y = 550
bullet_x = -50
bullet_size = 5
movement = 0
while gameOver == True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
if event.key == pygame.K_c:
GameLoop()
while not gameExit:
if prime_x <= 0:
prime_x = 0
if prime_x >= 790:
prime_x = 790
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
gameExit = True
gameQuit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
movement = -block
if event.key == pygame.K_RIGHT:
movement = block
if event.key == pygame.K_SPACE:
if bullet_y < 0:
bullet_y = 550
if bullet_y == 550:
bulletspeed = -bullet_size
bullet_x = prime_x
if bullet_y < 0:
bullet_y = 550
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or pygame.K_LEFT:
movement = 0
if alien_x < bullet_x < alien_x + 50 or alien_x < bullet_size + bullet_x < alien_x + 50:
if alien_y < bullet_y < alien_y + 50 or alien_y < bullet_size + bullet_y < alien_y + 50:
alien_health -= ship_damage
bulletspeed = 0
bullet_y = 550
bullet_x = -50
if alien_health <= 0:
alien_x = random.randrange(50,300)
alien_y = random.randrange(50,300)
alien_health = 1
prime_x += movement
bullet_y += bulletspeed
pygame.draw.rect(gameDisplay, green,[alien_x,alien_y,50 ,50 ] )
gameDisplay.blit(background, (0,0))
gameDisplay.blit(alien, (alien_x,alien_y))
gameDisplay.blit(ship, (prime_x-22,545))
pygame.draw.rect(gameDisplay, red, [bullet_x, bullet_y,bullet_size, bullet_size])
pygame.display.update()
clock.tick(fps)
Intro()
GameLoop()
pygame.quit()
sys.exit()
在Iexpress内部,我放置了:alien.png、bg.png、Game.exe、SDL.dll、SDL_IM~1.dll、SDL_MI~1、SDL_ttf.dll、ship.png、smpeg.dll
如果我在这里做错了什么,我深表歉意,这是我的第一个项目。
编辑:pygame.image.get_extended()返回1,使用Windows 8.1测试的Iexpress 32位和64位仍然无法工作。
我不能发表评论,所以这里有一个"答案"
你这条蟒蛇看起来不错。我认为它是IExpress,它很好,但故障排除可能有点棘手。以下是我想尝试的东西:
- 简单的一次检查可以确保文件不是空的,并且提取正确(文件大小、名称和其他属性)。其他进程没有打开该文件(IExpress没有暂停提取等)
- 检查IExpress压缩时的日志文件
- 检查IExpress配置文件(
*.sed
)中是否有UseLongFileName=1
- 检查以确保所有内容都提取正确
更改IExpress配置文件以具有以下设置ShowInstallProgramWindow=3 HideExtractAnimation=0 AppLaunched=cmd /C "echo begin&&pause&&echo end"
注意:确保您有ShowInstallProgramWindow=3
,这样您就希望有一个隐藏的进程等待用户输入——锁定资源 - 作为#4的变体,在退出之前更改安装命令以将临时文件夹复制到其他位置。
AppLaunched=cmd /C "copy * C:somefolder&&pause"
- 无聊的那个。创建一个pythong脚本来读取BMP文件,并逐字节检查是否存在差异,如下所示
import os org = open('original.bmp','rb') new = open('new.bmp','rb') size_compared = 0 comp_size = 1 MAX_SIZE = max(os.path.getsize(i) for i in ('original.bmp','new.bmp')) print 'comparing bytes...' while True: a = org.read[comp_size] b = new.read[comp_size] size_compared += comp_size if a!=b: print 'bytes don't match!' print '{} != {}'.format(a,b) print 'at byte {}'.format(size_compared) break if a=='' and b=='': break # files are empty if MAX_SIZE < size_compared: break # just in case... shouldn't happen... org.close() new.close() print 'done comparing... anything found?'
如果你有任何问题,请问-享受。