In this project, we'll continue animating 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'll start with our code from the last project (where we moved the aliens to the right), and we'll add moving the aliens back to the left and increasing the speed as they get closer and closer to the bottom of the LED Matrix screen.

To do this, we'll need to create two new variables to track the direction of the aliens (+1 or -1) and the speed of the aliens. Every time the aliens get to the edge of the screen, we'll move them down one row, and increase the alien speed. If the alien's row ever reaches 0, the aliens have won — ouch!

Here is what the code for this project should look like at this point (with new changes highlighted with arrows) — give it a try, and you should now see the aliens going back and forth, and marching down one row on each pass:

# ################################ # Initialize the game environment # ################################ # Import required functions/modules from rstem.accel import Accel from rstem.button import Button 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 alien_start_time = time.time() *alien_direction = 1 *alien_speed = 1 ALIENS_STEP_TIME = .8 # Initialize missiles fire_button = Button(12) missile_x, missile_y = -1, -1 MISSILE_COLOR = 10 MISSILE_STEP_TIME = 0.1 while True: # ################################ # Check for user input # ################################ # Get button input presses = fire_button.presses() # Get accelerometer input x_force, y_force, z_force = accel.forces() now = time.time() # ################################ # Update the game world # ################################ # Update the location of any missiles if missile_x >= 0 and now - missile_start_time > MISSILE_STEP_TIME: # Missile already launched - move it up missile_y += 1 if missile_y >= fb.height: missile_x, missile_y = -1, -1 missile_start_time = now elif presses: # Button was pressed - launch missile missile_x, missile_y = (round(spaceship_position), 1) missile_start_time = now # 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 * if now - alien_start_time > ALIENS_STEP_TIME / alien_speed: * alien_at_right_side = alien_direction > 0 and max(alien_columns) == fb.width - 1 * alien_at_left_side = alien_direction < 0 and min(alien_columns) == 0 * if alien_at_left_side or alien_at_right_side: * alien_row -= 1 * alien_speed *= 1.3 * alien_direction = - alien_direction * if alien_row == 0: * break * else: * alien_columns = [column + alien_direction for column in alien_columns] alien_start_time = now # Check for any missile collisions if missile_y == alien_row and missile_x in alien_columns: alien_columns.remove(missile_x) missile_x, missile_y = -1, -1 if not alien_columns: break # ################################ # Draw graphics on the screen # ################################ fb.erase() # Draw any missiles if missile_x >= 0: fb.point(missile_x, missile_y, MISSILE_COLOR) # 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 # ################################ if alien_columns: # Print "You lost..." if the player loses print("Ouch!") else: # Print "You lost..." if the player loses print("You win!")

1. Can you have the aliens start on the right side of the display (instead of the left)?

2. Can you increase the starting alien speed? How fast can you increase it and still win the game?

3. Our aliens move back and forth across the screen. Can you instead have them always move to the right, and when they hit the right side of the screen, restart them back at the left side?

4. Can you add a second row of aliens?