In this project, we continue the development of our Alien Intruders game by building the complete circuit, writing our initialization code and writing the code to do some basic LED Matrix drawing.

In our previous project, we discussed the pseudo-code for our Alien Intruders game. Before we start writing the code, we need to complete the circuit for the game.

For our Alien Intruders game, we'll be using the LED Matrix for our display, the Accelerometer to control the left/right movement of the ship and a button to fire missiles.

If you completed the previous projects with the LED Matrix and the Accelerometer, your breadboard is already configured mostly how it needs to be. Though, at very least, you'll probably need to add a button to the configuration (we're going to assume that the button is wired to GPIO7 for this project).

Here is what the breadboard should look like once it's correctly wired:

Building on the pseudo-code we wrote in the last project, let's initialize the framebuffer and write a single dot to the LED Matrix (we'll consider this our spaceship for now). While none of this is new, it will allow us to verify that the LED Matrix is working properly and will get some of our initialization code out of the way.

Here's what our initialization code should look like for the framebuffer:

# Import required functions/modules from rstem.led_matrix import FrameBuffer # Initialize the framebuffer fb = FrameBuffer()

Next, we'll write some code to place a single dot (our current spaceship) on the LED Matrix:

fb.erase() # Draw the spaceship fb.point(3,0) fb.show()

If you run the program at this point, you should see a dot on the LED Matrix at coordinates (3,0).

Here is the full code for our game at this point:

# ################################ # Initialize the game environment # ################################ # Import required functions/modules from rstem.led_matrix import FrameBuffer # Initialize framebuffer fb = FrameBuffer() # Initialize accelerometer # Initialize spaceship # Initialize aliens # Initialize missiles while True: # ################################ # Check for user input # ################################ # Get button input # Get accelerometer input # ################################ # Update the game world # ################################ # Update the location of any missiles # Update the location of the spaceship # Update the location of the aliens # Check for any missile collisions # ################################ # Draw graphics on the screen # ################################ fb.erase() # Draw the spaceship fb.point(3,0) # Draw the aliens # Draw any missiles fb.show() # Wait briefly time.sleep(0.001) # ################################ # Finish and cleanup # ################################ # Print "You won!" if the player wins # Print "You lost..." if the player loses