In this project, we'll finish up development of our Alien Intruders game by adding sound.

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

The very last step to completing our Alien Intruders game is adding sound/music to our game.

First, we import our sound functions and initialize variables that hold our notes and music:

from rstem.sound import Sound, Note from itertools import cycle fire_sound = Sound("fire.wav") hit_sound = Sound("hit.wav") notes = cycle([Note('B3'), Note('G3'), Note('E3'), Note('C3')])

Next, we insert the sounds at the various points in the program where they make sense. Specifically, when we fire a missile, when the aliens move and when an alien is hit by a missile.

# Button was pressed - launch missile fire_sound.play() # ... # Move alien next(notes).play(duration=0.20) # ... # Check for collision hit_sound.play()

Let's insert those code snippets into our full project, and we have a completed Alien Intruders game!

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

# ################################ # 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 *from rstem.sound import Sound, Note *from itertools import cycle 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 *# Initialize sounds *fire_sound = Sound("fire.wav") *hit_sound = Sound("hit.wav") *notes = cycle([Note('B3'), Note('G3'), Note('E3'), Note('C3')]) 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 * fire_sound.play() # 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: * next(notes).play(duration=0.20) 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 * hit_sound.play() 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. The small unamplified speaker that's in the kit is much better at producing high frequency sounds than low frequency sounds. Change the `Note`s in the game to be two octaves higher.

2. We used sounds "fire.wav" and "hit.wav" for the missile firing and explosion sounds. Can you change these to animal sounds?

3. Can you add another sound that occurs when a missile falls off the screen without hitting any aliens?

4. Start the aliens one row lower. Periodically, add a single alien dot that moves across the top of the display from left to right and then disappears. — this was a part of the original Space Invaders game.

5. Run multiple levels of the game, where each levels starts faster. At the end, display a score of how many levels were completed.

6. Change the controller to use left an right buttons instead of the accelerometer to move the spaceship.

7. Invert the controller to make a harder game — when the player tilts the kit left, move the spaceship right, and vice versa.

8. Do your own variation on this game, or create your own — the sky's the limit!