I tried to make an exe file of a Pygame code with the help of Pyinstaller. But when I run the exe file, it shows the above error. This error is showed only for Pygame code. When I run exe of simple Python code, it runs fine.
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()
Here is the code which I am converting to exe in the below path.
C:\Snakes\dist
Filename is Snake.py I am using python 3.9.4 and the latest versions of pygame and pyinstaller.
Please tell me the fix for this that why only Pygame code exe is not working.