将 python 文件转换为 exe,但 exe 文件不会运行 [重复]

问题描述 投票:0回答:0

当我点击exe文件时,它会在关闭前打开一个窗口。此窗口的顶部栏显示文件地址。

Python 初学者,按照 youtube 视频构建游戏 Snake。我该如何解决这个问题。我的代码如下。游戏在 VSCode 中运行良好

import pygame
import time
from pygame.locals import *
import random

SIZE = 35
BACKGROUND_COLOUR = (110,110,5)

class Apple:
    def __init__(self, parent_screen):
        self.apple = pygame.image.load("Pictures/apple.png").convert()
        self.parent_screen = parent_screen
        self.x = SIZE*3
        self.y = SIZE*3

    def draw(self):
        self.parent_screen.blit(self.apple,(self.x,self.y))
        pygame.display.flip()

    def move(self):
        self.x = random.randint(0,22)*SIZE
        self.y = random.randint(0,18)*SIZE 

class Snake:
    def __init__(self,parent_screen, length):
        self.length = length
        self.parent_screen = parent_screen
        #Snake block image and starting coordinates
        self.block = pygame.image.load("Pictures/square.png").convert()
        self.x = [SIZE]*self.length
        self.y = [SIZE]*self.length
        self.direction = ''

    def increase_length(self):
        self.length +=1
        self.x.append(-1)
        self.y.append(-1)

    def draw(self):
        for i in range(self.length):
            self.parent_screen.blit(self.block,(self.x[i],self.y[i]))
        pygame.display.flip()
        
    def move_left(self):
        self.direction = 'left'

    def move_right(self):
        self.direction = 'right'

    def move_up(self):
        self.direction = 'up'

    def move_down(self):
        self.direction = 'down'

    def walk(self):
       
        for i in range(self.length-1,0,-1):
            self.x[i] = self.x[i - 1]
            self.y[i] = self.y[i - 1]

       
        if self.direction== 'left':
            self.x[0] -= SIZE
        if self.direction== 'right':
            self.x[0] += SIZE
        if self.direction== 'down':
            self.y[0] += SIZE
        if self.direction== 'up':
            self.y[0] -= SIZE

        self.draw()

class Game:
    def __init__(self):
        pygame.init()
        self.play_background_music()
        pygame.mixer.init()

        #Screen display size and colour
        self.surface = pygame.display.set_mode((980,700))
        self.surface.fill(BACKGROUND_COLOUR)
        self.snake = Snake(self.surface,1) 
        self.snake.draw()
        self.apple = Apple(self.surface)
        self.apple.draw()

    def is_collision(self,x1,y1,x2,y2):
        if x1>= x2 and x1< x2 + SIZE:
          if y1 >= y2 and y1 < y2 + SIZE:
            return True

        return False

    def out_of_bounds(self,x,y):
        if x<0 or x>980 or y<0 or y>700:
            return True

    def render_background(self):
        bg = pygame.image.load("Pictures/background.jpg")
        self.surface.blit(bg,(0,0))

    def play_background_music(self):
        pygame.mixer.music.load("Music/background.mp3")
        pygame.mixer.music.play()

    def play(self):
        self.render_background()
        self.snake.walk()
        self.apple.draw()
        self.display_score()
        pygame.display.flip()
        #snake colliding w apple
        if self.is_collision(self.snake.x[0], self.snake.y[0], self.apple.x, self.apple.y):
            sound = pygame.mixer.Sound("Music/eat_apple.mp3")
            pygame.mixer.Sound.play(sound)
            self.snake.increase_length()
            self.apple.move()
        
        #snake colliding with itself
        for i in range(3,self.snake.length):
            if self.is_collision(self.snake.x[0],self.snake.y[0],self.snake.x[i], self.snake.y[i]):
                sound = pygame.mixer.Sound("Music/you_lose.mp3")
                pygame.mixer.Sound.play(sound)
                raise "Game Over"

        if self.out_of_bounds(self.snake.x[0], self.snake.y[0]):
            sound = pygame.mixer.Sound("Music/you_lose.mp3")
            pygame.mixer.Sound.play(sound)
            raise "Game Over"

    def show_game_over(self):
        self.render_background()
        font = pygame.font.SysFont('arial', 30)
        line1 = font.render(f"GAME OVER! YOUR SCORE IS: {self.snake.length}", True, (255,255,255))
        self.surface.blit(line1, (200,300))
        line2 = font.render(f"To play again press Enter. To exit press Escape!", True, (255,255,255))
        self.surface.blit(line2, (200,350))
        pygame.display.flip()
        pygame.mixer.music.pause()

    def reset(self):
        self.snake = Snake(self.surface,1) 
        self.apple = Apple(self.surface)

    def display_score(self):
        font = pygame.font.SysFont('arial',30)
        score = font.render(f"SCORE: {self.snake.length}", True, (255,255,255))
        self.surface.blit(score, (805,10))
            

    def run(self):
        running = True
        pause = False

        while running:
            #key commands
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        running = False

                    if event.key == K_RETURN:
                        pygame.mixer.music.unpause()
                        pause = False

                    if not pause:                   
                        if event.key == K_UP:
                            self.snake.move_up()
                        if event.key == K_DOWN:
                            self.snake.move_down()
                        if event.key == K_LEFT:
                            self.snake.move_left()
                        if event.key == K_RIGHT:
                            self.snake.move_right()
                elif event.type == QUIT:
                    running = False
 
            try:
                if not pause:
                    self.play()
            except Exception as e:
                self.show_game_over()
                pause = True
                self.reset()

            time.sleep(0.2)


if __name__ == "__main__":
    game = Game()
    game.run()

我尝试重新安装 python 以包含 PATH,但这并没有改变我的问题。

python pygame exe
© www.soinside.com 2019 - 2024. All rights reserved.