In this project, we continue the development of our Alien Intruders game by adding in the aliens.

The hardware setup for this project should be the same setup that was completed for the project. Here's a reminder:

In this project, we're going to add our aliens into the code.

The first thing we'll want to do is keep track of the aliens. We're going to have four aliens in our game (you're welcome to change that if you'd like), and we'll need to keep track of both what columns they are in and what row they are in at any given time.

We'll do that by adding a list to track their columns (they'll each be in their own column) and a variable to track their row (they'll all be in the same row). We'll start the aliens at the top of the display (the height of the display minus 1):

alien_columns = [0, 1, 2, 3] alien_row = fb.height - 1

Next, we need to add code to draw the aliens each time through the loop:

# Draw aliens for column in alien_columns: fb.point(column, alien_row)

The aliens are not moving yet, but at least they are displayed!

Here is what the code for this project should look like at this point (with new changes highlighted with arrows):

# ################################ # Initialize the game environment # ################################ # Import required functions/modules from rstem.accel import Accel from rstem.led_matrix import FrameBuffer import time # Initialize framebuffer fb = FrameBuffer() # Initialize accelerometer accel = Accel() # Initialize spaceship spaceship_position = fb.width / 2 TILT_FORCE = 0.1 SPACESHIP_STEP = 0.1 # Initialize aliens *alien_columns = [0, 1, 2, 3] *alien_row = fb.height - 1 # Initialize missiles while True: # ################################ # Check for user input # ################################ # Get button input # Get accelerometer input x_force, y_force, z_force = accel.forces() # ################################ # Update the game world # ################################ # Update the location of any missiles # Update the location of the spaceship if x_force > TILT_FORCE: spaceship_position -= SPACESHIP_STEP elif x_force < -TILT_FORCE: spaceship_position += SPACESHIP_STEP spaceship_position = max(0, min(fb.width - 1, spaceship_position)) # Update the location of the aliens # Check for any missile collisions # ################################ # Draw graphics on the screen # ################################ fb.erase() # Draw any missiles # Draw the spaceship fb.point(round(spaceship_position), 0) # Draw the aliens * for column in alien_columns: * fb.point(column, alien_row) 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

1. We store the positions of the aliens in the variable `alien_columns`, which is a list of column numbers. Can you create this same list using the `range()` function?

2. Instead of four aliens right next to each other, can you make three aliens that are separated from each other?

3. The aliens start at the top row. Can you change this to have them randomly start in one of the top three rows?