We'll use PyInstaller to convert the Pygame to an executable file so that it's easier to run.
In the root directory of your project, run the command in terminal/command line:
pip install pyinstaller
Check if it's installed by checking if the following command errors or not.
pyinstaller
There's a couple of parts in the Flappy game that errored when I tried to convert it to an .exe. These are the only changes you'll need to make (hopefully) so that it works.
Fonts: We need to add a font file and bundle it into the game. The font I used can be downloaded here: https://drive.google.com/file/d/1h88O-5b3JktXkWc0V31bgEahUfOe-OBC/view?usp=sharing
# Should be around line 10
SCREEN_WIDTH, SCREEN_HEIGHT = 500, 768
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
**FONT = pygame.font.Font('./assets/flappy.ttf', 144)**
pygame.display.set_caption("NEAT - Flappy Bird")
flap_sound = pygame.mixer.Sound("./assets/bird/wing.mp3")
point_sound = pygame.mixer.Sound("./assets/point.mp3")
**def display_score(score):
score_img = FONT.render("{}".format(score), True, (255, 255, 255))
score_rect = score_img.get_rect()
score_rect.center = (SCREEN_WIDTH // 2, 100)
SCREEN.blit(score_img, score_rect)**
Config file: We need to update the pathing to the config file when our game gets run as an executable.
import pygame, random, os, **sys**, neat
...
if __name__ == "__main__":
**if getattr(sys, 'frozen', False):
local_dir = os.path.dirname(sys.executable)
elif __file__:
local_dir = os.path.dirname(__file__)**
config_path = os.path.join(local_dir, 'config.txt')
run(config_path)
Run this command from the root of your game folder.
pyinstaller --onefile -w eval_genomes.py # alternatively, main.py
The .exe file should be in a new folder called dist. Drag this file into the root of the project directory and we're done!
Try running the .exe file and let me know if anything goes wrong.
We'll be creating a Github repo so it's easy to share your amazing project with anyone who wants to see it!