We'll use PyInstaller to convert the Pygame to an executable file so that it's easier to run.

Installing PyInstaller

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

Fixing the Flappy Game

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.

  1. 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

    1. Add this font into the assets folder
    2. In main.py and eval_genomes.py, update the code:
    # 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)**
    
  2. Config file: We need to update the pathing to the config file when our game gets run as an executable.

    1. Add sys as an import
    2. Update the code at the very bottom of the eval_genomes file to be:
    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)
    

Converting to Executable

  1. Run this command from the root of your game folder.

    pyinstaller --onefile -w eval_genomes.py # alternatively, main.py
    
  2. 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!

  3. Try running the .exe file and let me know if anything goes wrong.

Creating a Github Repo

We'll be creating a Github repo so it's easy to share your amazing project with anyone who wants to see it!

  1. On Github, click the plus button on the top right → "New repository"

Screen Shot 2021-11-26 at 9.20.52 PM.png

  1. Give the repository a name, check the box that is next to "Public", then click "Create repository"
  2. Initialize the Git repository for your project using this command in the project folder.