Python 可执行问题

Python 可执行问题,python,pygame,exe,py2exe,sys,Python,Pygame,Exe,Py2exe,Sys,我用python 2.6编写了一个脚本。当我尝试在计算机上运行它(WindowsVista HomeBasic)时,它工作得非常好,但当我尝试在.exe中运行它时,问题就开始了。问题是,当我在其他计算机(Windows7和我认为新版本的WindowsVista)上单击可执行文件时,它出现了一个错误框,并且很快消失了。它消失得如此之快,以至于我无法读取所有的错误消息。但我看到消息的一部分说,第70行和第54行有一个错误,并且有一个“importError:DLL加载失败;找不到指定的模块“”。不知

我用python 2.6编写了一个脚本。当我尝试在计算机上运行它(WindowsVista HomeBasic)时,它工作得非常好,但当我尝试在.exe中运行它时,问题就开始了。问题是,当我在其他计算机(Windows7和我认为新版本的WindowsVista)上单击可执行文件时,它出现了一个错误框,并且很快消失了。它消失得如此之快,以至于我无法读取所有的错误消息。但我看到消息的一部分说,第70行和第54行有一个错误,并且有一个“importError:DLL加载失败;找不到指定的模块“”。不知什么原因,我觉得它很好。以防我使用py2exe(最新版本)将脚本制作成可执行文件。这些行在下面的脚本中高亮显示

import pygame, random, sys
from pygame.locals import *

WINDOWWIDTH = 600
WINDOWHEIGHT = 600
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
TEXTCOLOR = WHITE
BACKGROUNDCOLOR = BLACK
FPS = 40
ASTEROIDMINSIZE = 10
ASTEROIDMAXSIZE = 40
ASTEROIDMINSPEED = 1
ASTEROIDMAXSPEED = 8
ADDNEWASTEROIDRATE = 6
PLAYERMOVERATE = 5
MAXHEALTH = 3

def terminate():
    pygame.quit()
    sys.exit()

def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE: # pressing escape quits
                    terminate()
                return

def playerHasHitBaddie(playerRect, asteroids):
    for a in asteroids:
        if playerRect.colliderect(a['rect']):
            return True
    return False

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Dodger')
pygame.mouse.set_visible(False)

# set up fonts
(LINE 54)font = pygame.font.SysFont(None, 48)

# set up sounds
gameOverSound = pygame.mixer.Sound('gameover.wav')
pygame.mixer.music.load('background.mid')

# set up images
playerImage = pygame.image.load('player.png')
playerRect = playerImage.get_rect()
baddieImage = pygame.image.load('asteroid.png')
tokenImage = pygame.image.load('tokens.png')

# show the "Start" screen
drawText('Asteroid Shower', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press a key to start.', font, windowSurface, (WINDOWWIDTH / 3) - 30, (WINDOWHEIGHT / 3) + 50)
drawText('By Sandy Goetjens', font, windowSurface, (WINDOWWIDTH / 3) - 100, (WINDOWHEIGHT / 3) + 100)
(LINE 70)pygame.display.update()
waitForPlayerToPressKey()

topScore = 0
while True:
    # set up the start of the game
    asteroids = []
    score = 0
    playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
    moveLeft = moveRight = moveUp = moveDown = False
    reverseCheat = slowCheat = False
    asteroidAddCounter = 0
    pygame.mixer.music.play(-1, 0.0)

    while True: # the game loop runs while the game part is playing
        score += 1 # increase score

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

            if event.type == KEYDOWN:
                if event.key == ord('z'):
                    reverseCheat = True
                if event.key == ord('x'):
                    slowCheat = True
                if event.key == K_LEFT or event.key == ord('a'):
                    moveRight = False
                    moveLeft = True
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveLeft = False
                    moveRight = True
                if event.key == K_UP or event.key == ord('w'):
                    moveDown = False
                    moveUp = True
                if event.key == K_DOWN or event.key == ord('s'):
                    moveUp = False
                    moveDown = True

            if event.type == KEYUP:
                if event.key == ord('z'):
                    reverseCheat = False
                    score = 0
                if event.key == ord('x'):
                    slowCheat = False
                    score = 0
                if event.key == K_ESCAPE:
                        terminate()

                if event.key == K_LEFT or event.key == ord('a'):
                    moveLeft = False
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveRight = False
                if event.key == K_UP or event.key == ord('w'):
                    moveUp = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveDown = False

            if event.type == MOUSEMOTION:
                # If the mouse moves, move the player where the cursor is.
                playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)

        # Add new baddies at the top of the screen, if needed.
        if not reverseCheat and not slowCheat:
            asteroidAddCounter += 1
        if asteroidAddCounter == ADDNEWASTEROIDRATE:
            asteroidAddCounter = 0
            asteroidSize = random.randint(ASTEROIDMINSIZE, ASTEROIDMAXSIZE)
            newAsteroid = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-asteroidSize), 0 - asteroidSize, asteroidSize, asteroidSize),
                        'speed': random.randint(ASTEROIDMINSPEED, ASTEROIDMAXSPEED),
                        'surface':pygame.transform.scale(baddieImage, (asteroidSize, asteroidSize)),
                        }

            asteroids.append(newAsteroid)

        # Move the player around.
        if moveLeft and playerRect.left > 0:
            playerRect.move_ip(-1 * PLAYERMOVERATE, 0)
        if moveRight and playerRect.right < WINDOWWIDTH:
            playerRect.move_ip(PLAYERMOVERATE, 0)
        if moveUp and playerRect.top > 0:
            playerRect.move_ip(0, -1 * PLAYERMOVERATE)
        if moveDown and playerRect.bottom < WINDOWHEIGHT:
            playerRect.move_ip(0, PLAYERMOVERATE)

        # Move the mouse cursor to match the player.
        pygame.mouse.set_pos(playerRect.centerx, playerRect.centery)

        # Move the baddies down.
        for a in asteroids:
            if not reverseCheat and not slowCheat:
                a['rect'].move_ip(0, a['speed'])
            elif reverseCheat:
                a['rect'].move_ip(0, -5)
            elif slowCheat:
                a['rect'].move_ip(0, 1)

         # Delete baddies that have fallen past the bottom.
        for a in asteroids[:]:
            if a['rect'].top > WINDOWHEIGHT:
                asteroids.remove(a)

        # Draw the game world on the window.
        windowSurface.fill(BACKGROUNDCOLOR)

        # Draw the score and top score.
        drawText('Score: %s' % (score), font, windowSurface, 10, 0)
        drawText('Top Score: %s' % (topScore), font, windowSurface, 10, 40)

        # Draw the player's rectangle
        windowSurface.blit(playerImage, playerRect)

        # Draw each baddie
        for a in asteroids:
            windowSurface.blit(a['surface'], a['rect'])

        pygame.display.update()

        # Check if any of the baddies have hit the player.
        if playerHasHitBaddie(playerRect, asteroids):
            if score > topScore:
                topScore = score # set new top score
            break

        mainClock.tick(FPS)

    # Stop the game and show the "Game Over" screen.
    pygame.mixer.music.stop()
    gameOverSound.play()

    drawText('GAME OVER', font, windowSurface, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
    drawText('Press a key to play again.', font, windowSurface, (WINDOWWIDTH / 3) - 80, (WINDOWHEIGHT / 3) + 50)
    pygame.display.update()
    waitForPlayerToPressKey()

    gameOverSound.stop()
导入pygame,随机,系统
从pygame.locals导入*
窗宽=600
窗高=600
红色=(255,0,0)
白色=(255,255,255)
黑色=(0,0,0)
text颜色=白色
背景颜色=黑色
FPS=40
小行星尺寸=10
最大尺寸=40
最小速度=1
最大速度=8
AddNewRate=6
PLAYERMOVERATE=5
MAXHEALTH=3
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
终止()
如果event.type==KEYDOWN:
如果event.key==K_ESCAPE:#按ESCAPE退出
终止()
返回
def playerHasHitBaddie(playerRect,小行星):
对于小行星中的行星:
如果playerRect.colliderect(a['rect']):
返回真值
返回错误
def drawText(文本、字体、曲面、x、y):
textobj=font.render(text,1,TEXTCOLOR)
textrect=textobj.get_rect()
textrect.topleft=(x,y)
surface.blit(textobj,textrect)
#设置pygame、窗口和鼠标光标
pygame.init()
mainClock=pygame.time.Clock()
windowSurface=pygame.display.set_模式((窗口宽度、窗口高度))
pygame.display.set_标题('Dodger'))
pygame.mouse.set_可见(False)
#设置字体
(第54行)font=pygame.font.SysFont(无,48)
#设置声音
gameOverSound=pygame.mixer.Sound('gameover.wav'))
pygame.mixer.music.load('background.mid')
#设置图像
playerImage=pygame.image.load('player.png')
playerRect=playerImage.get_rect()
baddieImage=pygame.image.load('asteroid.png')
tokenImage=pygame.image.load('tokens.png')
#显示“开始”屏幕
drawText('小行星雨',字体,窗口表面,(窗口宽度/3),(窗口高度/3))
drawText('按键开始'),字体,窗口表面,(窗口宽度/3)-30,(窗口高度/3)+50)
drawText('By Sandy Goetjens',字体,窗面,(窗宽/3)-100,(窗高/3)+100)
(第70行)pygame.display.update()
waitForPlayerToPressKey()
上核=0
尽管如此:
#设置游戏的开始
小行星=[]
分数=0
playerRect.topleft=(窗口宽度/2,窗口高度-50)
moveLeft=moveRight=moveUp=moveDown=False
反向加热=慢速加热=假
计数器=0
pygame.mixer.music.play(-1,0.0)
while True:#游戏循环在游戏部分播放时运行
分数+=1#增加分数
对于pygame.event.get()中的事件:
如果event.type==退出:
终止()
如果event.type==KEYDOWN:
如果event.key==ord('z'):
reverseCheat=True
如果event.key==ord('x'):
慢热=真
如果event.key==K_LEFT或event.key==ord('a'):
moveRight=False
moveLeft=True
如果event.key==K_RIGHT或event.key==ord('d'):
moveLeft=False
moveRight=True
如果event.key==K_UP或event.key==ord('w'):
向下移动=错误
向上移动=真
如果event.key==K_DOWN或event.key==ord('s'):
moveUp=False
向下移动=真
如果event.type==KEYUP:
如果event.key==ord('z'):
反向热=假
分数=0
如果event.key==ord('x'):
慢热=假
分数=0
如果event.key==K_转义:
终止()
如果event.key==K_LEFT或event.key==ord('a'):
moveLeft=False
如果event.key==K_RIGHT或event.key==ord('d'):
moveRight=False
如果event.key==K_UP或event.key==ord('w'):
moveUp=False
如果event.key==K_DOWN或event.key==ord('s'):
向下移动=错误
如果event.type==MOUSEMOTION:
#如果鼠标移动,请将播放器移动到光标所在的位置。
playerRect.move_ip(事件位置[0]-playerRect.centerx,事件位置[1]-playerRect.centery)
#如果需要,在屏幕顶部添加新的坏蛋。
如果不是反向加热,也不是慢速加热:
计数器+=1
如果AddCounter==AddNewRate:
计数器=0
asteroidSize=random.randint(ASTEROIDMINSIZE,ASTEROIDMAXSIZE)
newAsteroid={'rect':pygame.rect(random.randint(0,WINDOWWIDTH asteroidSize),0-asteroidSize,asteroidSize,asteroidSize),
“速度”:random.randint(ASTEROIDMINSPEED,ASTEROIDMAXSPEED),
“曲面”:pygame.transform.scale(baddieImage,(小行星大小,小行星大小)),
}
小行星。附加(新小行星)
#移动玩家。
如果moveLeft和playerRect.left>0:
playerRect.移动ip(-1*playerOverate,0)
如果moveRight和playerRect.right<窗口宽度:
playerRect.移动(播放)
try:

    something_with_error

except:
    import traceback
    traceback.print_exc()
    input()