In this project, we'll continue building our Alien Intruders game by animating our ship using a sprite.

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

We should now have a working Alien Intruders game, but we can make it more visually appealing by custom designing our ship using the Sprite functionality in the API that we learned about earlier.

To change the ship to a custom sprite, we need to include some basic initialization code, code to design the sprite configuration and code to draw the sprite.

Here is what the code for this project should look like at this point (with new changes highlighted with arrows) — when you run the code, you should now see our spaceship dot replaced with a nicer spaceship design:

# ################################ # Initialize the game environment # ################################ # Import required functions/modules from rstem.accel import Accel from rstem.button import Button from rstem.led_matrix import FrameBuffer *from rstem.led_matrix import Sprite import time # Initialize framebuffer fb = FrameBuffer() # Initialize accelerometer accel = Accel() # Initialize spaceship *spaceship = Sprite(''' * -F- * FAF * ''') *spaceship_middle = 1 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(7) 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 * spaceship_x = round(spaceship_position) - spaceship_middle * fb.draw(spaceship, origin=(spaceship_x, 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. Create your own `Sprite` that is five points wide and more than 2 points tall.

2. If you created your own wider `Sprite`, you'll notice that the missile will no longer shoot from the middle of the ship. Modify the code to have it shoot from the middle.

3. With a taller ship, you may notice that the missiles still shoot starting from the bottom row of the display. Change this so that missiles shoot starting from the top of the `Sprite`.

4. With a taller ship, aliens will continue marching down even when they've already touched the top of the ship. Modify the code so that the game ends right when the aliens get to the row that contains the top of the ship `Sprite`.