In this project, we continue the development of our Alien Intruders game by initializing and getting movement input from our accelerometer.

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

In the last project, we initialized our ship where it's supposed to start and we created the loop for our game loop. In this project, we're going to add the accelerometer and use it to get input from the user about which direction to move our ship (tilting the Ready Set STEM CREATOR Kit left will move the ship left and tilting right will move the ship right). In this project, we'll use that input to animate the movement of our ship.

As we learned in , the initialization code for the accelerometer will consist of importing the `Accel()` function and then initializing the accelerometer:

from rstem.accel import Accel accel = Accel()

We also need to create and define two other variables that we'll need at this point:

- TILT_FORCE: If we tilt the accelerometer right or left, it will register an X force greater than 0 (if we tilt left) or less than 0 (if we tilt right). In theory, any value greater than 0 is a left tilt and any value less than 0 is a right tilt. But, because the accelerometer is very sensitive, even if we try to hold it perfectly flat, it will likely register at least a tiny bit of tilt. So, to give us a margin of error, we're going to assume that the accelerometer needs to be tilted at least a certain amount before we register it as a tilt. If we want the game to be very sensitive to tilting, we can set this value close to 0 (so just a small tilt will register); if we don't want the game to be very sensitive, we can set the value close to 1 (so only a large tilt will register). And we'll start by setting the value to .1 and see how that works for us.

- SPACESHIP_STEP: As we register tilt in one direction or the other, we need to have the ship move in that direction. And we need to tell it how far to move during one pass through the game loop if a tilt is registered. If we move it too far each time, the ship will appear to move very fast and we'll feel like we have no control over it; if we move it too small of a distance each time, the game will feel sluggish and non-responsive. Most likely, we'll just want the ship to move a fraction of a dot each time a tilt is registered. We'll start with .1 dots for now and see how that works for us.

In our initialization section for the ship, we can set these values:

# Initialize the spaceship TILT_FORCE = 0.1 SPACESHIP_STEP = 0.1

Now that the accelerometer is initialized, we can use it to get input from the controller. We do this just like we did in the project:

# Check for user input x_force, y_force, z_force = accel.forces()

Next, we can add code to move the ship based on the input we've received from the accelerometer:

# Draw 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))

Here is the full code for our game at this point (with new changes highlighted with arrows) — when you run it, try tilting your Ready Set STEM CREATOR Kit left and right, and you should see your ship move left and right in response:

# ################################ # 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 # 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 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. The `TILT_FORCE` is the minimum amount of tilt that makes the spaceship move. Can you decrease this to zero to make a very sensitive controller?

2. Once the kit is tilted enough (more than `TILT_FORCE`), the spaceship moves at a constant speed. Tilting really far doesn't make it move faster. Can you modify the code so that the more you tilt, the faster the spaceship moves?