问题描述
我尝试在 Pyinstaller 的帮助下制作 Pygame 代码的 exe 文件。但是当我运行exe文件时,它显示了上述错误。此错误仅针对 Pygame 代码显示。当我运行简单的 Python 代码 exe 时,它运行良好。
import pygame
import random
import sys
pygame.init()
# Colors
white = (255,255,255)
red = (255,0)
black = (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 无法运行的解决方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)