cx_freeze app opens then closes quickly

2019-09-13 07:39发布

i am trying to turn my python game into an exe file using cx_Freeze. The build finishes but i cannot open the app. I ran it in cmd and it says:

__import__(name+"__init__")
ImportError:No module named 'pygame.py__init__'

not sure what this means here is my setup code:

import cx_Freeze
import os
executables = [cx_Freeze.Executable("pygame.py.py")]
os.environ['TCL_LIBRARY'] = "C:\\New folder\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\New folder\\tcl\\tk8.6"

cx_Freeze.setup(
   name="Speed",
   options={"build_exe": {"packages":["pygame"],
                       "include_files":                                                                                 ["background.png","car3.png","car2.png","car.png","song.ogg","trees.png","coin.png","carblock2.png","carblock.png", "crash.ogg", "coin_sound.ogg"]}},
executables = executables,
version='3.1'



)

And here is the code for the game:

import pygame
import time
import random

pygame.init()


pygame.mixer.music.load('song.ogg')

displaywidth= 900
displayheight=600


black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
blue= (115,200,250)
grey= (155,155,155)
carwidth= 70

gamedisplay = pygame.display.set_mode((displaywidth,displayheight))
pygame.display.set_caption('Race')
clock = pygame.time.Clock()
FPS= 500
mouse=pygame.mouse.get_pos()

background=pygame.image.load('background.png').convert_alpha()



carblock=pygame.image.load('carblock.png').convert_alpha()

carblock2=pygame.image.load('carblock2.png').convert_alpha()


def start_screen():

    start=True
    while start:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key==pygame.K_1:
                    global carimage
                    carimage= pygame.image.load('car.png').convert_alpha()

                    start=False

            if event.key==pygame.K_2:
                global carimage
                carimage= pygame.image.load('car2.png').convert_alpha()


                start=False

            if event.key==pygame.K_3:
                global carimage
                carimage= pygame.image.load('car3.png').convert_alpha()



                start=False
    gamedisplay.fill(blue)

    carstart1= pygame.image.load('car.png').convert_alpha()
    gamedisplay.blit(carstart1,(150, 100))

    carstart2= pygame.image.load('car2.png').convert_alpha()
    gamedisplay.blit(carstart2,(425, 100))

    carstart3= pygame.image.load('car3.png').convert_alpha()
    gamedisplay.blit(carstart3,(700, 100))

    font = pygame.font.SysFont("comicsansms", 50)
    text= font.render('Pick your car, press 1, 2 or 3', True, black)
    gamedisplay.blit(text,(75,400))

    pygame.display.update()
    clock.tick(FPS)


def blocks_dodged(count):
     font = pygame.font.SysFont("comicsansms", 30)
     text= font.render('Dodged:  '+str(count), True, black)
     gamedisplay.blit(text,(0,0))

def coins_collected(count):
    font = pygame.font.SysFont("comicsansms", 30)
    text= font.render('Coins:  '+str(count), True, black)
    gamedisplay.blit(text,(0,40))

def coin(coinx, coiny, coinw, coinh):
    coin= pygame.image.load('coin.png').convert_alpha()
    gamedisplay.blit(coin, (coinx, coiny, coinw, coinh))    

def blocks(blockx, blocky, blockw, blockh):
    gamedisplay.blit(carblock, (blockx, blocky, blockw, blockh))

def blocks2(block2x, block2y, block2w, block2h):
    gamedisplay.blit(carblock2, (block2x, block2y, block2w, block2h))

def car(x,y):
    gamedisplay.blit(carimage, (x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.SysFont("comicsansms", 115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = (displaywidth/2, displayheight/2)
    gamedisplay.blit(TextSurf, TextRect)


pygame.display.update()

time.sleep(4)

game_loop()

def crash():
    pygame.mixer.music.stop()
    crash=pygame.mixer.Sound('crash.ogg')
    pygame.mixer.Sound.play(crash)
    message_display('You Crashed')

def trees(treesx, treesy, treesw, treesh):
    trees=pygame.image.load('trees.png').convert_alpha()    
    gamedisplay.blit(trees, (treesx, treesy, treesw, treesh))

def trees2(trees2x, trees2y, trees2w, trees2h):
    trees2=pygame.image.load('trees.png').convert_alpha()    
    gamedisplay.blit(trees2, (trees2x, trees2y, trees2w, trees2h))

def line(linex, liney, linew, lineh, color):
    pygame.draw.rect(gamedisplay, color, (linex, liney, linew, lineh))

def line2(line2x, line2y, line2w, line2h, color):
    pygame.draw.rect(gamedisplay, color, (line2x, line2y, line2w, line2h))


def game_loop():
    x=(displaywidth * 0.45)
    y=(displayheight * 0.8)
    pygame.mixer.music.play(-1)

    x_change = 0



    coin_startx=random.randrange(50, displaywidth-50)
    coin_starty = -600
   coin_speed = 25
   coin_width = 70
   coin_height = 70

   block_startx=random.randrange(50, displaywidth-50)
   block_starty = -600
   block_speed = 25
   block_width = 70
   block_height = 124

   block2_startx=random.randrange(50, displaywidth-50)
   block2_starty = -600
   block2_speed = 25
   block2_width = 70
   block2_height = 124

   trees_startx= 850
   trees_starty = -1200
   trees_speed = 25
   trees_width = 50
   trees_height = 1200

   trees2_startx= 0
   trees2_starty = -1800
   trees2_speed = 25
   trees2_width = 50
   trees2_height = 1200


   line_startx= 600
   line_starty = -200
   line_speed = 25
   line_width = 30
   line_height = 200

   line2_startx= 300
   line2_starty = -200
   line2_speed = 25
   line2_width = 30
   line2_height = 200

   dodged = 0
   coins = 0

   car_speed2= -15
   car_speed= 15

   Gameexit = False

   while not Gameexit:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               pygame.quit()
               quit()
           if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_LEFT:
                   x_change= car_speed2
               elif event.key == pygame.K_RIGHT:
                   x_change = car_speed

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

        x += x_change

        gamedisplay.blit(background, (0,0))

        trees(trees_startx, trees_starty, trees_width, trees_height)
        trees_starty += trees_speed
        trees2(trees2_startx, trees2_starty, trees2_width, trees2_height)


        trees2_starty += trees2_speed

        if trees_starty > displayheight:
            trees_starty = 0-1800

        if trees2_starty > displayheight:
            trees2_starty = 0-1200

        line(line_startx, line_starty, line_width, line_height, white)
        line_starty += line_speed

        if line_starty > displayheight:
            line_starty = 0-200

        line2(line2_startx, line2_starty, line2_width, line2_height, white)
        line2_starty += line2_speed

        if line2_starty > displayheight:
            line2_starty = 0-200


        blocks(block_startx, block_starty, block_width, block_height)
        block_starty += block_speed

        blocks2(block2_startx, block2_starty, block2_width, block2_height)
        block2_starty += block2_speed 





        blocks_dodged(dodged)
        car (x,y)

        if x > displaywidth-carwidth or x < 0:
            crash()

        if block_starty > displayheight:
            block_starty = 0-random.randint(200,400)
            block_startx= random.randrange(50, displaywidth-50)
            dodged+= 1
            block_speed += 0.6


        if y < block_starty+block_height-20:
            if x> block_startx and x < block_startx + block_width or         x+carwidth > block_startx and x + carwidth < block_startx + block_width:
            crash()

        if x > displaywidth-carwidth or x < 0:
            crash()

        #2nd car

        if block2_starty > displayheight:
            block2_starty = 0-random.randint(300,600)
            block2_startx= random.randrange(50, displaywidth-50)
            dodged+= 1
            block2_speed += 0.6


        if y < block2_starty+block2_height-20:
            if x> block2_startx and x < block2_startx + block2_width or x+carwidth > block2_startx and x + carwidth < block2_startx + block2_width:
                crash()

        coin(coin_startx, coin_starty, coin_width, coin_height)
        coins_collected(coins)

        coin_starty += coin_speed

        if coin_starty > displayheight:
            coin_starty = 0-random.randint(300,600)
            coin_startx= random.randrange(50, displaywidth-50)

        if y < coin_starty+coin_height-20:
            if x> coin_startx and x < coin_startx + coin_width or x+carwidth > coin_startx and x + carwidth < coin_startx + coin_width:
                coin_starty=670
                coin_sound=pygame.mixer.Sound('coin_sound.ogg')
                pygame.mixer.Sound.play(coin_sound)
                coins+=1

        if coins==5:
            font = pygame.font.SysFont("comicsansms", 100)
            text= font.render('Boost', True, red)
            gamedisplay.blit(text,(325,0))
            car_speed2=-20
            car_speed=20

        if coins==10:
            font = pygame.font.SysFont("comicsansms", 100)
            text= font.render('Boost', True, red)
            gamedisplay.blit(text,(325,0))
            car_speed2=-25
            car_speed=25

        if coins==15:
            font = pygame.font.SysFont("comicsansms", 100)
            text= font.render('Max Boost!', True, red)
            gamedisplay.blit(text,(275,0))
            car_speed2=-30
            car_speed=30        


        pygame.display.update()
        clock.tick(FPS)

start_screen()
game_loop()
pygame.quit()

Any help would be great

0条回答
登录 后发表回答