# Intro to Alien Intruders

In this project, we kick off the development of an LED Matrix game we call "Alien Intruders." We'll start by writing the pseudo-code in this concept and then build the game over the next several projects.

You're may be familiar with the game Space Invaders®, which was originally made popular in the 1970s in arcades and on the Atari (one of the first home gaming systems). When it first came out, this game was pretty revolutionary. The game involves the user controlling a ship that can move left and right — the ship shoots missiles at alien invaders scrolling from the top of the screen towards the bottom.

Over the next 10 projects, we're going to build a game that is a simplified version of Space Invaders® using many of the Ready Set STEM CREATOR Kit components that we've already learned to wire up. The LED Matrix will be used to display the graphics, the accelerometer will be used to control the left and right movement of the ship (by tilting the kit left or right) and a single button will be used to fire missiles at the invading aliens. By the time we've completed this project, you should have the basic skills necessary to build more complex games.

Like most games you'll build, this one is going to start with a game loop, and in this first Alien Intruders project, we'll start by writing the pseudo-code for our game loop (see for more information on both pseudo-code and game loops). In our experience, the best way to write pseudo-code is in an iterative process, meaning that we write the very high level steps out first, and then we break each of those steps into smaller chunks with more details. We continue doing this until we have enough detail that we're ready to actually start writing our code.

# Our Game Loop

Let's start with the basic outline for any game loop, much as we described in :

    Initialize the game environment

    while (game not finished):
        Check for user input
        Update the game world
        Draw graphics on the screen
        Wait for some period of time

    Finish & Cleanup

Now, let's look at each of these areas individually and determine what we'll need to do in each of these areas to bring our Alien Invaders game to life...

## Initialize the game environment

This is where we're going to initialize all of the components and game elements that we'll be using throughout our game. In our Alien Intruders implementation, the three major components we'll have are the ship that we control ("spaceship"), the alien ships that we try to destroy ("aliens") and the missiles that we fire from our spaceship at the aliens ("missiles"). We'll need to initialize the design, position and other aspects of each of those components, as well as general setup, like importing the functions we'll need and initializing the framebuffer.

Here's a general overview of what this part of the code will look like:

    # Initialize the game environment
    Import required functions/modules
    Initialize framebuffer
    Initialize accelerometer
    Initialize spaceship
    Initialize aliens
    Initialize missiles

There will be other details that we'll add in there as well, but these are the big items we'll need to address in our initialization code.

## while (game not finished):

This will be the start of our game loop. For this game, we'll loop continuously until the game is completed (either you destroy all the aliens or they get to the bottom of the screen and destroy you), at which point we can break from our while loop.

So, our loop will just be:

    while True:

## Check for user input

Unlike our previous game loops, for this game we'll be taking user input. Specifically, we'll be reading data from the accelerometer to know which direction the game is being tilted and we'll be checking the button to determine if a missile was fired. This part of the code will look something like:

    # Check for user input
    Get accelerometer input
    Get button input

## Update the game world

Each time through the loop, we'll have to update the status of all the three major components of the game — the spaceship, the aliens and the missiles. In addition, we'll want to test to see if any of the previously launched missiles has collided with an alien.

With that in mind, this section of the code will look something like:

    # Update the game world
    Update the location of the spaceship
    Update the location of the aliens
    Update the location of any missiles
    Check for any missile collisions

Now that we have updated information for each of the components of our game, it's time to update the graphics based on the new information. This involves erasing the framebuffer, writing the new graphics to the framebuffer and then showing the revised framebuffer. Here's what that will look like in our code:

    # Draw graphics on the screen
    Erase the framebuffer
    Draw the spaceship
    Draw the aliens
    Draw any missiles
    Show the framebuffer

## Wait for some period of time

At the end of our game loop, we need to include a pause before continuing our next round through the loop. For this game, our graphics aren't going to (necessarily) update each time through the loop, so our wait time doesn't need to be correlated to our graphics updates. That said, we need to pick a time that is not too long — otherwise the graphics will not be as responsive as we'd like — and also not too short — or we'll needlessly overwork the processor.

We'll play around with various wait times and see what works, but typically, we'd expect something in the .001 to .1 seconds time frame...

## Finish & Cleanup

Finally, we need to handle the case where we fall through the loop, either because we've won the game (destroyed all the aliens) or lost the game (the aliens got to the bottom of the screen). In either case, we'll at least print the final game status (win or lose) to the output window.

    # Finish & Cleanup
    Print "You won!" if the player wins
    Print "You lost..." if the player loses

## Putting It All Together

Here is what the full pseudo-code for this game will look like... we'll spend the next projects building it piece-by-piece:

    # Initialize the game environment
    Import required functions/modules
    Initialize framebuffer
    Initialize accelerometer
    Initialize spaceship
    Initialize aliens
    Initialize missiles

    while True:
        # Check for user input
        Get accelerometer input
        Get button input

        # Update the game world
        Update the location of the spaceship
        Update the location of the aliens
        Update the location of any missiles
        Check for any missile collisions

        # Draw graphics on the screen
        Erase the framebuffer
        Draw the spaceship
        Draw the aliens
        Draw any missiles
        Show the framebuffer

        # Wait for some period of time
        Wait for between .001 and .01 seconds

    # Finish & Cleanup
    Print "You won!" if the player wins
    Print "You lost..." if the player loses