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:

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

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

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:

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

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:

  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?