0

I just make a small game. And want to make it as .exe file.

so I use pyinstaller -w -F ."file_name". But "Fail to execute script error".

I try to make a game and send it to my friends, but the same error keeps happening.

I wonder why this problem occurs when the external image is not being imported and used.

Need your help. May I ask your favor.

here is a source code

import pygame
from random import *

def setup(level):
    global display_time
    display_time = 10 - (level // 3)
    display_time = max(display_time, 5)


    number_count = (level //3) +5
    number_count = min(number_count, 20)

    shuffle_grid(number_count)

def shuffle_grid(number_count):
    rows = 5
    columns = 9

    cell_size = 130 
    button_size = 110

    screen_left_margin = 55
    screen_top_margin = 20



    grid = [[0 for col in range(columns)] for row in range(rows)] # 5*9

    number = 1
    while number <= number_count:
        row_idx = randrange(0,rows) 
        col_idx = randrange(0,columns) 

        if grid[row_idx][col_idx] == 0:
            grid[row_idx][col_idx] =  number 
            number += 1

            center_x = screen_left_margin + (col_idx * cell_size) + (cell_size / 2)
            center_y = screen_top_margin + (row_idx * cell_size) + (cell_size / 2)
            button = pygame.Rect(0,0,button_size, button_size)
            button.center = (center_x,center_y)

            number_buttons.append(button)
    print(grid)

def display_start_screen():
    pygame.draw.circle(screen,WHITE,start_button.center, 60,5)

    msg = game_font.render(f"{curr_level}", True, WHITE)
    msg_rect = msg.get_rect(center =start_button.center)
    screen.blit(msg,msg_rect)

def display_game_screen():
    global hidden

    if not hidden:
        elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
        if elapsed_time > display_time:
            hidden = True

    for idx, rect in enumerate(number_buttons, start = 1):
        if hidden:
            pygame.draw.rect(screen, WHITE, rect)
        else:
            cell_text = game_font.render(str(idx), True, WHITE)
            text_rect = cell_text.get_rect(center = rect.center)
            screen.blit(cell_text, text_rect)

def check_buttons(pos):
    global start, start_ticks
    if start:
        check_number_buttons(pos)
    elif start_button.collidepoint(pos):
        start = True
        start_ticks = pygame.time.get_ticks()

def check_number_buttons(pos):
    global start, hidden, curr_level

    for button in number_buttons:
        if button.collidepoint(pos):
            if button == number_buttons[0]:
                print("Correct!")
                del number_buttons[0]
                if not hidden:
                    hidden = True
            else:
                game_over()
            break
    if len(number_buttons) == 0:
        start = False
        hidden = False
        curr_level += 1
        setup(curr_level)

def game_over():
    global running
    running = False
    msg1 = game_font.render(f"Your level is {curr_level}", True, WHITE)
    msg1_rect = msg1.get_rect(center =(screen_width/2, screen_height/2))
    msg2 = game_font.render(f"GAME OVER", True, RED)
    msg2_rect = msg2.get_rect(center =(screen_width/2, screen_height/3))
    screen.fill(BLACK)
    screen.blit(msg1,msg1_rect)
    screen.blit(msg2,msg2_rect)

pygame.init()
screen_width = 1280
screen_height = 720
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Memory Game")
game_font = pygame.font.Font(None, 120)

start_button = pygame.Rect(0,0,120,120)
start_button.center = (120,screen_height- 120)

BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)

number_buttons = []
curr_level = 1 
display_time = None 
start_ticks = None 

start = False

hidden = False

setup(curr_level)

running = True 
while running :
    click_pos = None
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running = False 
        elif event.type == pygame.MOUSEBUTTONUP: 
            click_pos = pygame.mouse.get_pos()
            print(click_pos)
    
    screen.fill(BLACK)
    if start: 
        display_game_screen()
    else:
        display_start_screen()
    
    if click_pos:
        check_buttons(click_pos)
    
    pygame.display.update()

pygame.time.delay(5000)

pygame.quit()
Joachim
  • 3
  • 1
  • Can you post the entire error so that we can see all the details? You mention something about an image not being found... "the external image is not being imported and used" but I'm not sure what that means? – dstricks Apr 20 '21 at 00:51
  • sorry. I meaned "I did;t use any image file and gif file. just use code." error in exe_file is... "Fatal error detected" "Failed to execute script 'file_name'" – Joachim Apr 20 '21 at 02:25
  • 2
    what pygame version are You on? it could be an issue with `.init()`. Does otherwise the program work fine (when run in terminal/IDE)? You could also try running the file in terminal if You haven't yet tried that. Last time I had a similar issue it was with `font.init()`. Also for debugging You can try applying the method of copying the files to a new directory and keep stripping away the code and each time convert to `.exe` until it runs so that You know where the error lies, also You can try a bit older pygame version (if You have 2.0.1 try something like 2.0.0) – Matiiss Apr 20 '21 at 07:37
  • thx for your answer. I use latest version in pygame and It works in terminal. but not in ,exe by Pyinstaller. I will try to user older version's pygame. thank you. – Joachim Apr 21 '21 at 01:28

0 Answers0