SPACE INVADERS 3
In this project, we continue the development of our Space Invaders game by initializing and getting movement input from our accelerometer.
I/O (Input/Output) RaspberrySTEM Cell
LED Matrix RaspberrySTEM Cell
Accelerometer RaspberrySTEM Cell
Having issues? Check out the Troubleshooting Guide.

In the last project, we initialized our ship where it's supposed to start and we created the loop in 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 RaspberrySTEM 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 our ship.

As we learned in Accelerometer, 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 ACCELEROMETER 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 -- when you run it, try tilting your RaspberrySTEM CREATOR Kit left and right, and you should see your ship move left and right in response:



home | prev | next