0

我一直在努力开发一款游戏,目的是将其编译为 .exe,就像过去使用我的 wxPython 程序能够做到的那样,但是我研究过的 py2exe/pyinstllaer 和 pygame 不去一起非常棒。为了让我的 .exe 工作,我必须将所有 pygame dll/pyc 文件粘贴到 dist 文件夹中,一切正常!..除了我的游戏中的文本输入屏幕,它使用模块“输入框”,这对我的游戏来说是完美的,并且可以与 .pyw 文件一起使用。但是当我尝试在 .exe 中使用它时出现错误说的是:

运行时错误!应用程序已请求运行时以不寻常的方式终止它。

这是输入框模块:http ://www.pygame.org/pcr/inputbox/

这是 setup.py:

from distutils.core import setup
import py2exe
options={"py2exe": {"includes": ["inputbox"]}}
setup(windows=['PiBlocks.pyw'])

以防万一这里需要是我的游戏代码:

    #Init stuff
    import pygame,random
    from pygame.locals import *
    from collections import namedtuple
    import inputbox,time,subprocess,os,sys
    pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=500)
    f=open('texdir.txt','r')
    texdir=f.read()
    f.close()
    f=open(texdir+"\\splash.txt",'r')
    splash=f.read()
    splash=splash.replace('(','')
    splash=splash.replace(')','')
    splash=splash.split(',')
    f.close()
    splashlen=len(splash)
    chc=random.randint(0,int(splashlen))
    splash=splash[chc-1]
    f=open(texdir+"//backcolor.txt")
    pygame.init()
    clock=pygame.time.Clock()
    screen=pygame.display.set_mode((640,480))
    pygame.display.set_caption("PiBlocks | By Sam Tubb")
    max_gravity = 100
    blocksel=texdir+"\\dirt.png"
    backimg = pygame.image.load(texdir+"\\menu.png").convert()
    backimg = pygame.transform.scale(backimg, (640,480))
    clsimg = pygame.image.load("clear.bmp").convert()
    clsimg = pygame.transform.scale(clsimg, (640,480))
    ingame=0
    sbtn=pygame.image.load("startbtn.png").convert()
    qbtn=pygame.image.load("quitbtn.png").convert()
    tbtn=pygame.image.load("texbtn.png").convert()
    sbtnrect=sbtn.get_rect()
    sbtnrect.x=220
    sbtnrect.y=190
    qbtnrect=qbtn.get_rect()
    qbtnrect.x=220
    qbtnrect.y=225
    tbtnrect=tbtn.get_rect()
    tbtnrect.x=220
    tbtnrect.y=260
    go=0
    gotime=35
    #set cursor
    curs = pygame.image.load(texdir+"\\cursor.png").convert()
    curs.set_colorkey((0,255,0))

    #set backcolor
    COLOR=f.read()
    f.close()
    COLOR=COLOR.replace('(','')
    COLOR=COLOR.replace(')','')
    COLOR=COLOR.split(',')
    c1=COLOR[0]
    c2=COLOR[1]
    c3=COLOR[2]

    #load sounds
    place=pygame.mixer.Sound('sound\\place.wav')
    place2=pygame.mixer.Sound('sound\\place2.wav')
    place3=pygame.mixer.Sound('sound\\place3.wav')

    #set sprites and animation frames
    psprite = pygame.image.load(texdir+"\\player\\playr.png").convert()
    psprite.set_colorkey((0,255,0))
    psprite2 = pygame.image.load(texdir+"\\player\\playr2.png").convert()
    psprite2.set_colorkey((0,255,0))
    psprite3 = pygame.image.load(texdir+"\\player\\playr3.png").convert()
    psprite3.set_colorkey((0,255,0))
    anim=1
    class Block(object):
            def __init__(self,x,y,sprite):
                    if blocksel==texdir+"\\woodslab.png":
                           self.sprite = pygame.image.load(sprite).convert()
                           self.rect = self.sprite.get_rect(centery=y+8, centerx=x)
                    else:
                           self.sprite = pygame.image.load(sprite).convert_alpha()
                           self.rect = self.sprite.get_rect(centery=y, centerx=x)

    class Player(object):
        sprite=psprite
        def __init__(self, x, y):
            self.rect = self.sprite.get_rect(centery=y, centerx=x)
            # indicates that we are standing on the ground
            # and thus are "allowed" to jump
            self.on_ground = True
            self.xvel = 0
            self.yvel = 0
            self.jump_speed = 7
            self.move_speed = 3

        def update(self, move, blocks):

            # check if we can jump 
            if move.up and self.on_ground:
                self.yvel -= self.jump_speed

            # simple left/right movement
            if move.left:
                    self.xvel = -self.move_speed
            if move.right:
                    self.xvel = self.move_speed

            # if in the air, fall down
            if not self.on_ground:
                self.yvel += 0.3
                # but not too fast
                if self.yvel > max_gravity: self.yvel = max_gravity

            # if no left/right movement, x speed is 0, of course
            if not (move.left or move.right):
                self.xvel = 0

            # move horizontal, and check for horizontal collisions
            self.rect.left += self.xvel
            self.collide(self.xvel, 0, blocks)

            # move vertically, and check for vertical collisions
            self.rect.top += self.yvel
            self.on_ground = False;
            self.collide(0, self.yvel, blocks)

        def collide(self, xvel, yvel, blocks):
            # all blocks that we collide with
            for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:

                # if xvel is > 0, we know our right side bumped 
                # into the left side of a block etc.
                if xvel > 0: self.rect.right = block.rect.left
                if xvel < 0: self.rect.left = block.rect.right

                # if yvel > 0, we are falling, so if a collision happpens 
                # we know we hit the ground (remember, we seperated checking for
                # horizontal and vertical collision, so if yvel != 0, xvel is 0)
                if yvel > 0:
                    self.rect.bottom = block.rect.top
                    self.on_ground = True
                    self.yvel = 0
                # if yvel < 0 and a collision occurs, we bumped our head
                # on a block above us
                if yvel < 0: self.rect.top = block.rect.bottom

    colliding = False
    Move = namedtuple('Move', ['up', 'left', 'right'])
    player=[]
    blocklist=[]
    font=pygame.font.SysFont('arial',15)
    while True:
            if ingame==1:
                screen.fill((int(c1),int(c2),int(c3)))
                mse = pygame.mouse.get_pos()
                key = pygame.key.get_pressed()
                if key[K_a]:
                        anim+=1
                        if anim==9:
                                anim=1
                if key[K_d]:
                        anim+=1
                        if anim==9:
                                anim=1
                if key[K_1]:
                    blocksel=texdir+"\\dirt.png"
                if key[K_2]:
                    blocksel=texdir+"\\stonetile.png"
                if key[K_3]:
                    blocksel=texdir+"\\sand.png"
                if key[K_4]:
                    blocksel=texdir+"\\woodplank.png"
                if key[K_5]:
                    blocksel=texdir+"\\woodslab.png"
                if key[K_LEFT]:
                    try:
                            for b in blocklist:
                                b.rect.left+=32
                    except:
                            pass
                    try:
                            player.rect.left+=32
                    except:
                            pass
                if key[K_RIGHT]:
                        try:
                            for b in blocklist:
                                b.rect.left-=32
                        except:
                                pass
                        try:
                            player.rect.left-=32
                        except:
                                pass
                if key[K_UP]:
                        try:
                            for b in blocklist:
                                b.rect.top+=32
                        except:
                                pass
                        try:
                            player.rect.top+=32
                        except:
                                pass
                if key[K_DOWN]:
                        try:
                            for b in blocklist:
                                b.rect.top-=32
                        except:
                                pass
                        try:
                            player.rect.top-=32
                        except:
                                pass
                if key[K_ESCAPE]:
                    subprocess.call(['PiBlocks.exe'])
                    raise SystemExit
                for event in pygame.event.get():
                    if event.type == QUIT:
                        raise SystemExit

                    if key[K_LSHIFT]:
                        if event.type==MOUSEMOTION:
                            if not any(block.rect.collidepoint(mse) for block in blocklist):
                                snd=random.randint(1,3)
                                x=(int(mse[0]) / 32)*32
                                y=(int(mse[1]) / 32)*32
                                if go==1:
                                            if snd==1:
                                                place.play()
                                            elif snd==2:
                                                place2.play()
                                            elif snd==3:
                                                place3.play()
                                            blocklist.append(Block(x+16,y+16,blocksel))
                    if key[K_RSHIFT]:
                        if event.type==MOUSEMOTION:
                            to_remove = [b for b in blocklist if b.rect.collidepoint(mse)]
                            for b in to_remove:
                                    blocklist.remove(b)
                    else:
                        if event.type == pygame.MOUSEBUTTONUP:
                            if event.button == 1:
                                to_remove = [b for b in blocklist if b.rect.collidepoint(mse)]
                                for b in to_remove:
                                    blocklist.remove(b)

                                if not to_remove:
                                    snd=random.randint(1,3)
                                    x=(int(mse[0]) / 32)*32
                                    y=(int(mse[1]) / 32)*32
                                    if go==1:
                                            if snd==1:
                                                place.play()
                                            elif snd==2:
                                                place2.play()
                                            elif snd==3:
                                                place3.play()
                                            blocklist.append(Block(x+16,y+16,blocksel))

                            elif event.button == 3:
                                x=(int(mse[0]) / 32)*32
                                y=(int(mse[1]) / 32)*32
                                player=Player(x+16,y+16)

                move = Move(key[K_w], key[K_a], key[K_d])

                for b in blocklist:
                        screen.blit(b.sprite, b.rect)

                if player:
                    player.update(move, blocklist)
                    if anim==1 or anim==2 or anim==3:
                            screen.blit(psprite, player.rect)
                    elif anim==4 or anim==5 or anim==6:
                            screen.blit(psprite2, player.rect)
                    elif anim==7 or anim==8 or anim==9:
                            screen.blit(psprite3, player.rect)
                x=(int(mse[0]) / 32)*32
                y=(int(mse[1]) / 32)*32
                screen.blit(curs,(x,y))
                clock.tick(60)
                x=blocksel.replace(texdir,'')
                x=x.replace('.png','')
                vers=font.render('PiBlocks Alpha 0.4',True,(255,255,255))
                tex=font.render('Selected Texture Pack: '+texdir,True,(255,255,255))
                words=font.render('Selected Block: '+str(x), True, (255,255,255))
                screen.blit(vers,(1,1))
                screen.blit(tex,(1,12))
                screen.blit(words,(1,25))
                if gotime==0:
                        go=1
                else:
                        gotime-=1
                pygame.display.flip()
            elif ingame==0:
                    blocklist=[]
                    mse = pygame.mouse.get_pos()
                    player=[]
                    key = pygame.key.get_pressed()
                    text=font.render(splash, True, (255,255,255))
                    if key[K_RETURN]:
                            ingame=1
                    for event in pygame.event.get():
                            if event.type == QUIT:
                                raise SystemExit
                            if event.type == KEYDOWN:
                                    print event.key
                    if sbtnrect.collidepoint(mse):
                            if pygame.mouse.get_pressed()==(1,0,0):
                                    go=0
                                    ingame=1
                    if qbtnrect.collidepoint(mse):
                            if pygame.mouse.get_pressed()==(1,0,0):
                                    raise SystemExit
                    if tbtnrect.collidepoint(mse):
                            if pygame.mouse.get_pressed()==(1,0,0):
                                    ingame='texsel'
                    screen.blit(backimg,(0,0))
                    screen.blit(text,(364,76))
                    screen.blit(sbtn,sbtnrect)
                    screen.blit(qbtn,qbtnrect)
                    screen.blit(tbtn,tbtnrect)
                    pygame.display.flip()
            elif ingame=='texsel':
                     screen.blit(clsimg,(0,0))
                     inp = inputbox.ask(screen, 'Texture Directory')
                     f=open('texdir.txt','w')
                     f.write(str(inp))
                     f.close()
                     pygame.display.flip()
                     subprocess.call(['PiBlocks.exe'])
                     raise SystemExit
4

0 回答 0