Step 0: Installation/Setup

  1. Pull from the repo

    git clone <https://github.com/allengu01/flappy-starter.git> <name of folder>
    
  2. Installing Python

    Make sure you have Python 3.7.7 by opening command line and using:

    python3 --version
    

    If not, follow these instructions to install Python.

    Linux (Ubuntu): sudo apt install python3

    macOS: Download from this website: Python 3

    Windows: Download from this website: Python 3

  3. Setting Up a Virtual Environment

    Python virtual environments let us separate and manage different environments for each project we create. Why do we need them?

    python3 -m venv .venv
    source .venv/bin/activate
    deactivate
    

    If you want to learn more about Python virtual environments, take a look here: https://realpython.com/python-virtual-environments-a-primer/

  4. Installing Pygame

    Pygame will be the package we use to create our game.

    python3 -m pip install -U pygame
    python3 -m pygame.examples.aliens
    

    If running the demo game works, you're good to go.

  5. Installing NEAT-Python

    NEAT-Python is a package that implements the NEAT (Neuro-Evolution of Augmenting Topologies) algorithm. The algorithm will be used to teach the machine how to play the game you develop.

    pip install neat-python
    

Step 2: Starting the first Pygame file

  1. Create a file called main.py
  2. Import Pygame
  3. Define the screen
  4. Add a caption for the window screen (this is what shows up in the top bar)
  5. Create a main method
    1. Create a while loop that will run until the game is quit.
    2. The first thing we do in the loop is always to check for any events and handle them. For now, the only event we’ll look at is whether the game has been quit yet.
    3. After checking for all the events, we’re going to fill the screen to white (RGB = 255, 255, 255) and then tell the display to update.
    4. Finally we initialize a clock at the start of main, which get's called once per iteration. "clock.tick(FPS)" controls the max frame rate (frames/second) of the game, i.e. how often the screen gets updated per second.
  6. Lastly, we’ll add this section at the bottom that checks if name == “main”. This code means that the program only runs the main function if the main.py file is the file being run (as compared to being called for an import).
import pygame;
pygame.init()

SCREEN_WIDTH, SCREEN_HEIGHT = 500, 768
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("NEAT - Flappy Bird")

def main():
   run = True
   clock = pygame.time.Clock()

   while run:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               run = False
               pygame.quit()
       SCREEN.fill((255, 255, 255))
       pygame.display.update()
       clock.tick(60)

if __name__ == "__main__":
   main()