python - 当我运行 pygame 代码 exe 时显示 Pyinstaller“无法执行脚本应用程序”错误
问题描述
我试图在 Pyinstaller 的帮助下制作一个 Pygame 代码的 exe 文件。但是当我运行exe文件时,它显示了上述错误。此错误仅针对 Pygame 代码显示。当我运行简单 Python 代码的 exe 时,它运行良好。
import pygame
import random
import sys
pygame.init()
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)
screen_width = 600
screen_height = 400
# Creating Window
gameWindow = pygame.display.set_mode((screen_width, screen_height))
# Game Title
pygame.display.set_caption(" Snake")
pygame.display.update()
# Game specific variable
exit_game = False
game_over = False
snake_x = 100
snake_y = 100
velocity_x = 0
velocity_y = 0
food_x = random.randint(20, screen_width-20)
food_y = random.randint(20, screen_height-20)
score = 0
snake_size = 10
fps = 30
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 55)
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, (x, y))
# Game Loop
while not exit_game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
velocity_x = 10
velocity_y = 0
if event.key == pygame.K_LEFT:
velocity_x = -10
velocity_y = 0
if event.key == pygame.K_UP:
velocity_y = -10
velocity_x = 0
if event.key == pygame.K_DOWN:
velocity_y = 10
velocity_x = 0
if event.key == pygame.K_SPACE:
velocity_x = 0
velocity_y = 0
snake_x += velocity_x
snake_y += velocity_y
gameWindow.fill(white)
pygame.draw.rect(gameWindow, black, [snake_x, snake_y, snake_size, snake_size])
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()
这是我在以下路径中转换为 exe 的代码。
C:\Snakes\dist
文件名是 Snake.py 我使用的是 python 3.9.4 和最新版本的 pygame 和 pyinstaller。
请告诉我为什么只有 Pygame 代码 exe 不起作用的解决方法。