如何修复“警告:找不到隐藏的导入“ pygame._view”!”我将.py程序转换为.exe之后?

时间:2019-12-15 19:45:20

标签: python pygame exe pyinstaller

当我使用if语句中的行作为exe运行程序时:“ if start == True:” 被注释掉,而 last else语句被注释掉然后程序运行无错误。但是,当未注释掉时,它会附带错误,并且在运行程序时会崩溃。我认为这与 message_display()函数有关,但我不确定...


import pygame, random, time
from creating_grid import *
from snake_class import *

## --- Creating Apple Location ---
def appleLocation(grid):
    appleY = random.randint(0,(len(grid)-1))
    appleX = random.randint(0,(len(grid[0])-1))
    while grid[appleY][appleX] != None:
        appleY = random.randint(0,(len(grid)-1))
        appleX = random.randint(0,(len(grid[0])-1)) 
    apple = snake(appleY,appleX)
    grid[appleY][appleX] = "apple"
    return grid, apple

## --- Creating the window ---
pygame.init()

winWidth = 800
winHeight = 400

win = pygame.display.set_mode((winWidth,winHeight))

pygame.display.set_caption("SK_24")


## --- Creating a grid and square ---
squareSize = 20

grid = createGrid(squareSize,winWidth,winHeight)
backGrid = createGrid(squareSize,winWidth,winHeight)
for i in range(random.randint(30,40)):
    backGrid[random.randint(0,len(backGrid))-1][random.randint(0,len(backGrid[0]))-1] = i


## --- Text Creator ---
def text_objects(text, font):
    textSurface = font.render(text, True, (255,255,255))
    return textSurface, textSurface.get_rect()

def message_display(text,textSize = 50,lower = False):
    largeText = pygame.font.Font('freesansbold.ttf',textSize)
    TextSurf, TextRect = text_objects(text, largeText)
    if lower == False:
        TextRect.center = ((winWidth/2),(winHeight/2))
    else:
        TextRect.center = ((winWidth/2),(winHeight/2+55))
    win.blit(TextSurf, TextRect)

## --- Creating snake and placing on grid ---
body = {}
startSize = 5

for i in range(startSize):
    body[i] = snake(6,6-i)

for i in body:
    grid[body[i].posY][body[i].posX] = i

direction = "down"

grid, apple = appleLocation(grid)

start = True


## --- Updating the window ---
run = True
while run:
    pygame.time.delay(100) 

    if start == True:
        message_display("Snake Game")
        pygame.display.update()
        time.sleep(2)
        win.fill((0,0,0))
        message_display("Starting in...")
        pygame.display.update()
        time.sleep(1)
        win.fill((0,0,0))
        message_display("3")
        pygame.display.update()
        time.sleep(1)
        win.fill((0,0,0))
        message_display("2")
        pygame.display.update()
        time.sleep(1)
        win.fill((0,0,0))
        message_display("1")
        pygame.display.update()
        time.sleep(1)
        win.fill((0,0,0))
        start = False

    move = False
    for i in body:
        if i != 0:
            if body[0].posY == body[i].posY and body[0].posX == body[i].posX:
                run = False

    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if direction != "right" and move == False:
                    direction = "left"

            if event.key == pygame.K_RIGHT:
                if direction != "left" and move == False:
                    direction = "right"

            if event.key == pygame.K_UP:
                if direction != "down" and move == False:
                    direction = "up"

            if event.key == pygame.K_DOWN:
                if direction != "up" and move == False:
                    direction = "down"
            move = True

    run, preX, preY = body[0].move(grid,body,direction,run)

    win.fill((154,205,50))
    for j in backGrid:
        for each in j:
            if each != None:
                backY = int(backGrid.index(j))
                backX = int(backGrid[backY].index(each))
                pygame.draw.rect(win,(144,195,40),(backX*squareSize,backY*squareSize,squareSize,squareSize))

    if body[0].posY == apple.posX and body[0].posX == apple.posY:
        grid, apple = appleLocation(grid)
        num = len(body)
        body[num] = snake(preX,preY)

    ## Visualising the grid
    for eachRow in grid:
        for eachItem in eachRow:
            if eachItem != None:
                if eachItem != "apple":
                    y = grid.index(eachRow)
                    x = grid[y].index(eachItem)
                    y = (y)*squareSize
                    x = (x)*squareSize
                    pygame.draw.rect(win,(128,128,0),(x,y,squareSize,squareSize))
                else:
                    pygame.draw.rect(win,(255,0,0),(apple.posY*squareSize,apple.posX*squareSize,squareSize,squareSize))
    if run == True:
        pygame.display.update()
    else:
        highScoreFile = open("high_score.txt","r+")
        highScore = int(highScoreFile.read())
        newScore = (len(body)-startSize)*100

        if newScore > highScore:
            open('high_score.txt', 'w').close()
            highScoreFile = open("high_score.txt","r+")
            highScoreFile.write(str(newScore))
            highScoreFile.close()

        message_display("Game Over...")
        pygame.display.update()
        time.sleep(2)
        win.fill((0,0,0))
        highScore = "High Score: " + str(highScore)
        message_display(highScore,30,True)
        score = "Score: "+str(newScore)
        message_display(score)
        pygame.display.update()
        time.sleep(4)
        win.fill((0,0,0))

pygame.quit()

0 个答案:

没有答案